Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

null pointer exception when populating JList

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to help. This is one of those things everybody goes through once. Next time, you'll know!
 
Pay attention! Tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic