java - ListSelectionListener null pointer error -
i'm trying specific color specific category display on south jpanel nullpointerexeception error. doing wrong?
//this arrays holding category names , colors.
try checking if selected index in list greater -1.
//this arrays holding category names , colors. string[] cati = {"ingen", "matställen", "skolor", "kyrkor", "kollektiv trafik"}; color[] colors = {color.white, color.blue, color.green, color.yellow, color.pink}; // inner class in super class. class kategorifärg implements listselectionlistener { public void valuechanged(listselectionevent event) { if (kategorlista.getselectedindex() > -1) { system.out.println("does work?"); syd.setbackground(colors[kategorilista.getselectedindex()]); //syd south jpanel } } }
-1 means no item in list selected. if error occurs on if statement, kategorlista null , needs initialized. sample code, provided, looks initialized it. otherwise, syd null , needs initialized.
if of running. in code provided.
öst.add(kategorilista);
is öst
initialized? error you're getting should have been pointing @ line of code causing error.
edit
based on code you've added... make these 2 lines of codes class variables
string[] cati = { "ingen", "matställen", "skolor", "kyrkor", "kollektiv trafik" }; color[] colors = { color.white, color.blue, color.green, color.yellow, color.pink };
you have them coded in contructor , cannot seen anywhere else in class. if have them declared class variables, in constructor change code to:
cati = { "ingen", "matställen", "skolor", "kyrkor", "kollektiv trafik" }; colors = { color.white, color.blue, color.green, color.yellow, color.pink };
i believe name call shadowing... original code, shadowed class variables, , local variables initialized instead of class variables.
Comments
Post a Comment