I've got a constructor for a class that iterates over a ArrayList of ImageIcon
s, but half way through it crashes with a NullPointerException. Any ideas why?
for (ImageIcon i : dm.GetIcons())
{
_labels.add(new JLabel(i));
}
public ArrayList<ImageIcon> GetIcons(){
return _icons;
}
I tried throwing GetIcons
into a variable and set a breakpoint, it has 8 items (exactly as I expect) but by the time I Step Over my loop 2 or 3 times it crashes. No idea what I'm doing wrong. Newbie to Java. Any thoughts?
You have null element/s in the List of ImageIcon
s
You can check for nullity first,
for (ImageIcon i : dm.GetIcons())
{
if (i != null) {
_labels.add(new JLabel(i));
}
}
Only if it is ok to skip null elements ie depending on what your app needs.
您的ImageIcon ArrayList可能包含一些空对象或没有值的Empty对象
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.