• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advice on fixing the index out of bounds error  RSS feed

 
James obrien
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Is it ok to disable a listener or should I go back to the drawing board?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you provide some more context? Because it's really hard to understand what your intention is with this code and from just a few lines of code, we can't know why and when you get an index out of bounds error.

TellTheDetails
 
James obrien
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The remove function on the jlist GUI -1's from the index. If I try and get the current get selected list it throws an exception if the value is -1.Thus, my solution was to change the value to the last index value e.g 3 or however many in the list. My problem is when I get to the last value I want the listener to stop listening, so that the exception will not be thrown? Sorry if I make no sense I have Aspergers syndrome. I might also want to restart listening I guess. Is it possible to stop and start listening? or is this bad practise?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still hard to understand exactly what you're after.

JList.getSelectedIndex() returns -1 if no elements in the list are selected.
JList.getLastVisibleIndex() returns -1 if nothing is visible or the list is empty.

I guess that what you want to do is (I'm not sure, you haven't told us):

Get the index of the selected element. If nothing is selected, get the index of the last visible item in the list. Then set the text of employeeName based on the index that was found. If nothing is selected or nothing is visible or the list is empty, you probably want to skip that last step. You can do that by simply checking if the index is not -1.

 
James obrien
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I was getting confused. It now works with your code, problem solved.
I had thought it was always -1 when I was removing an index.
I will comment my code next time and provide a full description.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!