• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception in thread "main" java.lang.NullPointerException at newproject.PG1.main(PG1.java:30)  RSS feed

 
Ken Montgomery
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting exception in thread "main" java.lang.NullPointerException
at newproject.PG1.main(PG1.java:30)
in Eclipse when I run the following code.  How do I fix this?

 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line is line 30?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code always enclose it in Code tags. You do this by highlighting the code in the edit window and then clicking the "Code" button just above the edit window. You can verify that it worked by clicking on "Preview" prior to submitting. I've fixed it for you this time seeing as it's your first post.

And welcome to the Ranch.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"actualTitle" is null. You got this from driver.getTitle(), so something is wrong with getTitle().
 
Ken Montgomery
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  if (actualTitle.contentEquals(expectedTitle)){
Is line 30
 
Ken Montgomery
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown how do I correct the getTitle?
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You appear to be setting actualTitle to the emty String "" in line 18. You doubtless know already that isn't null.
Then in line 24 you are setting it to whatever is returned from that method in driver. That must be returning null. Try adding System.out.println(actualTitle); as line 25 and see whether that prints null.
So when you get to line 30 you are calling a method on null, which the JVM doesn't like, and its way of saying, “ouch!” is to throw an exception.
There is a trick whereby you write such calls apparently backwards, which will sort out the exception, but I think you are better off finding out why you are getting null in line 24. I don't know how the driver gets the title, but at least the web address appears to open Mercury Tours correctly.
Since both variables appear to be String type, do you need contentEquals() rather than plain simple equals()?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!