Results 1 to 13 of 13
  1. #1
    iuna's Avatar
    iuna is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default 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)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    iuna's Avatar
    iuna is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default

    ok, here's my "parent" class

    Java 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...
      	}
    }
    here is the Matchess class

    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()));
      	}
      }	
    }
    there's another class named Boardd but I think that won't be needed here..

    I hope I got the SSCCE right...
    Last edited by iuna; 10-05-2008 at 06:58 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I hope I got the SSCCE right..
    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.

    so, if you still need help, then please post the SSCCE. If not, then no biggie. Again, good luck.

  5. #5
    iuna's Avatar
    iuna is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default 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

    Java 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();
                    }
                }
    }
    "subclass" Matchess

    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 07:36 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  7. #7
    iuna's Avatar
    iuna is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default

    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
                    }
                }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    Java Code:
      public Matchess(Matchsticks parent) //!! add parameter to constructor
      {
        this.parent = parent;
      }
    in Matchsticks.java
    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. #10
    iuna's Avatar
    iuna is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default

    oh, sorry i forgot my main method..

    I have edited my "SSCCE" above..

    And again, where do you set parent = to anything?
    yes.. because when I tried this:

    private Matchsticks parent = new Matchsticks();

    i got a StackOverflow error..

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    private Matchsticks parent = new Matchsticks();
    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,...

    do what I suggested above. I am 99.9% certain that this is your problem and your solution.

  12. #12
    iuna's Avatar
    iuna is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default

    yeah, it worked! thank you so much!

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome. I hope you understand what was wrong and how it was fixed. Please ask if you don't. Good luck in your studies.

Similar Threads

  1. Replies: 15
    Last Post: 10-12-2010, 06:51 PM
  2. Replies: 2
    Last Post: 12-14-2009, 02:46 AM
  3. Replies: 3
    Last Post: 01-29-2008, 02:37 AM
  4. Replies: 1
    Last Post: 08-06-2007, 07:04 PM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •