When I run the program, I get OutOfBoundsException at points[0] = pointFiller(scanner); I can not understand why ...

Thank you in advance for the answers.


 public class Point { private int lineCount; private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } Point[] points = new Point[lineCount]; public void pointsCreator() { Scanner scanner = new Scanner(System.in); lineCount = scanner.nextInt(); System.out.println("----------------------"); System.out.println("enter points for first line: "); points[0] = pointFiller(scanner); for (int i = 1; i < lineCount; i++){ System.out.println("----------------------"); System.out.println("enter points for next line:"); points[i] = pointFiller(scanner); } System.out.println("----------------------"); System.out.println(Arrays.toString(points)); } public static Point pointFiller(Scanner scanner) { System.out.print(" - enter x: "); int x = scanner.nextInt(); System.out.print(" - enter y: "); int y = scanner.nextInt(); return new Point(x, y); } @Override public String toString() { return "Points for line {x = " + x + ", y = " + y + '}'; } } 

    2 answers 2

    This is because the default lineCount initialized to zero, then you create an array of zero length:

     Point[] points = new Point[lineCount]; 

    And after creating the array, you enter the value of lineCount .

    Try this:

     public class Point { private int lineCount; private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } Point[] points; public void pointsCreator() { Scanner scanner = new Scanner(System.in); lineCount = scanner.nextInt(); points = new Point[lineCount]; System.out.println("----------------------"); System.out.println("enter points for first line: "); points[0] = pointFiller(scanner); for (int i = 1; i < lineCount; i++){ System.out.println("----------------------"); System.out.println("enter points for next line:"); points[i] = pointFiller(scanner); } System.out.println("----------------------"); System.out.println(Arrays.toString(points)); } public static Point pointFiller(Scanner scanner) { System.out.print(" - enter x: "); int x = scanner.nextInt(); System.out.print(" - enter y: "); int y = scanner.nextInt(); return new Point(x, y); } @Override public String toString() { return "Points for line {x = " + x + ", y = " + y + '}'; } } 
    • points = = new Point[lineCount]; Is something missing here? - Alexon Classic
    • All figured out! - Alexon Classic
    • @AlexonClassic, There was a typo. - post_zeew
    • @AlexonClassic, do not use answers for thanks. Just mark the answer as accepted (green check mark under the vote). - Nofate
     Point[] points = new Point[lineCount]; 

    At the moment this line is lineCount == 0 , lineCount == 0 . After that, you logically get out of the array, because array has zero length.

    Initialize the array after you get the lineCount value from the console.

    • That is, in the pointsCreator method - Alexon Classic
    • Ie yes, in it. - Nofate
    • I need the array to be separate (well, i.e., following the pointsCreator method) - Alexon Classic
    • one
      @AlexonClassic; So declare it separately, and create it in a method. - post_zeew pm
    • Sorry, stupid ... Something I can not understand how ... PS I'm just learning Java - Alexon Classic