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.

Results 1 to 7 of 7

Thread: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    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


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    Can you post the code you are using? and even better the full stack trace to go with it?

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    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)

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
    Looks like exit.png isn't found.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    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

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    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.

Similar Threads

  1. Replies: 2
    Last Post: November 24th, 2012, 09:18 AM
  2. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM