Results 1 to 13 of 13
- 10-05-2008, 05:32 AM #1
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
I'm getting this error whenever I "submit" my chosen item from a JComboBox.. what is this?? please help..
Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Matchess.myTurn(Matchess.java:66) at Matchsticks.actionPerformed(Matchsticks.java:119) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6041) at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) at java.awt.Component.processEvent(Component.java:5806) at java.awt.Container.processEvent(Container.java:2058) at java.awt.Component.dispatchEventImpl(Component.java:4413) at java.awt.Container.dispatchEventImpl(Container.java:2116) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) at java.awt.Container.dispatchEventImpl(Container.java:2102) at java.awt.Window.dispatchEventImpl(Window.java:2440) at java.awt.Component.dispatchEvent(Component.java:4243) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
-
You have a bug in that you are trying to call a method on a null object. Why this is, I haven't the foggiest without seeing code. Consider creating an SSCCE and posting it. Please see the link to find out how to do this.
- 10-05-2008, 05:54 AM #3
ok, here's my "parent" class
here is the Matchess classJava Code:public class Matchsticks extends JFrame implements ActionListener, ItemListener{ JComboBox choice = new JComboBox(); private JButton accept = new JButton("Accept"); private int selected = 1; private int count = 0; private Matchess matches = new Matchess(); Boardd board = new Boardd(matches); JTextField textField = new JTextField(50); //...more components public Matchsticks() { super("Matchsticks"); choice.addItem("1"); choice.addItem("2"); choice.addItem("3"); choice.addItemListener(this); submit.addActionListener(this); //more panel stuff... } public int getSelected() { return this.selected; } public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { int result = matches.getCount(); if (result > 1) { this.selected = e.getStateChange()+ 1; if ((result - selected) < 1) { textField.setText("Wrong choice! "); } else { textField.setText("You picked " + selected); } } } } public void actionPerformed(ActionEvent e) { if (e.getSource()==start) { //...there's supposed to have an existing start button } if (e.getSource()==accept) { boolean edit = counter.isEditable(); if (edit) { //...same with start button } if (e.getSource()==submit) { int newcount = matches.getCount(); if (newcount > 1) { if ((newcount - selected) < 1) { if (newcount == 3) { textField.setText("Take 2 !!!"); } else { textField.setText("Take 1 !!!"); } } else { newcount -= selected; matches.setCount(newcount); board.repaint(); counter.setText((String.valueOf(newcount).toString())); matches.myTurn(); //this is what the error "Matchess.myTurn(Matchess.java:66)" is pointing at } } } } public static void main(String[] args) { //main goes here... } }
there's another class named Boardd but I think that won't be needed here..Java Code://import stuff... public class Matchess { private int count; private Matchsticks parent; public Matchess() { } public void setCount(int value) { this.count = value; } public int getCount() { return this.count; } public int getTurn(int old, int choosen) { int getback; switch((old+choosen)%4) { case 0: if (choosen == 1) { getback = 2; } else { getback = 1; } break; case 1: getback = 4 - choosen; break; case 2: if (choosen == 2) { getback = 3; } else { getback = 1; } break; default: if (choosen == 1) { getback = 1; } else { getback = 3; } break; } return getback; } public void myTurn() { int turn; if (count == 1) { parent.textField.setText("You WIN !!!"); //suppose a textfield is existing } else { switch(count%4) { case 1: turn = (int)(Math.random()*3+1); break; default: turn = getTurn(count, parent.getSelected()); break; } count = count - turn; parent.board.repaint(); if (count == 1) { parent.textField.setText("I took " + turn + " and you LOSE !!!"); } else { parent.textField.setText("Ok...i took " + turn + ", and you ?"); } parent.counter.setText((String.valueOf(count).toString())); } } }
I hope I got the SSCCE right...Last edited by iuna; 10-05-2008 at 05:58 AM.
-
not quite. Again, please read the link and it will explain it. It doesn't mean just posting your code. Instead it's an active process where you isolate the problem and nothing but the problem and then post a small self-contained compilable program that demonstrates the problem. Many have said that if you can't create this, you don't understand the problem. I say that if you ask a volunteer to help you, you should be expected to put in the effort to make the volunteer's work as easy as possible.I hope I got the SSCCE right..
so, if you still need help, then please post the SSCCE. If not, then no biggie. Again, good luck.
- 10-05-2008, 06:22 AM #5
I think I got it..
I tried "compressing" my code but it still looked long.. I really tried my best isolating the problem.. And this is compilable.. well here it is..
my "parent" class Matchsticks
"subclass" MatchessJava Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Matchsticks extends JFrame implements ActionListener, ItemListener{ JComboBox choice = new JComboBox(); JButton submit = new JButton("Submit"); JPanel panel = new JPanel(); int selected = 1; Matchess matches = new Matchess(); public Matchsticks() { super("Matchsticks"); choice.addItem("1"); choice.addItem("2"); choice.addItem("3"); panel.add(choice); panel.add(submit); getContentPane().add(panel, BorderLayout.CENTER); choice.addItemListener(this); submit.addActionListener(this); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } public int getSelected() { return this.selected; } public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { int result = matches.getCount(); if (result > 1) { selected = choice.getSelectedIndex() + 1; } } public static void main(String[] args){ Matchsticks m = new Matchsticks(); m.setSize(200,100); m.setVisible(true); m.setResizable(false); } } public void actionPerformed(ActionEvent e) { if (e.getSource()==submit) { int newcount = matches.getCount(); newcount -= selected; matches.setCount(newcount); matches.myTurn(); } } }
Java Code:public class Matchess { private int count; private Matchsticks parent; public Matchess() { } public void setCount(int value) { this.count = value; } public int getCount() { return this.count; } public int getTurn(int old, int choosen) { int getback; switch((old+choosen)%4) { case 0: if (choosen == 1) { getback = 2; } else { getback = 1; } break; case 1: getback = 4 - choosen; break; case 2: if (choosen == 2) { getback = 3; } else { getback = 1; } break; default: if (choosen == 1) { getback = 1; } else { getback = 3; } break; } return getback; } public void myTurn() { int turn; switch(count%4) { case 1: turn = (int)(Math.random()*3+1); break; default: turn = getTurn(count, parent.getSelected()); break; } count = count - turn; } }Last edited by iuna; 10-05-2008 at 06:36 AM.
-
By the way, that "parent" Matchstick variable that is in Matchess,... where in your code do you let it refer to a viable object? What line is line 66 of the Matchess class? The one that refers to parent?
- 10-05-2008, 06:27 AM #7
Java Code:public void actionPerformed(ActionEvent e) { if (e.getSource()==submit) { int newcount = matches.getCount(); newcount -= selected; matches.setCount(newcount); matches.myTurn(); //this is that line 66 the error is pointing at } }
-
And as for your SSCCE: it compiles, but doesn't run and thus can't reproduce your error. If my comment above above that "orphan" parent variable doesn't help, you'll need to put more work into a true SSCCE. Again, read the link, it will explain a lot.
And again, where do you set parent = to anything?
-
Look into passing a reference to the Matchsticks object to the Matchess object via the Matchess's constructor. This will make sure that parent is referring to the correct object and is non-Null. Something like so:
in Matchess.java
in Matchsticks.javaJava Code:public Matchess(Matchsticks parent) //!! add parameter to constructor { this.parent = parent; }
Java Code:public class Matchsticks extends JFrame implements ActionListener, ItemListener { JComboBox choice = new JComboBox(); //... Matchess matches; // = new Matchess(); //!! see change public Matchsticks() { super("Matchsticks"); //.... matches = new Matchess(this); //!! add this }
- 10-05-2008, 06:39 AM #10
oh, sorry i forgot my main method..
I have edited my "SSCCE" above..
yes.. because when I tried this:And again, where do you set parent = to anything?
private Matchsticks parent = new Matchsticks();
i got a StackOverflow error..
-
will cause a recursive headache. Your new Matchsticks object (which by the way will have no relationship to the one showing on the screen), will create a new Matchess object which will create a new Matchsticks object,...Java Code:private Matchsticks parent = new Matchsticks();
do what I suggested above. I am 99.9% certain that this is your problem and your solution.
- 10-05-2008, 06:45 AM #12
yeah, it worked! thank you so much!
-
Similar Threads
-
Exception in thread "main" java.lang,NullPointerException
By ljk8950 in forum AWT / SwingReplies: 15Last Post: 10-12-2010, 05:51 PM -
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
By satishkumar_lskin in forum AWT / SwingReplies: 2Last Post: 12-14-2009, 01:46 AM -
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-29-2008, 01:37 AM -
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:04 PM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks

Bookmarks