• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Pointer exception  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys I am getting a NULL pointer exception but I'm not too sure why and how I am getting the error and would I go about fixing it ,

thanks

main




car.java









 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Silly me guys sorry I forgot the return statement
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:Silly me guys sorry I forgot the return statement


More correctly, for others who might be confused by this, you had a return statement that was hard-coded to return a null.

A missing return statement would result in a compilation error and never have gotten to the point where an NPE could occur.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write so many empty lines; they don't improve legibility. Don't mix tab characters and spaces for indenting. Put a space or two before each { at the end of a line. I have edited your code to correct those things (well, not the tabs), so it would look like this:-

car.java

You have repeated code; look at the speedUp methods. That makes me suspect that BMW extends Car and Merc extends Car are not good examples of inheritance. Please add a toString() method to each of your classes which prints out all the details (class name and all the fields). I think you will have a surprise when you call those methods. Also work out why your randomCar method isn't quite as random as you would have liked.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for the tips,

I used the speedUp() method in all classes to just demonstrate polymorphism to myself
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it could be more random that I will have to figure out haha any tips ? =)

thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:Thanks guys for the tips,

I used the speedUp() method in all classes to just demonstrate polymorphism to myself
Aaaaaaaaaaaaaaaaaaaaah!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:it could be more random that I will have to figure out haha any tips ? =)

thanks
Yes. Read about switch in the Java™ Tutorials or your book.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Campbell what books do you recommend ?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with something old: Head First Java (2005 edition) by Kathy Sierra and Bert Bates. Buy a second‑hand copy.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!