0

I have been trying for quite a while now and i cant figure out what the problem is. Im using actionlisteners on my JmenuItems but when i start the program and click the button i get errors which i cant understand why? Any help is appreciated.

code for the button im using:

final JMenuItem Start = new JMenuItem("Start");
    Start.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
         if(e.getSource() == Start) {
            pane.GuessBox.setEditable(true);

         }

        }

    });

The errors im recieving:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Main$1.actionPerformed(Main.java:37)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

GuessBox:

JTextField GuessBox = new JTextField(1);

    GuessBox.setEditable(false);
    GuessBox.addActionListener(new Command());
    GuessPanel.add(GuessBox);
Sam
  • 7,252
  • 16
  • 46
  • 65
User999
  • 21
  • 5
  • 1
    which is line no 37 in Main.java? – Aniket Kulkarni Oct 14 '13 at 14:45
  • Pane.Guessbox.setEditable(true); – User999 Oct 14 '13 at 14:46
  • 1
    What is `pane`? What is `pane.GuessBox`? I can assure you that one of them is **null** – Simon Forsberg Oct 14 '13 at 14:47
  • I think pane is null. – Aniket Kulkarni Oct 14 '13 at 14:50
  • Im using Pane which is another class file and grabbing the guessbox textfield from it. – User999 Oct 14 '13 at 14:52
  • Have you created object of `Pane pane = new Pane();` or just used `Pane pane;` – Aniket Kulkarni Oct 14 '13 at 14:53
  • i did, Pane pane = new Pane(); – User999 Oct 14 '13 at 14:54
  • 2
    Please read about [What is a NullPointerException?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception/218390#218390) – Simon Forsberg Oct 14 '13 at 14:55
  • `Follow Java naming conventions`. Variable names should NOT start with an upper case character! "Start" should be "start"... – camickr Oct 14 '13 at 15:22
  • Quit guessing. Which variable is null, "pane" or "guessBox"? All you need to do us use System.out.println(...) to print both variable to determine which is null. Then you solve your problem. We can't help because we don't have your code. Just make sure you haven't defined your variable twice, once as an instance variable and once as a local variable. – camickr Oct 14 '13 at 15:25
  • @camickr - `Start`, is set final. So it should really be `START` instead. – Rudi Kershaw Oct 14 '13 at 15:50
  • @RudiKershaw, didn't notice the "final". But there is no need for it to be final. It was only done that way because the ActionListener was not implement properly. There is no need to check if the button is the start button because an anonymous inner class is being used. So I would suggest the ActionListener code should be changed. – camickr Oct 14 '13 at 19:01
  • I found the problem i had two of the same variables one instantiated and one who wasn't instantiated, and the program was referring to the one not instantiated so the problem is fixed. – User999 Oct 14 '13 at 19:17
  • @RudiKershaw All capitalized letters should only be used for **static final** (what we in Java could call "constants"). The JMenuItem here is not a static final and should therefore be called `start` according to the naming conventions. – Simon Forsberg Oct 15 '13 at 11:50
  • @SimonAndréForsberg - Do you have a source for that? Although I can't find any specifications in the official Oracle/JavaBeans docs, using caps for non-static finals seems in wide use. – Rudi Kershaw Oct 15 '13 at 12:12
  • @RudiKershaw [The official Oracle code conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367) and [Wikipedia](http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java) :) – Simon Forsberg Oct 15 '13 at 12:20
  • @SimonAndréForsberg - My bad, I seem to miss the obvious fairly regularly. I do wonder though what they meant by "class constants and of ANSI constants". Comparing ANSI constants to class constants implies that ANSI might not be 'class' constants. – Rudi Kershaw Oct 15 '13 at 12:28

1 Answers1

3

It looks like your problem is probably here.

pane.GuessBox.setEditable(true);

If GuessBox (or pane) is not instantiated (or set null), then setEditable can not be called on null.

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77