-1
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 Guiii extends JFrame{

  private JButton menu;
  private JButton custom;

  public Guiii(){
    super ("The Title");
    setLayout(new FlowLayout());

    menu = new JButton("menu Button");
    add (menu);

    Icon b = new ImageIcon (getClass().getResource("button.png"));
    Icon x = new ImageIcon (getClass().getResource("greenbutton.png"));
    custom = new JButton("Custom",b);
    custom.setRolloverIcon(x);
    add (custom);


    HandlerClass handler = new HandlerClass();
    menu.addActionListener(handler);
    custom.addActionListener(handler);

  }

  private class HandlerClass implements ActionListener{
    public void actionPerformed(ActionEvent event){

      JOptionPane.showMessageDialog(null, String.format("~s",     event.getActionCommand()));
    }   
  }
}

This is my code I am having some trouble and its not working, I have made a main for it but errors are showing can anyone help me and explain how the code will work khg

These are the errors

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at Guiii.<init>(Guiii.java:22)
    at main.main(main.java:7)

This is the rest of the code but it had to be in another file:

import javax.swing.JFrame;
public class main {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    Guiii o = new Guiii();
    o.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    o.setSize(300,200);
    o.setVisible(true);
  }
}
Grim
  • 1,938
  • 10
  • 56
  • 123
AHMER93
  • 35
  • 5
  • What are the errors?.. – Mathews Mathai Mar 07 '16 at 15:54
  • [link]Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:205) at Guiii.(Guiii.java:22) at main.main(main.java:7) – AHMER93 Mar 07 '16 at 15:55
  • This isn't the complete code.right? – Mathews Mathai Mar 07 '16 at 15:58
  • You can delete your comment with error since now it is part of question. – Pshemo Mar 07 '16 at 15:58
  • i have put the rest of the code in – AHMER93 Mar 07 '16 at 16:01
  • 2
    `Foo.` represents constructor of `Foo` class. So `Guiii.(Guiii.java:22)` means that problem is inside constructor of `Guiii` class at line 22. This line is `Icon b = new ImageIcon (getClass().getResource("button.png"));` and it is confirmed by `at javax.swing.ImageIcon.` which suggests that problem is inside `ImageIcon` constructor. I suspect that you passed `null` as argument which means that `getClass().getResource("button.png")` returned `null`. If that is the case then this means your `button.png` is not inside same location as your `Guiii.class` file. – Pshemo Mar 07 '16 at 16:04
  • 1
    To help you more we would need to know about your project structure. You should also probably read this question: http://stackoverflow.com/questions/9864267/load-icon-image-exception – Pshemo Mar 07 '16 at 16:06
  • do you know what the correct code would be – AHMER93 Mar 07 '16 at 16:16
  • Correct code depends on your project structure and how you want to organize it. – Pshemo Mar 07 '16 at 16:27

1 Answers1

1

The constructor of the icon throws a NPE because getResource returns null.

Place the images in the same folder as the class Guiii.

Grim
  • 1,938
  • 10
  • 56
  • 123