- #1
UltimateSomni
- 62
- 0
The error: "Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at Buttons.<init>(Windows.java:12)
at Buttons.main(Windows.java:38)
Java Result: 1"
The Windows.java code:
_______________________________________
import javax.swing.*;
class Buttons extends JFrame
{
JPanel pnl = new JPanel();
ClassLoader ldr = this.getClass().getClassLoader();
java.net.URL tickURL = ldr.getResource("tick.png");
java.net.URL crossURL = ldr.getResource("cross.png");
ImageIcon tick = new ImageIcon( tickURL ); //LINE 12
ImageIcon cross = new ImageIcon( crossURL );
//ImageIcon tick = new ImageIcon( "tick.png" );
//ImageIcon cross = new ImageIcon( "cross.png" );
JButton btn = new JButton( "Click Me" );
JButton tickBtn = new JButton( tick );
JButton crossBtn = new JButton( "STOP", cross );
public Buttons()
{
super("Swing Window");
setSize( 500,200 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
add(pnl);
pnl.add( btn );
pnl.add( tickBtn );
pnl.add( crossBtn );
setVisible( true );
}
public static void main ( String[] args )
{
Buttons gui = new Buttons();//LINE 38
}
}
_____________________________________________
and line 167 of ImageIcon.java looks like this :
public ImageIcon (URL location) {
this(location, location.toExternalForm()); //line 167
}
_______________________________________________
according to the book this should work
at javax.swing.ImageIcon.<init>(ImageIcon.java:167)
at Buttons.<init>(Windows.java:12)
at Buttons.main(Windows.java:38)
Java Result: 1"
The Windows.java code:
_______________________________________
import javax.swing.*;
class Buttons extends JFrame
{
JPanel pnl = new JPanel();
ClassLoader ldr = this.getClass().getClassLoader();
java.net.URL tickURL = ldr.getResource("tick.png");
java.net.URL crossURL = ldr.getResource("cross.png");
ImageIcon tick = new ImageIcon( tickURL ); //LINE 12
ImageIcon cross = new ImageIcon( crossURL );
//ImageIcon tick = new ImageIcon( "tick.png" );
//ImageIcon cross = new ImageIcon( "cross.png" );
JButton btn = new JButton( "Click Me" );
JButton tickBtn = new JButton( tick );
JButton crossBtn = new JButton( "STOP", cross );
public Buttons()
{
super("Swing Window");
setSize( 500,200 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
add(pnl);
pnl.add( btn );
pnl.add( tickBtn );
pnl.add( crossBtn );
setVisible( true );
}
public static void main ( String[] args )
{
Buttons gui = new Buttons();//LINE 38
}
}
_____________________________________________
and line 167 of ImageIcon.java looks like this :
public ImageIcon (URL location) {
this(location, location.toExternalForm()); //line 167
}
_______________________________________________
according to the book this should work