-3

I am going through thenewboston's tutorials and I have an unexpected error. I have tried to do everything that Eclipse suggest, but can't figure it out where the problem is.

this is my Main Class

import javax.swing.JFrame;

class Main {
 public static void main(String args[]) {

     Gui go = new Gui();
     go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     go.setSize(300,200);
     go.setVisible(true);

 }
}

and this is GUI Class 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 JButton reg;
    private JButton custom;

    public Gui(){

        super("The title");
        setLayout(new FlowLayout());

        reg = new JButton("Regular Button");
        add(reg);

        Icon b = new ImageIcon(getClass().getResource("b.png"));
        Icon a = new ImageIcon(getClass().getResource("a.png"));
        custom = new JButton("Custom", b);
        custom.setRolloverIcon(a);
        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()));
        }
    }

}

Thanks brothers for helping me out!

enter image description here

enter image description here

enter image description here

Khaled.K
  • 5,828
  • 1
  • 33
  • 51
  • 2
    Can't really see any of your code. – Sdyess Feb 24 '15 at 10:24
  • 1
    Post your code of your Main class. – mlethys Feb 24 '15 at 10:25
  • what is the 23rd line of your GUI class? please share your code. – Syeda Zunaira Feb 24 '15 at 10:29
  • and don't worry on the warning on `serial...` this is not what causes your application to crash. Can you share the code of `Gui` class ? – T.Gounelle Feb 24 '15 at 10:31
  • 1
    The above text is a *warning*. You don't have to care about that one now. The text below is an *exception*. You have to care about that. You can start with some research: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Tom Feb 24 '15 at 10:40
  • It is very difficult to solve your problem using screenshots, post the code which made the error. – Anptk Feb 24 '15 at 10:54
  • @mlethys here you go, brother! – Vaclovas Rekašius Jr. Feb 24 '15 at 10:58
  • @anptk Here you go! Thanks for helping me out! – Vaclovas Rekašius Jr. Feb 24 '15 at 11:02
  • 1
    @Zoya: please note that inline code spans (`like this`) [should not be used for highlighting](http://meta.stackexchange.com/a/135113/220428), only for code within sentences. Also, please try and improve the post as much as possible when editing to save other people's time. See the [editing guidelines](http://stackoverflow.com/help/editing) for more information. Thanks! – Qantas 94 Heavy Feb 24 '15 at 11:32

1 Answers1

0

You've posted a couple of different stacktraces with the error on different line numbers but the code seems to have moved around. The error itself is talking about a NullPointerException in a constructor for ImageIcon. Not really anything to do with the JButton so the tags are misleading.

Basically you're looking up an image location of b.png and a.png. If these two files don't exist then you'll get a runtime exception like you have. The quick fix is to add these two images to the project so they are found.

A more robust solution would be to handle the exception and either output a more meaningful error or just carry on without the icon on the gui.

Ben Thurley
  • 6,943
  • 4
  • 31
  • 54