ComboBox NullPointerException

N

newsguy

Hi All;

I'm having problems seting up a new Combo box model. My Box Model...

stateBoxModel = new DefaultComboBoxModel(new String[]{state1,state2});
int size;
size = stateBoxModel.getSize();
System.out.println("Size is " + size);
....
Which prints out 2 and I'm sure the state strings have something in them.

But when I use the model...

stateComboBox.setModel(stateBoxModel);
....
I get:

java.lang.NullPointerException
at javax.swing.JComboBox.setModel(JComboBox.java:289)
at Desktop_9.initComponents(Desktop_9.java:389) //this is the
stateComboBox.setModel(stateBoxModel);
at Desktop_9.<init>(Desktop_9.java:90) // initComponents();
at Desktop_9.main(Desktop_9.java:2596)// new Desktop_9().show();
Exception in thread "main"

So I'm not sure why the model is null

Thanks for your time.
 
N

newsguy

Never mind();

Totaly stupid I was initComponants(); before getting the string values and
making the model.

Thanks for your attention.
 
S

Steve R. Burrus

Glad/happy to hear that u corrected your own problem, but I was going to
suggest that your line of code : >>"stateBoxModel = new
DefaultComboBoxModel(new String[]{state1,state2});"<< be rewritten to be :
"DefaultComboBoxModel stateBoxModel = new DefaultComboBoxModel(new
String[]{state1,state2});". Just a guess, and I could be wrong about that.


 
R

raven

NullPointerException appears when you use null as a value. You must
have a problem with the content of your model. I tried a very simple
code, and it worked. TRy it and adapt it to your situation if you
need. Just to show the model is fine, you have a mistake within
somewhere else regarding the values you use in your model.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Combo extends JFrame{
DefaultComboBoxModel dcm;
String state1=new String("bla");
String state2=new String("blah");
JComboBox jc;
public Combo() {
super("Combo");
dcm=new DefaultComboBoxModel(new String[]{state1,state2});
System.out.println(dcm.getSize());
jc=new JComboBox();
jc.setModel(dcm);
add(jc);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}

public static void main(String[] a) {
new Combo();}}

REgards

http://www.DevPlug.com --Connecting Developers
Posted from: http://www.devplug.com/ftopic22997.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top