Results 1 to 5 of 5
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Null pointer when accessing a JComboBox

    Hi,
    I have this code here which asks the for the selectedindex of a combo box.

    Java Code:
    action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()],
    						action.getColors()[SelectColor.getSelectedIndex()]);
    However, when getSelectedIndex runs it does not return anything but a null pointer that looks like this :( :
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at AlgorithmAnalyzer.GUI$SelectListener.actionPerformed(GUI.java:60)
    	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.plaf.basic.BasicButtonListener.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$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$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)
    How do I fix this? Those are some of the scariest errors Ive ever seen


    this is the rest of the code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class GUI extends JFrame {
    	
    	private JComboBox SelectColor;
    	private JComboBox SelectWord;
    	private JButton Button;
    	private WordManager manager;
    	
    	Actions action = new Actions();
    	
    	public GUI (WordManager manager){
    		
    		super ("Algorithm Analyzer 0.1");
    		this.setLayout(new FlowLayout());
    		
    		this.manager = manager;
    		
    		for (int i = 0; i < manager.getWords().length; i++){
    			
    			this.add(manager.getWords()[i].getLabel());
    		}
    		
    		SelectListener handler = new SelectListener ();
    		
    		final JComboBox SelectWord = new JComboBox(manager.getWordNames());
    		SelectWord.setSelectedIndex(0);
    		SelectWord.addActionListener(handler);
    		this.add(SelectWord);
    		
    		
    		final JComboBox SelectColor = new JComboBox(action.getColorNames());
    		SelectColor.setSelectedIndex(0);
    		SelectColor.addActionListener(handler);
    		this.add(SelectColor);
    		
    		JButton Button = new JButton ("Submit");
    		Button.addActionListener(handler);
    		this.add(Button);
    			
    		}
    	
    	
    	private class SelectListener implements  ActionListener  {
    		
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
    			if (e.getActionCommand() == "Submit"){
    				
    				action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()],
    						action.getColors()[SelectColor.getSelectedIndex()]);
    
    				
    			}
    			
    		}
    	}
    
    }
    Thanks in advance to anyone who can help out :D

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Null pointer when accessing a JComboBox

    Java Code:
    action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()],
            action.getColors()[SelectColor.getSelectedIndex()]);
    The NullPointerException means that something is null in that line but you are treating it as if it referenced an actual thing.

    So, use System.out.println() to display the possible culprits: action, manager, manager.getWords(), SelectWord, action.getColors(), and SelectColor. (Also make sure you see why you can't use . or [] with something that is null). Once you have found which variable is causing the problem, go back through your code to the place you thought you were assigning it a nonnull value and figure out why that didn't happen.

    -----

    Follow Java coding standards: variables start with a lowercase letter, like selectColor. Personally I would use colorCB because that says what it is (a colour combo box), but the lowercase starting letter is important. Also action (not actions?) should be private unless you have a very good reason for it not to be.
    Last edited by pbrockway2; 10-30-2011 at 02:04 AM. Reason: too many typos ;(

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Re: Null pointer when accessing a JComboBox

    I followed your instructions and pinpointed the null pointer to my two ComboBoxes. However I can't see anything wrong with their declarations here:

    Java Code:
    private JComboBox SelectColor;
    	private JComboBox SelectWord;
    or their assignments:

    Java Code:
    JComboBox SelectWord = new JComboBox(manager.getWordNames());
    		SelectWord.setSelectedIndex(0);
    		SelectWord.addActionListener(handler);
    		this.add(SelectWord);
    		
    		
    		JComboBox SelectColor = new JComboBox(action.getColorNames());
    		SelectColor.setSelectedIndex(0);
    		SelectColor.addActionListener(handler);
    		this.add(SelectColor);
    I cant say what I'm missing

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

    Default Re: Null pointer when accessing a JComboBox

    You're shadowing the class fields by re-declaring the variables in the constructor.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Null pointer when accessing a JComboBox

    Yes, it's the shadowing that means the (intended) assignment didn't happen: you assigned to something else. If you haven't seen this before there is a brief discussion in Wikipedia, or google for more.

Similar Threads

  1. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 02:25 PM
  2. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  3. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  4. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  5. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM

Posting Permissions

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