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.

[3486 byte] By [spicefooa] at [2007-11-26 23:17:46]
# 1

Maybe you're trying to access a variable that is out of scope? Perhaps you had accidentally declared it in a method and now it has lost scope?

I can't really tell. Which line is line 757 in your code? Find that, and if it is a variable error, make sure you declared it within the scope of the method/class.

lethalwirea at 2007-7-10 14:19:19 > top of Java-index,Java Essentials,New To Java...
# 2

Maybe you're trying to access a variable that is out

of scope?

Assuming the code you posted is up-to-date, the only variable being referenced in updatLabel() is pic. pic isn't being passed in, so I assume it's a field.

I don't see where updater() is called in your constructor, so it's either before or after the snippet of your constructor that you've posted. Is updater() being called before the rest of your code? If so, then you have "pic." happeneing before you've assigned to pic, hence the NPE.

This is my guess. You might need to share the whole constructor, and provide line #s, so we have a full picture of what is going on.

bheilersa at 2007-7-10 14:19:19 > top of Java-index,Java Essentials,New To Java...