Index out of bounds exception trying to create ArrayList with 1-based indices
Your trying to set an index
that doesn't exist.
The size of your ArrayList
is 0 but you are trying to access index 1
which doesn't exist.
You can use ArrayList.add()
to add another element to your ArrayList
. If you use set()
the index
must actually exist in the ArrayList
.
rasperryPi
Updated on June 14, 2022Comments
-
rasperryPi almost 2 years
So up until this point I've just used
points.add(new Point(x,y))
to add points to myArrayList
. However, I found out that I need the first point to be in index=1 in order to make it possible to multiply the number for each step. So I tried setting thecounter
from0
to1
, and as expected I knew I would get a error because of the range, but I've tried changing up the condition within the while-loop, but nothing seems to work.Here's my code:
ArrayList<Point> points = new ArrayList<>(); int counter = 1; int nPoints = 12; while (counter <= nPoints) { x = (int) (centerX + r * Math.cos(start)); y = (int) (centerY + r * Math.sin(start)); points.set(counter, (new Point(x, y))); //points.add(new Point(x,y)); counter++; }
This is the error that I am getting:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.set(Unknown Source)
I've deleted much of the code that isn't relevant for this problem in order to make it easier to read.
Edit:
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; a = getWidth() /2; b = getHeight() /2; g2d.setColor(Color.RED); //draw cirle g2d.drawOval(a-r, b-r, 2*r, 2*r); //draw lines for (int i= 1; i < points.size();i ++) { g2d.drawLine(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y); } g2d.dispose(); }
I've created a circle in which I'm trying to draw lines between points along the circumference. Here I want the 1st point to connect to 2, 2 to 4, 3 to 6, 4 to 8 and so on... So the pattern here is is that it is multiplied by 2 for each time. So my initial thought was that I could use the
i
inside thefor
-loop to multiply by 2 each time. But since I have my first point in i=0 inside the ArrayList, I am having trouble. -
rasperryPi over 8 yearsI am not sure if I understood your comment right, but, you're trying to say that I should just keep it the way I had:
points.add( new Point(x, y));
andcounter
set to 0. But in myfor
-loop inside thepaint
-method, I should useindex+1
? -
Reut Sharabani over 8 yearsYes. Use an offset of 1 from the index. You didn't show the calculations but that's what your description indicates.
-
Tamas Hegedus over 8 years@rasperryPi then I'm afraid of you will have to share the code that draws. Anyway, I have found inconsistency in your code, see edit
-
rasperryPi over 8 yearsOk, so here's my problem:
for (int i= 0; i < points.size()-1;i ++) { g2d.drawLine(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y); }
So then if I startint i=1
, then I won't be including the first point since the ArrayList is starting from 0. Am I missing something? -
rasperryPi over 8 yearsMy ultimate goal is that
i
should be 1 infor (int i=0...
-
rasperryPi over 8 yearsYes. I'm aware of that, I removed all that in order to make it easier to read. I thought I would be confusing having stuff that didn't really matter for my problem.
-
Tamas Hegedus over 8 years@rasperryPi Okay, I didn't know that. Could you please tell us what was the error when running this code?
-
Reut Sharabani over 8 yearsWhere do you need
1, 2, 3, ...
instead of0, 1, 2, ...
? That's where you're going to usei + 1
. When accessing the ArrayList usei
as is. -
rasperryPi over 8 yearsSure! This is the error I'm getting:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
-
Tamas Hegedus over 8 years@raspberryPi Are you sure you are not reading the 0th element of the list? Try duplicating the first point, se acessing the 0th and 1st element will result in the same object, see edit
-
Reut Sharabani over 8 yearsYou should add more details to you're question and present some more of your logic. It's unclear what
i*2
is to acheive here. -
rasperryPi over 8 yearsI don't see any changes in your snippet different from mine..?
-
John Kugelman over 8 yearsI did because you originally posted it with just the first two sentences, which weren't very helpful. Correct but not helpful.
-
brso05 over 8 years@JohnKugelman actually it was helpful it described his problem and why the exception was being thrown.
-
brso05 over 8 years@JohnKugelman we will have to agree to disagree. You should be more concerned with helping people and less concerned with downvoting...
-
Tamas Hegedus over 8 yearsI suspect that your list is null. Please double check that you save the generated list to a field, and see my edit
-
Reut Sharabani over 8 years@rasperryPi the limit of the for loop is different, since you need to have a "next element" (so only iterate to the one before last element, which is size-1)