#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Error When dealing with Swing


    I'm getting an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" error without a line reference. I'm also new to java, so easy with the lingo when describing what is wrong.

    Thanks in advance.

    Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
     
    public class SwingExample extends JFrame{
    
    		public SwingExample(){
    			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(file);
    			
    			menubar.add(file);
    			
    			setJMenuBar (menubar);
    			
    			setTitle("I'm Awesome... again");
    			setSize(800,600);
    			setLocationRelativeTo (null);
    			setDefaultCloseOperation(EXIT_ON_CLOSE);
    			
    			
    		}
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
            	public void run(){
            		SwingExample se= new SwingExample();
            		se.setVisible(true);
            	}
            });
            
    }
    }
    Errors

    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(Unknown Source)
    	at SwingExample.initUI(SwingExample.java:21)
    	at SwingExample.<init>(SwingExample.java:15)
    	at SwingExample$2.run(SwingExample.java:51)
    	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.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,613
    Rep Power
    1945
    It refer to ImageIcon.

    When you create the ImageIcon, you have following as paramater:
    Code:
    getClass().getResource("exit.png")
    Try run it in it own line and check its return value, as it can return null, which is the error message you received.
    Something like this (untested):
    Code:
    URL myPicLocation = getClass().getResource("exit.png");
    if(myPicLocation == NULL)
    {
    	// do something 
    }
    ImageIcon icon = new ImageIcon(myPicLocation);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    So I did this(below), which I think was your correction added correctly. I left the comment in because if I can't get it to compile then what's the point? Right?

    Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.net.URL;
    
    import javax.swing.ImageIcon;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
     
    public class SwingExample extends JFrame{
    
    		public SwingExample(){
    			initUI();
    		}
    		
    		public final void initUI(){
    			
    			JMenuBar menubar = new JMenuBar();
    			
    			URL myPicLocation = getClass().getResource("exit.png");
    			if(myPicLocation == null)
    			{
    				// do something 
    			}
    			
    			ImageIcon icon = new ImageIcon(myPicLocation);
    			
    			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(file);
    			
    			menubar.add(file);
    			
    			setJMenuBar (menubar);
    			
    			setTitle("I'm Awesome... again");
    			setSize(800,600);
    			setLocationRelativeTo (null);
    			setDefaultCloseOperation(EXIT_ON_CLOSE);
    			
    			
    		}
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
            	public void run(){
            		SwingExample se= new SwingExample();
            		se.setVisible(true);
            	}
            });
            
    }
    }

    and got this

    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(Unknown Source)
    	at SwingExample.initUI(SwingExample.java:27)
    	at SwingExample.<init>(SwingExample.java:16)
    	at SwingExample$2.run(SwingExample.java:57)
    	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.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    The problem is that you need something there to make sure myPicLocation isn't null.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Like what? Because so far, we have deviated from my knowledge of java. Honestly this all came about because I was following a tutorial on Swing and this error came up. The first code I posted was a literally copy and paste from the example code of the lesson.

    So the value type of myPicLocation is a URL, something I have never dealt with yet. How do I return a URL with a value.
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,613
    Rep Power
    1945
    You have created a picture called 'exit.png'? and placed it in the same folder as the class file?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Yea guys I appreciate everyting you have done. I made a mistake and it was a jpeg instead of a png


    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo