Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Exception in thread "main" java.lang.NullPointerException error code

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i try to run the following code and i get a "Exception in thread "main" java.lang.NullPointerException
at model.Restaurant.toString(Restaurant.java:7)"
on line
my code is as follows
 
Marshal
Posts: 8432
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post full stack trace you get.
 
Master Rancher
Posts: 4891
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Exception in thread "main" java.lang.NullPointerException


What variable in the line with the exception has the null value?  Add a print statement just before that line that prints out the values of all the variables used on that line.  The print out will show which is null.
 
Liutauras Vilda
Marshal
Posts: 8432
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Please post full stack trace you get.


I see you posted part of it, which is probably enough here.

However, we don't see which line is 7 in your code. The snippet you posted is hardly that, because code supposed to be at least in a class, which would make this code to shift further a bit. And the only place where NullPointerException may happen in the snippet you posted is this part: this.reviewsCollection.size().

But that couldn't be line 7.
 
Georgios Adamidis
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

Exception in thread "main" java.lang.NullPointerException


What variable in the line with the exception has the null value?  Add a print statement just before that line that prints out the values of all the variables used on that line.  The print out will show which is null.


all four of the variables return null
 
Georgios Adamidis
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

Exception in thread "main" java.lang.NullPointerException


What variable in the line with the exception has the null value?  Add a print statement just before that line that prints out the values of all the variables used on that line.  The print out will show which is null.


even though i tried to input values to the variables as seen below
 
Norm Radder
Master Rancher
Posts: 4891
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What variable in the line with the exception has the null value?  Add a print statement just before that line that prints out the values of all the variables used on that line.  The print out will show which is null.


Why doesn't the code use the values passed as arguments to the constructor instead of using its own values?
Normally the values passed in the constructor are saved in the class.
Setting class variable values with hardcoded values would be done With a constructor without arguments.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic