wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes null pointer exception when populating JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "null pointer exception when populating JList" Watch "null pointer exception when populating JList" New topic
Author

null pointer exception when populating JList

J Chandler
Ranch Hand

Joined: May 17, 2003
Posts: 32
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
J Chandler
Ranch Hand

Joined: May 17, 2003
Posts: 32
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

Glad to help. This is one of those things everybody goes through once. Next time, you'll know!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null pointer exception when populating JList
 
Similar Threads
HashMap problem
Can I change the List's DLM dynamically?
I need help creating a custom ListModel
how to manage class relationship?
starting RMI...