• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException in every class :) - Making me crazy  RSS feed

 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a 'system' for a car rental company. When i compile the code i get this:

Exception in thread "main" java.lang.NullPointerException
at se.kth.ict.carrental.model.CarRegister.checkCD(CarRegister.java:23)
at se.kth.ict.carrental.model.CarDescription.checkWithRegister(CarDescription.java:28)
at se.kth.ict.carrental.controller.Controller.startRental(Controller.java:28)
at se.kth.ict.carrental.view.View.testRun(View.java:36)
at se.kth.ict.carrental.startup.Startup.main(Startup.java:19)

The thing is that everyone of these methods are connected but i cant seam to find out whats wrong.
What i want to do is to send a wish from the view, containing type and cost and get a boolean back to the view (true if the car described exists (and it exists as i put it there when the method reaches the CarRegister.)


Here are the codes in the different classes In Order:

Startup Nullpointer on row 03


View Nullpointer on row 07


Controller Nullpointer on row 03


CarDescription Nullpointer on row 03


CarRegister Nullpointer on row 03


Thanks alot

/ Johannes


 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the line of code throwing the exception?

If so then you have an instance variable named "carRegister" but it contains null. You haven't ever assigned a value to it.
 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Is this the line of code throwing the exception?

If so then you have an instance variable named "carRegister" but it contains null. You haven't ever assigned a value to it.


carRegister is a LinkedList of carDescription objects, and by the line:



i add the first element and then check the index of the first element, and then returns the carDescription
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johannes Thorén wrote:carRegister is a LinkedList of carDescription objects, and by the line:



i add the first element and then check the index of the first element, and then returns the carDescription


Except that you don't. When that line of code is executed, the carRegister variable is null (you can tell this because the NullPointerException is thrown). And therefore you don't add any element and so on. I suggest that you review your code and find the place where you declare that variable but fail to assign it a value.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you get a NullPointerException, there are two possibilities:
1) your code itself throws the exception. Go to the line number shown in the stack trace; you already did that. A NullPointerException is thrown if the left hand side of any . or [ (for array access) is null. No other options. In your case, the line is "carRegister.add(cd);" which 100% guaranteed means that carRegister must be null. There is no other possibility.

2) the core API or a used library throws the exception. Either there is a bug in the library or you passed a null reference where you shouldn't have. Go to the first line of the stack trace that marks your own code and check if any of the arguments to the method is null.
 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for the help, the code is working now. :-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!