my dog learned polymorphism
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.NullPointerException" Watch "Exception in thread "main" java.lang.NullPointerException" New topic

Exception in thread "main" java.lang.NullPointerException

Zack Brown

Joined: Mar 18, 2012
Posts: 2
I have the following Viewer class:

and ViewerTest class:

Both compile. However, when I run the ViewerTest, I get the following errors:

Exception in thread "main" java.lang.NullPointerException
at Viewer.toString(
at ViewerTest.main(

Does anyone have any ideas? Thanks.
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

If the line numbers in your post match the line numbers in your code, then as the error message tells you, the error on line 88 is here:

So it must be that movieCode is null, since that's the only thing you're dereferencing in that line.
Zack Brown

Joined: Mar 18, 2012
Posts: 2
I changed the Viewer class code to this:

when I run the following test code:

I get " Jeff Brown DR $336.6" when I should be getting "Jeff Brown DR $5.24" I have checked the calculations in the switch statement and if/else statement and cannot see where the problem lies. Does anyone see what's wrong?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I don't understand why you're multiplying a cost number by a char value extracted from the movie code.
I agree. Here's the link:
subject: Exception in thread "main" java.lang.NullPointerException
It's not a secret anymore!