Re: JList getSelectedValue returning null




ricky.clarkson@xxxxxxxxx wrote:
> This is really poor code, and running it through a code checking
> utility such as CheckStyle would probably give the answer:
>
> Object o=null;
> try {
> o=data.elementAt(index);
> } catch (Exception ex) {
> //System.out.println("Index out of bounds" +
> ex.toString());
> }
> return o;
>
> That looks like a good candidate for returning null. You are
> swallowing all possible exceptions. DON'T.
>
> http://findbugs.sourceforge.net/bugDescriptions.html#REC_CATCH_EXCEPTION
> and
> http://checkstyle.sourceforge.net/config_coding.html#IllegalCatch


Many thanks! I'll look into CheckStyle. Why should the index go out of
bounds? And what value should be returned to the caller if the index is
out of bounds? The problem is:
- say there are 3 elements to begin with in the JList
- by some method you delete the last element of the JList
- call to JList's getSelectedValue tries to return the 3rd element
(index = 2)
I tried to use setSelectedIndex method of JList with argument 0. But
that
doesn't seem to work. Any other ideas are welcome.

Thanks again.

.