ComboBox NullPointerException

Discussion in 'Java' started by newsguy, Apr 18, 2005.

  1. newsguy

    newsguy Guest

    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, Apr 18, 2005
    #1
    1. Advertising

  2. newsguy

    newsguy Guest

    Never mind();

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

    Thanks for your attention.


    "newsguy" <> wrote in message
    news:...
    > 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, Apr 18, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    >>snip<<
     
    Steve R. Burrus, Apr 18, 2005
    #3
  4. newsguy

    raven Guest

    DefaultComboBoxModel NullPointerException

    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
     
    raven, Apr 18, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon A. Cruz

    Re: NullPointerException Error??

    Jon A. Cruz, Jul 6, 2003, in forum: Java
    Replies:
    0
    Views:
    523
    Jon A. Cruz
    Jul 6, 2003
  2. Tohru Kao
    Replies:
    3
    Views:
    438
    Neil Masson
    Jul 14, 2003
  3. Tohru Kao
    Replies:
    1
    Views:
    392
    Chris
    Jul 8, 2003
  4. Dhek Bhun Kho
    Replies:
    0
    Views:
    2,238
    Dhek Bhun Kho
    Jul 9, 2003
  5. Replies:
    5
    Views:
    483
    Roedy Green
    Oct 11, 2012
Loading...

Share This Page