• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line is line 30?
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 if (actualTitle.contentEquals(expectedTitle)){
Is line 30
 
Ken Montgomery
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey Brown how do I correct the getTitle?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic