-2

i seem to have a rather peculiar issue with my overloaded constructors

in the default constructor AutoFleetServicesMain()

i have

listModel = new DefaultListModel();
list = new JList(listModel);

and in the overloaded constructor AutoFleetServicesMain(int i)

i have

listModel.addElement(dbh.findAll());

according to my output, the default constructor is called before the overloaded one, so i see no reason why listModel is null

although if i add listModel = new DefaultListModel(); to the overloaded constructor, it will run without erroring but i still don't get a list?

Any ideas for what i could do?

adam2510
  • 563
  • 1
  • 7
  • 22

1 Answers1

2

It sounds like you want to chain your constructors, but that doesn't happen automatically. Try adding the following statement to the beginning of your AutoFleetServicesMain(int) constructor:

this();

This will call the default constructor before the subsequent logic. See this post for more examples of constructor chaining: How do I call one constructor from another in Java?

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • -_- stupid blonde moment, i should have called DefaultListModel listModel = new DefaultListModel(); in the global variables instead of DefaultListModel listModel; – adam2510 Sep 27 '12 at 01:32
  • i'm going to say you're correct on this :) but in my project i'm chaining it through another view, thanks for the help though :) – adam2510 Sep 27 '12 at 01:37