• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception - ArrayList  RSS feed

 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.... 2 posts in a row feel like I'm taking advantage here. oh well, if helps available...
When I create an instance of Car through the RentalCompany class I get a null pointer exception (ln 14 I think). Please can you give me a hand you lovely people.




Thank You
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at your other post, too, but I don't see the code for your Car class. Do you have a Car class with a constructor that takes a String and two integers?
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the stack trace to locate where NPE is being thrown exactly.
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Hussein I am using BlueJ which is what the university use to teach with and i don't know how to check the stack with it or is there just a generic way to do it.
The NPE happens when i try to put the newCar into the carList array

@Jk Robbins Yes it does take those types as argumentsI have included the constructor for the Car class as follows



thanks
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Launch your shell/terminal, run your Java program from there and see where is the faulty line. Even better, write a unit test.
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i have taken bits of code out when i posted just to make it easier and i didn't realise what you meant. its line 15
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
carList is null.
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bear but how to i solve that?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gino, when you run your program in BlueJ, you should see output at the bottom of the Run window. When an exception occurs, it should print the name of the class and the line number where the exception happened.

Gino Giuliani wrote:Thank you Bear but how to i solve that?

It will be easier to help you if you show us the whole file you are attempting to run.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not Bear but ...
By figuring out why carList is null in the first place.
You can employ many things like print statements at specific locations, writing unit tests or using Java debugger to see what is really going on.
 
Mori Gad
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Perhaps you meant this to be a constructor for the RentalCompany class? If so, remove void. I am assuming from the error you already have it as a global field for the class.

That way, when you do

you actually set carList as a new ArrayList.
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Curly I wrote most this today as it is due in friday and i have another assignment to do (true student style) and hadn't even noticed that the constructor was wonky. I've removed that line so the contructor is now empty but still the same problem occurs

going to try further testing to see where exactly the problem occurs

Here's the full code so I am putting myself up for scrutiny
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your RentalCompany() constructor isn't initializing carList instance variable.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a side note,
In your returnCar() method, why you are defining a variable that is never got used?
 
Mori Gad
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I should have made myself clear.



To be a constructor should be changed to:



The contents do not change whether it is a constructor or not.

In context, I believe you had the correct contents in the previous version but were not declaring the constructor correctly.



Would create a new RentalCompany with an empty ArrayList of Cars.
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waaahooooooo!!! thank you so much now I can reap the rewards of all the help and hard work of all of you lovely people!!!
and thanks for pointing out the thisCar thing it was just that i got a bit lazy and copied the method before to the return car method to save time as an outline.


just wondering... Why does the constructor have to have void removed?
 
Mori Gad
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better than I can put it...

http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you know, constructor's name should matches the class name.

But it is totally acceptable in Java to have a method that is also named as the class name. Maybe it is a design flaw, can't be sure.
And thats what caused you a problem.


Have a look at: Providing Constructors for Your Classes.
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see a bit weird but thanks again you were really helpful and so far it seems to all be working.... fingers crossed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!