Hi All,
I am new to swing when i tried to run one swing example it gives error like
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Pls help on that
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi All,
I am new to swing when i tried to run one swing example it gives error like
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Pls help on that
Can you post the code you are using? and even better the full stack trace to go with it?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class Example extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public Example() {
initUI();
}
public final void initUI() {
JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenuItem eMenuItem = new JMenuItem("Exit", icon);
eMenuItem.setMnemonic(KeyEvent.VK_C);
eMenuItem.setToolTipText("Exit application");
eMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
file.add(eMenuItem);
menubar.add(file);
setJMenuBar(menubar);
setTitle("Simple menu");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Example ex = new Example();
ex.setVisible(true);
}
});
}
}
Stack Trace for exception ........
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Example.initUI(Example.java:29)
at Example.<init>(Example.java:23)
at Example$2.run(Example.java:59)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
Looks like exit.png isn't found.
so how can i resolve this either i should have to keep exit.png in my locale and give the exact path of exit.png
As long as the image is in a source folder than you should be able to reference it (aka. your code as-is works). The problem comes in when you have an image in a non source folder. That is a whole new ball game.
As a further note, i would recommend getting the resource separately from creating the ImageIcon instance. This way you can check to see if the resource exists and fail gracefully instead of having the blatant error.
getResource() uses the classpath to find the resource. Make sure the image file is in a folder on the classpath.
If you don't understand my answer, don't ignore it, ask a question.