New To Java - why does NullPointerException happen in my code?!?!?
hey people! i have a huge problem! there's a stupid error happening in my code! i dunno why it happens! i've checked everything!
publicvoid updateLabel(String name){
ImageIcon icon = createImageIcon(name);
System.out.println(icon);
if(icon==null){
icon= createImageIcon("image/question_mark.jpg");
pic.setIcon(icon);
pic.setText("Image not found.");
}
elseif (icon!=null){
pic.setIcon(icon);//line 757
pic.setText("");
System.out.println("Pumunta sia d2!");
System.out.println("File found!");
}
}
public ImageIcon createImageIcon(String path){
java.net.URL imgURL = MyWindow.class.getResource(path);
if (imgURL !=null){
returnnew ImageIcon(imgURL);
}else{
System.err.println("Couldn't find file: " + path);
returnnull;
}
}
i added that method to another method called updater(Node n) which refreshes the values to be displayed in my gui eyerytime a change occurs like searching for that node.
under updater(Node n) is updateLabel(n.image)
image is a String variable in another class: Node
pic=new JLabel();
pic.setFont(pic.getFont().deriveFont(Font.ITALIC));
pic.setHorizontalAlignment(JLabel.CENTER);
pic.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(),
BorderFactory.createEmptyBorder(5,5,5,5)));
pic.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0,0,10,0),
pic.getBorder()));
pic.setPreferredSize(new Dimension(300, 330));
that is defined in my constructor MyWindow()
now, why do i get that freakin error message!?!!? there shouldn't be a nullpointerexcpetion!!!!
output:
--Configuration: <Default>--
file:/C:/Documents%20and%20Settings/spicefoo/My%20Documents/mp2/image/a.jpg
Exception in thread "main" java.lang.NullPointerException
at MyWindow.updateLabel(MyWindow.java:757)
at MyWindow.updater(MyWindow.java:624)
at MyWindow.<init>(MyWindow.java:353)
at MyWindow.main(MyWindow.java:533)
Process completed.

