I followed a tutorial and built my own little program but get this message:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Gui.<init>(Gui.java:27)
at appls.main(appls.java:6)

What to do? Here is my code btw

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;

public class Gui extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1;
	private JButton reg;
	private JButton custom;
	
	public Gui()
	{
		super("The Title");
		setLayout(new FlowLayout());
		reg = new JButton("Regular ol' button");
		add(reg);
		
		Icon b = new ImageIcon(this.getClass().getResource("marioc.png"));
		Icon x = new ImageIcon(this.getClass().getResource("mario jump.png"));
		
		custom = new JButton("Custom Button", b);
		custom.setRolloverIcon(x);
		add(custom);
		
		HandlerClass handler = new HandlerClass();
		reg.addActionListener(handler);
		custom.addActionListener(handler);
	}
	
	private class HandlerClass implements ActionListener{
		public void actionPerformed(ActionEvent event){
			JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
						
		}

		
	}

}

Recommended Answers

All 3 Replies

At first glance it seem that this statement:

this.getClass().getResource("marioc.png");

returns null.

well what can I do?

At first glance it seem that this statement:

this.getClass().getResource("marioc.png");

returns null.

Huzzah,

I solved it by rightcliking the picture in my jdk and clicking validate

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.