This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.NullPointerException error" Watch "Exception in thread "main" java.lang.NullPointerException error" New topic
Author

Exception in thread "main" java.lang.NullPointerException error

Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
Hello,

I am getting the following error that I can not figure out, or do not have any idea where to begin. It compiles, but when I go to run it, I get this:
Car

null

null

null

null

null

Exception in thread "main" java.lang.NullPointerException
at Inventory.value(Inventory.java:28)
at Inventory.view(Inventory.java:37)
at Inventory6.<init>(Inventory6.java:35)
at Inventory6.main(Inventory6.java:180)


Here are the codes in question:



and:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So basically when you create your Inventory6, you add the SUVs, print them out, and they're null already?

I mean, are those the print statements you're showing us? Does it do the same thing if the call to sort() is removed?
Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
Yes, it is doing the same thing
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You might want to consider using more standard naming conventions for class properties, or at least their accessors.

Take a close look at Inventory.add(SUV p).
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(And why is there an add for Cars, and an add for SUVs?)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

When you create your Inventory instance, its products array contains all null values. In the add method you increase numCar, but in several of the loops you are looping over all elements, not just until numCars. As such, you will encounter null for those elements not yet assigned.

Either loop to numCars (exclusively), or use a List<Car> instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kevin Quarles
Greenhorn

Joined: Sep 07, 2009
Posts: 26
I apologize for wasting your time. I found what I did wrong, and it had nothing to do with the errors I spoke of above. The problem was in the other classes, and after sleeping on it, I immediately saw what I did wrong.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Um... the add(SUV) is most definitely wrong, and fixing it fixes the output.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception in thread "main" java.lang.NullPointerException error