DefaultListModel() 'must not be null' error. help!

Hi, I'm new to all this and having a problem with a JList in a JDialog.

I'm trying to list the contents of an ArrayList in the JList and I think I have set this up correctly. However, I get the following error when I try to run the project;

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: model must be non null

at javax.swing.JList.setModel(JList.java:1211)

at oop2workagain.RecipeGUI.initComponents(RecipeGUI.java:83)

at oop2workagain.RecipeGUI.<init>(RecipeGUI.java:24)

at oop2workagain.RecipeGUI$5.run(RecipeGUI.java:289)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

The part I believe to be correct is as follows;

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

String textName;

textName = ingNameText.getText();

Ingredient.setName(textName);

String textQuantity;

textQuantity = ingQText.getText();

Ingredient.setQuantity(Double.parseDouble(textQuantity));

String textUnit;

textUnit = ingUnitText.getText();

Ingredient.setUnit(textUnit);

ingList.addElement(Ingredient.getName() + " : " + Ingredient.getQuantity() + " : " + Ingredient.getUnit());

ingNameText.setText("");

ingQText.setText("");

ingUnitText.setText("");

Any help with this is much appreciated!

[1868 byte] By [jed199a] at [2007-11-26 17:50:09]
# 1
Hmm, that is an odd exception.
Armadilloa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 2
What is on this line?at oop2workagain.RecipeGUI.initComponents(RecipeGUI.java:83)And, when you post code, make sure you use the code button above the posting box.
doremifasollatidoa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 3

> Hmm, that is an odd exception.

Not sure what you mean by that. But, since there are at least three people on the forum today who are working on Recipe GUIs, I'd assume you are all in the same class. Maybe you can work together in person, instead of asking random people on the internet (some of us may be more experienced and have better advice, but with three of you there, you might be able to figure it out between the three of you, and learn more in the process).

doremifasollatidoa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 4

> What is on this line?

> at

> oop2workagain.RecipeGUI.initComponents(RecipeGUI.ja

> va:83)

>

> And, when you post code, make sure you use the code

> button above the posting box.

ok, on that line I have;

jList2.setModel(ingList);

jed199a at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 5

> What is on this line?

> at

> oop2workagain.RecipeGUI.initComponents(RecipeGUI.ja

> va:83)

It must be something like this, from the stacktrace:

mylist.setModel(null);

or

mylist.setModel(mymodel);

where mylist is an instance of JList, and mymodel == null

Edit: The OP posted as I was posting this. Ok, so "ingList" is null then.

warnerjaa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 6

> ok, on that line I have;

>

> > jList2.setModel(ingList);

>

ingList is null. Did you say something like:

ingList = new DefaultListModel();

or, since JList will create its own if you don't provide one:

ingList = jList2.getModel();

(use whatever list model you need).

doremifasollatidoa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 7
I used;ingList = new DefaultListModel();
jed199a at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 8

> I used;

>

> > ingList = new DefaultListModel();

>

That may be true, but obviously from the stacktrace at the time you executed the line of code in question ingList was null. So you're doing things out of order I suppose.

warnerjaa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 9

> That may be true, but obviously from the stacktrace

> at the time you executed the line of code in question

> ingList was null. So you're doing things out

> of order I suppose.

Given the way he has his other variables declared, I'd guess he has the following in the constructor:

ListModel ingList; //Local variable!

ingList = new DefaultListModel();

So the button's actionPerformed method is accessing a still-uninitialized instance variable 'ingList'.

doremifasollatidoa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 10
Thanks for your help!
jed199a at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...
# 11
> Thanks for your help!So, is your problem fixed now?
doremifasollatidoa at 2007-7-9 5:02:39 > top of Java-index,Java Essentials,New To Java...