Null pointer when accessing a JComboBox
Hi,
I have this code here which asks the for the selectedindex of a combo box.
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 :( :
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 :s:
this is the rest of the code:
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
Re: Null pointer when accessing a JComboBox
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.
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:
Code:
private JComboBox SelectColor;
private JComboBox SelectWord;
or their assignments:
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
Re: Null pointer when accessing a JComboBox
You're shadowing the class fields by re-declaring the variables in the constructor.
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.