Results 1 to 5 of 5
- 10-30-2011, 01:26 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Null pointer when accessing a JComboBox
Hi,
I have this code here which asks the for the selectedindex of a combo box.
However, when getSelectedIndex runs it does not return anything but a null pointer that looks like this :( :Java Code:action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()], action.getColors()[SelectColor.getSelectedIndex()]);
How do I fix this? Those are some of the scariest errors Ive ever seenJava 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)

this is the rest of the code:
Thanks in advance to anyone who can help out :DJava 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()]); } } } }
- 10-30-2011, 02:01 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Re: Null pointer when accessing a JComboBox
The NullPointerException means that something is null in that line but you are treating it as if it referenced an actual thing.Java Code:action.ChangeColor(manager.getWords()[SelectWord.getSelectedIndex()], action.getColors()[SelectColor.getSelectedIndex()]);
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 ;(
- 10-30-2011, 02:51 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
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:
or their assignments:Java Code:private JComboBox SelectColor; private JComboBox SelectWord;
I cant say what I'm missingJava 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);
-
Re: Null pointer when accessing a JComboBox
You're shadowing the class fields by re-declaring the variables in the constructor.
- 10-30-2011, 03:07 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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
-
Null pointer exception
By samuel.roshni in forum Java ServletReplies: 14Last Post: 01-22-2011, 02:25 PM -
Null Pointer
By theen3my in forum AWT / SwingReplies: 3Last Post: 10-03-2009, 02:10 PM -
Null pointer exception?
By coffee in forum New To JavaReplies: 4Last Post: 08-03-2009, 03:22 AM -
Help with null pointer exception
By gammaman in forum New To JavaReplies: 4Last Post: 07-14-2009, 12:23 AM -
null pointer help
By mayhewj7 in forum New To JavaReplies: 5Last Post: 02-17-2009, 11:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks