Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ComboBox NullPointerException

Reply
Thread Tools

ComboBox NullPointerException

 
 
newsguy
Guest
Posts: n/a
 
      04-18-2005
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.


 
Reply With Quote
 
 
 
 
newsguy
Guest
Posts: n/a
 
      04-18-2005
Never mind();

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

Thanks for your attention.


"newsguy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
 
 
 
Steve R. Burrus
Guest
Posts: n/a
 
      04-18-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>>snip<<



 
Reply With Quote
 
raven
Guest
Posts: n/a
 
      04-18-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox Project: How to put 4 text items in the combobox christopher.m.lusardi@gmail.com Java 5 10-11-2012 09:29 PM
LinkedList NullPointerException occurs after switched from IBM JVM 1.4.0 to 1.4.1 Tohru Kao Java 3 07-14-2003 08:12 AM
Re: java.lang.NullPointerException (Tomcat) Dhek Bhun Kho Java 0 07-08-2003 11:53 PM
LinkedList NullPointerException occurs after switched from IBM JVM 1.4.0 to 1.4.1 Tohru Kao Java 1 07-08-2003 09:09 AM
Re: NullPointerException Error?? Jon A. Cruz Java 0 07-06-2003 04:27 AM



Advertisments