Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception when populating JList

 
J Chandler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've written some code to populate a JList.
The source data comes from a method called via the 'controller' in the form of an ArrayList of custom objects.

The steps I have taken are:-

Extended AbstactListModel with a new class (DateListModel) - containing a Vector to hold the objects I want on the JList.
Included overriding methods:- public int getSize() { .....} and
public Object getElementAt(int index){.....}
Passed my custom objects into the constuctor on instantiation and added them to the afforementioned Vector as String objects.
Called the fireContentsChanged(.....) method.


The 'main' class in which I am creating the JList, declares an object of type DateListModel (see above) - and instantiates it in the constructor:-

dlm = controller.getAvailDates(id); //problem here?

I passed the DateListModel instance to the JList:-

list = new JList(dlm);

When I run the program (which compiles ok) I always get a NullPointerException however I re-write the code. I've been over and over it and I'm going nuts here guys


I have done the TableModel thing, so I'm pretty sure I understand how it's supposed to work...

Anyone know where I'm going wrong?

J
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the stack trace from the NPE. If the method named first is one you wrote, then look at the line number mentioned in the stack trace -- somewhere on that line, a variable to the left of a "." is null. You might add a few println()s to check this, or set a debugger breakpoint and step through it.

If the top method on the list isn't yours, then look down until you see the first one that is. There will be a method call on that line; examine the arguments you're passing, as it's likely that one or more are null (or are containers with null contents of some kind.)

If you're still stuck, you can come back here and show us all the above data, and we can further advise you.
 
J Chandler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

you are a gent!

It's after 1am and I'm dog-tired but with your help I have solved the mystery.

somewhere on that line, a variable to the left of a "." is null.


ok, the controller object I was using to invoke the method that delivers the all important data was indeed null - it required that I pass the connection type on instantiation (which I had forgotten to do as I was working on a new class) and so I passed the original controller object to my new class instead.

:roll: Thanks again for the tip,
Regards, J
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help. This is one of those things everybody goes through once. Next time, you'll know!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic