twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: NullPointerException ??
|
Posted: Feb 4, 2004 10:51 AM
|
|
NullPointerException means that you have tried to use an object that doesn't really exist yet. In your code, you are creating an array of JLabel objects, but not any JLabel objects. See my comments below.
> it can compile, but NullPointerException occured when i > run it. what's the matter ? > > // code : > import javax.swing.*; > import java.awt.*; > > class Temp extends JFrame { > > JLabel[] lbl; > > public Temp() { > lbl = new JLabel[1]; //creates an array
//add the line below to actually create lbl[0] = new JLabel(); //the JLabel
> Icon image = new ImageIcon("1.jpg"); //without the line that I added, the lbl[0] //object does not exist yet. > lbl[0].setIcon(image); > getContentPane().add(lbl[0]); > } > > public static void main(String args[]) { > Temp t = new Temp(); > t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > t.show(); > t.pack(); > } > }
|
|