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: Code doesn't work

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Code doesn't work

    I can't see why my code aren't working. (I'm still learning Java)
    Please help me.


    The Error / Console:
    Exception in thread "main" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(Unknown Source)
    	at GUI_JButton.Gui.<init>(Gui.java:28)
    	at GUI_JButton.Main.main(Main.java:9)

    The main Class:
    	public static void main(String[] args) {
     
    		Gui go = new Gui();
    		//This is need to close the window.
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		go.setSize(300,200);
    		go.setVisible(true);
     
    	}

    The Gui-Class:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
     
    @SuppressWarnings("serial")
    public class Gui extends JFrame{
     
    	private JButton reg;
    	private JButton custom;
     
    	public Gui(){
    		//Super is how you call methods form the SUPER-ClASS. It is also how you creat a title.
    		super("The Title");
    		setLayout(new FlowLayout());	//This is the default layout
     
    		reg = new JButton("reg Button");
    		add(reg);
     
    		//This is how to import photos/icons to the buttons.
    		Icon b = new ImageIcon(getClass().getResource("b.png"));
    		Icon x = new ImageIcon(getClass().getResource("x.png"));
     
    		//This is the cusotm button.
    		custom = new JButton("Custom", b);
    		custom.setRolloverIcon(x);
    		add(custom);
     
    		HandlerClass handler = new HandlerClass();
    		reg.addActionListener(handler);
    		custom.addActionListener(handler);
     
    	}
     
    	//When you implement a class, then you have to override all the methodes - Lucky this only has one.
    	private class HandlerClass implements ActionListener{
     
    		public void actionPerformed(ActionEvent e) {
    			JOptionPane.showMessageDialog(null, String.format("%s", e.getActionCommand()));
    		}
    	}
     
    }


    NOTE: I' using a png-file called "b.png" and one called "x.png" - They are both in my "src" / scouce folder - Thanks for your time!


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    447
    Thanks
    0
    Thanked 87 Times in 72 Posts

    Default Re: Code doesn't work

    What is the problem? Are any exceptions thrown or anything?

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code doesn't work

    My "program" won't run. It just shows an error.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,721
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Code doesn't work

    Post the error, the whole thing.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code doesn't work

    It is the whole error... and the whole code. Just look under: "The Error / Console"

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,721
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Code doesn't work

    There is a null value in line 28 of the Gui class. I suspect that either or both of the files b.png and x.png are not being found. Depending on your environment setup, you may need to specify the whole path or the relative location from the source, as in "./b.png".

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code doesn't work

    I tried to do this at those lines, but it didn't work.

    Icon b = new ImageIcon(getClass().getResource("C:\Users\Nicklas \Dropbox\Eclipse\WorkSpace Done\Code Reminder\src\b.png"));
    Icon x = new ImageIcon(getClass().getResource("C:\Users\Nicklas \Dropbox\Eclipse\WorkSpace Done\Code Reminder\src\x.png"));

    The console / Error says:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

    at GUI_JButton.Gui.<init>(Gui.java:28)
    at GUI_JButton.Main.main(Main.java:9)



    So i know that I did something wrong, but what? How should my code look?

Similar Threads

  1. Code doesn't work despite being from book
    By Math2 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 16th, 2013, 08:24 PM
  2. actionListner code doesn't work ????
    By sciences in forum AWT / Java Swing
    Replies: 4
    Last Post: February 27th, 2012, 05:46 AM
  3. Code is working in Eclipse but when exported to Runnable Jar doesn't work
    By jjain.jitendra@gmail.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 24th, 2011, 07:12 AM
  4. File Reading code doesn't work
    By koryvandell in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2011, 08:40 AM
  5. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM

Tags for this Thread