I´m writing a program where different symbols are drawn on an image based on what the user selects in a JList. This works fine, but my problem is that I want to place a symbol on the image if nothing is selected in the JList as well. Is there some way to check if the selection is empty? This is my code right now, and it throws me a NullPointerException if I don´t select anything from the JList.
if(categoriesList.getSelectedValue().equals("Bus")) {
BusSymbol bs = new BusSymbol(x, y);
mp.add(bs);
}
else if
(categoriesList.getSelectedValue().equals("Underground")) {
UndergroundSymbol us = new UndergroundSymbol(x,y);
mp.add(us);
}
else if (categoriesList.getSelectedValue().equals("Train")) {
TrainSymbol ts = new TrainSymbol(x,y);
mp.add(ts);
}
else if (categoriesList.getSelectedValue().equals(null)) {
NoCategorySymbol ncs = new NoCategorySymbol(x,y);
mp.add(ncs);
}
mp.validate();
mp.repaint();
nullhas no.equalsmethod. If something might be null, you want to check it with== null(or ideally with another method ; isn't there ahasSelectedValue()method on that API ?) - Aaron