Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ComboBox NullPointerException

1 view
Skip to first unread message

newsguy

unread,
Apr 18, 2005, 12:42:50 PM4/18/05
to
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.


newsguy

unread,
Apr 18, 2005, 2:53:55 PM4/18/05
to
Never mind();

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

Thanks for your attention.


"newsguy" <wil...@pdfsystems.com> wrote in message
news:d40o2...@news4.newsguy.com...

Steve R. Burrus

unread,
Apr 18, 2005, 3:52:22 PM4/18/05
to
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.


"newsguy" <wil...@pdfsystems.com> wrote in message
news:d40o2...@news4.newsguy.com...

>>snip<<


raven

unread,
Apr 18, 2005, 4:01:25 PM4/18/05
to
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

0 new messages