Results 1 to 3 of 3
  1. #1
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default Not sure what my problem is.....

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at PracticeSpace.Entity.<init>(Entity.java:24)
    at PracticeSpace.Practice.main(Practice.java:8)

    This is the problem I get, basically it has a problem with the picture being used. Here is a like the the tutorial I was following to right this code if you are interested, just with diff names for my classes.
    The New Boston Java Programming Tutorial 63 JButton Final Program
    It is the second of a two parter.

    I simply copy/pasted the name of the pngs I am using so I am sure the spelling is right and I have rechecked it many times because that is all I can think of. Help appreciated.

    Java Code:
    package PracticeSpace;
    
    import javax.swing.JFrame;
    
    public class Practice {
    	public static void main(String[] args){
    		
    		Entity go = new Entity();
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		go.setSize(800,600);
    		go.setVisible(true);
    		
    	}
    }
    Java Code:
    package PracticeSpace;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.Icon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
    public class Entity extends JFrame {
    	
    	private JButton reg;
    	private JButton custom;
    	
    	public Entity(){
    		super("The title");
    		setLayout(new FlowLayout());
    		
    		reg = new JButton("Regular button");
    		add(reg);
    		
    		Icon b = new ImageIcon(getClass().getResource("40px-Japanese_Barn-icon.png"));
    		Icon x = new ImageIcon(getClass().getResource("40px-Terra_globe_icon_light.png"));
    		custom = new JButton("Custom", 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()));
    		}
    	}
    
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    On line 24 of your Entity class something is null. Chances are it cannot find your image files. One solution is to use absolute paths rather than relative ones.

  3. #3
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    That did work. Thank you. But I just don't understand why it didn't work the way I had it though, especially when it worked for the guy making the video tutorial and several others. :(

    For anyone who might view this thread this is all I changed in my code.
    Java Code:
    		Icon b = new ImageIcon("C:\\Users\\MyUser\\Desktop\\practice\\40px-Japanese_Barn-icon.png");
    		Icon x = new ImageIcon("C:\\Users\\MyUser\\Desktop\\practice\\40px-Terra_globe_icon_light.png");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •