Im new in Java socket and i try to develop a small game through socket, everything is ok on server but in client , i got trouble when i create a list of JLabel to show a letter “_” for each letter of word
Ok i show my code you will see what trouble i got.
my Snippett code like this:
List<JLabel>labels;
private void getWordLabels(String word){
char[] chars = word.toCharArray();
System.out.println(chars);
int gapBetweenLetter = 300/chars.length;
labels = new ArrayList<JLabel>();
for(int i =0; i< chars.length ;i++){
// JLabel l = new JLabel("_");
labels.add(new JLabel());
labels.get(i).setText("_");
//this.add(l);
//l.setBounds(10, 10, 10, 10);
//l.show();
this.removeAll();
this.revalidate();
this.repaint();
labels.get(i).setBounds((i*gapBetweenLetter) + 10, 100, 50, 50);
this.add(labels.get(i));
labels.get(i).show();
initComponents();
/* labels.add(new JLabel("_"));
this.add(labels.get(i));
//labels[i].setText("_");
System.out.println(labels.get(i));
* */
}
}
}
but i got null pointer exception, i don’t know why?
Someone can help me or show what wrong in my code.
Thank you!
Can’t see anything obvious, so I’d start by refactoring to
which may help you to see the problem. It also avoids repeating the get(i) [probably not expensive on an ArrayList, but still]
BTW: what type is ‘this’?