5 Replies - 265 Views - Last Post: 30 July 2013 - 11:04 PM Rate Topic: -----

#1 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 133
  • Joined: 29-June 13

Why getting so many errors?

Posted 30 July 2013 - 09:44 PM

hi im getting all these errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
	at menuexample.initUI(menuexample.java:25)
	at menuexample.<init>(menuexample.java:21)
	at menuexample$2.run(menuexample.java:53)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
	at java.awt.EventQueue.access$400(EventQueue.java:82)
	at java.awt.EventQueue$2.run(EventQueue.java:676)
	at java.awt.EventQueue$2.run(EventQueue.java:674)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



i have no idea what that all is or even means...

here the code
/*
 * to implement a MenuBar, we use three objects.
 *JMenuBar, JMenu & JMenuItem
 */

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 menuexample extends JFrame {

    public menuexample() {
        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_E);
        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() {
            	menuexample ex = new menuexample();
                ex.setVisible(true);
            }
        });
    }
}



Help is really appreciate :D

Is This A Good Question/Topic? 0
  • +

Replies To: Why getting so many errors?

#2 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Why getting so many errors?

Posted 30 July 2013 - 09:51 PM

ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));


It appears to be this line. Is seems that getResource is returning null.
Was This Post Helpful? 0
  • +
  • -

#3 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 133
  • Joined: 29-June 13

Re: Why getting so many errors?

Posted 30 July 2013 - 10:06 PM

View PostConciselyVerbose, on 30 July 2013 - 10:51 PM, said:

ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));


It appears to be this line. Is seems that getResource is returning null.


How can i go about fixing that? i got it from this website SWING LEARNING

If you recommend an even better URL to learn Swing then by all means please link me as it seems the one im studying from is a bit outdated.

Thank You

-Jonathan
Was This Post Helpful? 0
  • +
  • -

#4 ConciselyVerbose  Icon User is offline

  • D.I.C Regular

Reputation: 90
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: Why getting so many errors?

Posted 30 July 2013 - 10:20 PM

I'm sorry, but for an answer to why that method is not working as you intended, you're going to have to wait for someone else. I am not sure how that is intended to be used.
Was This Post Helpful? 0
  • +
  • -

#5 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 133
  • Joined: 29-June 13

Re: Why getting so many errors?

Posted 30 July 2013 - 10:44 PM

View PostConciselyVerbose, on 30 July 2013 - 11:20 PM, said:

I'm sorry, but for an answer to why that method is not working as you intended, you're going to have to wait for someone else. I am not sure how that is intended to be used.


Oh i see well no biggies thanks for pointing out the answer at least :D I was having issues finding what the cause to the problem was, thanks to you i know what it is :D Now to the real issue, is how i solve it!

Thank YOU!!!
-Jonathan
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1376
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: Why getting so many errors?

Posted 30 July 2013 - 11:04 PM

You should be familiar with NullPointerException before you start GUI programming ^^ Console applications are the best way of learning as a beginner

The docs say

Quote

public URL getResource(String name)

Returns:
A URL object or null if no resource with this name is found

Seems like it can't find the resource you are looking for. Your image is not in the correct directory
Try placing exit.png in the directory where your menuexample.class is located
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1