Index out of bounds exception trying to create ArrayList with 1-based indices

17,154

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.

Share:
17,154
rasperryPi
Author by

rasperryPi

Updated on June 14, 2022

Comments

  • rasperryPi
    rasperryPi almost 2 years

    So up until this point I've just used points.add(new Point(x,y)) to add points to my ArrayList. 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 the counter from 0 to 1, 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 the for-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
    rasperryPi over 8 years
    I 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)); and counter set to 0. But in my for-loop inside the paint-method, I should use index+1?
  • Reut Sharabani
    Reut Sharabani over 8 years
    Yes. Use an offset of 1 from the index. You didn't show the calculations but that's what your description indicates.
  • Tamas Hegedus
    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
    rasperryPi over 8 years
    Ok, 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 start int i=1, then I won't be including the first point since the ArrayList is starting from 0. Am I missing something?
  • rasperryPi
    rasperryPi over 8 years
    My ultimate goal is that i should be 1 in for (int i=0...
  • rasperryPi
    rasperryPi over 8 years
    Yes. 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
    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
    Reut Sharabani over 8 years
    Where do you need 1, 2, 3, ... instead of 0, 1, 2, ...? That's where you're going to use i + 1. When accessing the ArrayList use i as is.
  • rasperryPi
    rasperryPi over 8 years
    Sure! This is the error I'm getting: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  • Tamas Hegedus
    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
    Reut Sharabani over 8 years
    You 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
    rasperryPi over 8 years
    I don't see any changes in your snippet different from mine..?
  • John Kugelman
    John Kugelman over 8 years
    I did because you originally posted it with just the first two sentences, which weren't very helpful. Correct but not helpful.
  • brso05
    brso05 over 8 years
    @JohnKugelman actually it was helpful it described his problem and why the exception was being thrown.
  • brso05
    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
    Tamas Hegedus over 8 years
    I suspect that your list is null. Please double check that you save the generated list to a field, and see my edit
  • Reut Sharabani
    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)