JList is returning null value
-
I'm showing or selecting images from a list. My code for listselectionlistener is:
listener = new ListSelectionListener() {
@Override public void valueChanged(ListSelectionEvent e) { index = imageList.getSelectedIndex(); imageList.setSelectedIndex(index); o = imageList.getSelectedValue(); if (o instanceof BufferedImage) { imageView.setIcon(new ImageIcon((BufferedImage)o)); } } }; imageList.addListSelectionListener(listener);
to get next item in JList (imageList) I'm coding like this:
public void nextimage()
{
index ++;imageList.addListSelectionListener(listener);
imageList.setSelectionMode(ListSelectionModel.SINGLE\_SELECTION); imageList.setSelectedIndex(index); -----here is error: Null Pointer Exception
}
I initialize index as int index=0; Program in starting returning right value but after 4 or 5 images it's not working. How can I change the Item in this list? Please suggest your opinions.
-
I'm showing or selecting images from a list. My code for listselectionlistener is:
listener = new ListSelectionListener() {
@Override public void valueChanged(ListSelectionEvent e) { index = imageList.getSelectedIndex(); imageList.setSelectedIndex(index); o = imageList.getSelectedValue(); if (o instanceof BufferedImage) { imageView.setIcon(new ImageIcon((BufferedImage)o)); } } }; imageList.addListSelectionListener(listener);
to get next item in JList (imageList) I'm coding like this:
public void nextimage()
{
index ++;imageList.addListSelectionListener(listener);
imageList.setSelectionMode(ListSelectionModel.SINGLE\_SELECTION); imageList.setSelectedIndex(index); -----here is error: Null Pointer Exception
}
I initialize index as int index=0; Program in starting returning right value but after 4 or 5 images it's not working. How can I change the Item in this list? Please suggest your opinions.
You need to check if the JList does contain so much values. You're not coding safe. You need to take more care. You can never expect a value to be there as you need it - always check if the value is valid before you use it!
regards Torsten When I'm not working
-
You need to check if the JList does contain so much values. You're not coding safe. You need to take more care. You can never expect a value to be there as you need it - always check if the value is valid before you use it!
regards Torsten When I'm not working
Thanks for your quick reply. Will you be more specific on checking the value? As I'm selecting the value from Jlist that is showing thumbnails. I'm not getting the exact way for selecting next item.:confused:
-
Thanks for your quick reply. Will you be more specific on checking the value? As I'm selecting the value from Jlist that is showing thumbnails. I'm not getting the exact way for selecting next item.:confused:
"TorstenH" want to say you that you have to check the "index" value before using it to set the selected item. You should check "index" value whether it is in the range of your image list size.
Regards Shubhashish
-
"TorstenH" want to say you that you have to check the "index" value before using it to set the selected item. You should check "index" value whether it is in the range of your image list size.
Regards Shubhashish
Well I did this. The only problem was SwingUtlities.InvokeLater().
-
Well I did this. The only problem was SwingUtlities.InvokeLater().
That's a function, not a problem. The problem would be that you tried to update the GUI while you where using it. SwingUtilities.InvokeLater()[^] would cause the Thread to wait and update the GUI when possible.
regards Torsten When I'm not working