Results 1 to 18 of 18
  1. #1
    daverave1212 is offline Member
    Join Date
    Jul 2015
    Posts
    9
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException but I don't understand why?

    So I'm trying to make a simple program where I have a listmodel and a list.
    I have 2 buttons, 'Adauga' which means 'add' and 'Sterge' which means 'remove'
    When I try to remove an item from the listmodel/list, I get an ArrayIndexOutOfBoundsException and I can't figure out why.
    I can successfully add items to the listmodel and the list.
    Here's the code:
    public void actionPerformed(ActionEvent e) {
    if(e.getSource()==adauga){

    list.add(new Om(nume.getText(), prenume.getText(), Long.parseLong(cnp.getText()), Long.parseLong(telefon.getText())));
    listmodel.addElement(list.get(counter).toString()) ;
    counter++;}
    if(e.getSource()==sterge){
    listmodel.removeElementAt(index);
    list.remove(list.get(index));
    }}
    public void valueChanged(ListSelectionEvent arg0) {
    index=jlist.getSelectedIndex();

    }

    In my program the parantheses are closed normally and if they aren't here, I just failed at copy/paste
    Can anyone help? Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Once you've removed the element from the ListModel, it is already removed from the JList. Assuming those are the classes involved.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    daverave1212 is offline Member
    Join Date
    Jul 2015
    Posts
    9
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by DarrylBurke View Post
    Once you've removed the element from the ListModel, it is already removed from the JList. Assuming those are the classes involved.

    db
    That's not it.
    listmodel.removeElementAt(index);
    list.remove(list.get(index));
    They both give the same AIOOBE: -1 separately too
    Note: my 'list' isn't connected to the listmodel. a 'jlist' is.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    It would help if you provided a short, compilable "working" example of the problem. And did you try sprinkling print statements thruout your program (like the value of index and the size of your various lists) to see the what is going on? And please place your code between code tags. See Darryl's link on BB Code list.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    daverave1212 is offline Member
    Join Date
    Jul 2015
    Posts
    9
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by jim829 View Post
    It would help if you provided a short, compilable "working" example of the problem. And did you try sprinkling print statements thruout your program (like the value of index and the size of your various lists) to see the what is going on? And please place your code between code tags. See Darryl's link on BB Code list.

    Regards,
    Jim
    yes, I did that, but I didn't post them in this thread
    Apparently, the 'index' changes when I select a list in the item:

    public void valueChanged(ListSelectionEvent arg0) {
    System.out.println(jlist.getSelectedIndex());
    this.index=jlist.getSelectedIndex();
    System.out.println(index);
    System.out.println(list.get(index).toString());

    }

    But when I call the actionPerformed method and SOP(index) it displays -1.
    Anyone knows why? I have tried using this.index but no results.

    UPDATE:
    Here's the part of the code that is wrong, but I still don't understand why:
    int jindex = jlist.getSelectedIndex();
    System.out.println(jindex);
    System.out.println(listmodel.getElementAt(jindex). toString());
    listmodel.removeElementAt(jindex);

    Now, if I just keep:
    int jindex = jlist.getSelectedIndex();
    System.out.println(jindex);
    System.out.println(listmodel.getElementAt(jindex). toString());
    it works out normally, the correct index is printed and everything is ok.
    However, if I add listmodel.removeElementAt(jindex); on top of that, the jindex suddenly changes to -1.

    BUT if instead of jindex i put, say, 1 or 2 or whatever, it works perfectly :|
    This makes no sense and i don't know what to do
    I tried casting jindex to int but it's the same thing
    Last edited by daverave1212; 09-26-2015 at 12:00 PM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    What's so difficult to understand about using code tags?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by daverave1212 View Post
    But when I call the actionPerformed method and SOP(index) it displays -1.
    Have you read the API for JList#getSelectedIndex()? What does it say about the possibility of returning -1?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    check before removing
    if (listmodel.getSize() > 0)
    list.remove(list.get(index));
    Ramya:cool:

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by RamyaSivakanth View Post
    check before removing
    if (listmodel.getSize() > 0)
    list.remove(list.get(index));
    No, that's not it. I suggest that you too read the API I mentioned at #8. Also the first of the two links I provided at #2.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    daverave1212 is offline Member
    Join Date
    Jul 2015
    Posts
    9
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by DarrylBurke View Post
    No, that's not it. I suggest that you too read the API I mentioned at #8. Also the first of the two links I provided at #2.

    db
    I already did and tried everything I knew

    Also, I never quite understood how forum tags work lol. I know html tho...
    Java Code:
     meh

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by daverave1212 View Post
    I already did and tried everything I knew
    As I said before, you should provide a short, compilable example that runs and demonstrates the problem. Then we might be able to help you. It is practically impossible to help you debug a code snippet out of context.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    I know html tho.
    Code tags use the same principles and syntax. There is a beginning tag before the code to be formatted and and another tag at the end of the code to be formatted.
    See the link for how to spell the tags.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by daverave1212 View Post
    I already did and tried everything I knew
    You're forcing me to repeat myself.
    Quote Originally Posted by DarrylBurke View Post
    Have you read the API for JList#getSelectedIndex()? What does it say about the possibility of returning -1?
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    daverave1212 is offline Member
    Join Date
    Jul 2015
    Posts
    9
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by DarrylBurke View Post
    You're forcing me to repeat myself.


    db
    repeat what? I checked the api like 5 times now and I haven't found a solution.
    I googled through like the first 20 pages of google and I haven't managed to find a solution.
    To this day, I haven't found a solution.
    I really need to finish this project. If I don't finish it, I won't be able to take my exam. I asked some colleagues but nobody knows why this is happening.

    For your concern:
    Returns the smallest selected cell index; the selection when only a single item is selected in the list. When multiple items are selected, it is simply the smallest selected index. Returns -1 if there is no selection.
    I made sure every time I had an item selected.

    As I said, getSelectedIndex() works perfectly fine. It just changes when used with either removeElementAt() or remove()
    Sorry, I am just very pissed because it doesn't seem to make any sense

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Can you post the code in code tags that has the problem? Add a comment to the statement where the ArrayIndexOutOfBoundsException happens.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by daverave1212 View Post
    I really need to finish this project. If I don't finish it, I won't be able to take my exam. I asked some colleagues but nobody knows why this is happening.
    Then why have you ignored my request for a short, compilable, executable, program that demonstrates the problem?

    UPDATE:
    Here's the part of the code that is wrong, but I still don't understand why:
    int jindex = jlist.getSelectedIndex();
    System.out.println(jindex);
    System.out.println(listmodel.getElementAt(jindex). toString());
    listmodel.removeElementAt(jindex);
    If you don't understand why, then how to you know that's where the problem lies?

    Regards,
    Jim
    Last edited by jim829; 09-28-2015 at 12:32 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    daverave1212 is offline Member
    Join Date
    Jul 2015
    Posts
    9
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException but I don't understand why?

    Quote Originally Posted by jim829 View Post
    Then why have you ignored my request for a short, compilable, executable, program that demonstrates the problem?



    If you don't understand why, then how to you know that's where the problem lies?

    Regards,
    Jim
    I did that, but I haven't been able to reproduce the problem. It worked perfectly fine. In fact, I just copy pasted that portion of the code and it worked.
    Another problem that appeared is when I add, say, string elements to the listmodel with addElement("String") and I run the program, I get the same exception when I try to select them and I'm not even able to select them.
    HOWEVER it works when I select multiple items and I try to remove them. So, apparently, the program considers one item selected as no item selected. How do I fix that?

    UPDATE:
    I managed to partially fix the problem. I still don't know why it occured and why this fixed a part of it, but I used
    Java Code:
    listmodel.removeElement(listmodel.get(jlist.getSelectedIndex()));
    which seems overly complicated to me.
    Why is this partial then? Because it only works when the selected item already has another duplicate in the list, which is not helpful at all almost

    UPDATE:
    I managed to fix the problem, but I have no idea how. I just made a new program in which I copy-pasted everything chunk by chunk and I renamed the defaultlistmodel (listmodel) to dlm. It's strange...

    UPDATE:
    Ok, WTF. I added some documentation to the new code and it randomly doesn't work anymore. I deleted the documentation and it works. This was the problem at the old program too...
    Last edited by daverave1212; 09-28-2015 at 07:22 PM.

Similar Threads

  1. ArrayIndexOutofBoundsException
    By javagirl in forum New To Java
    Replies: 1
    Last Post: 02-10-2014, 02:30 AM
  2. ArrayIndexOutOfBoundsException
    By np2392 in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 10:28 PM
  3. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 06:50 PM
  4. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 01:25 AM
  5. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •