Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with "import javax.swing.ImageIcon;"

807597Feb 18 2005 — edited Feb 18 2005
Hello people, I am getting back into Java after a long pause. I am starting from scratch, because my Java skills weren't really that good. Anyways, I just got the new "How to Program Java, Sixth Edition (Deitel)" and I am doing one example of theirs and they are importing the "import javax.swing.ImageIcon;" but at complie time it gives me a error. I did check to see if I had that class(methods) and it was in the directory. Here is the error and the code:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
at ButtonFrame.<init>(ButtonFrame.java:24)
at ButtonTest.main(ButtonTest.java:9)
Press any key to continue...

Here is the copy of the code:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.*;

public class ButtonFrame extends JFrame
{
private JButton plainJButton;
private JButton fancyJButton;

public ButtonFrame()
{
super( "The New Roman Empire" );
setLayout( new FlowLayout() );

plainJButton = new JButton( "Plain Button" );
add(plainJButton);

Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ));
Icon bug2 = new ImageIcon( getClass().getResource( "bug2.gif" ));
fancyJButton = new JButton( "Fancy Button", bug1 );
add( fancyJButton );

ButtonHandler handler = new ButtonHandler();
fancyJButton.addActionListener(handler);
plainJButton.addActionListener(handler);

}

private class ButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog( ButtonFrame.this, String.format(
"You Pressed: %s", event.getActionCommand() ) );

}
}

}

Thanks in advance.

Roman03
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 18 2005
Added on Feb 18 2005
8 comments
486 views