X Tutup
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Ron McLeod
Saloon Keepers:
  • Tim Holloway
Bartenders:

Unable to understand NullPointerException in Java program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a beginner in Java and I am facing a NullPointerException in my program. I am not able to understand why this error is happening.
My Goal:
I want to create a simple Student object and print the student name.
Problem:
When I run the program, I get the following error:
Exception in thread "main" java.lang.NullPointerException
   at Main.main(Main.java:6)
My Code:

 
Bartender
Posts: 11218
91
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Work the problem backwards. Main() calls s.display(), has 's' been initialized? yes. So then look  in the display() method itself, it calls name.length(), has 'name' been initialized?
 
Marshal
Posts: 82158
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the concept that every class should have fields, methods, and constructors (well, at least one of each of those). I think you wouldn't have this problem if  you had done that.
 
Saloon Keeper
Posts: 29101
215
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have the concept that every class should have fields, methods, and constructors (well, at least one of each of those).



Plenty of cases where that isn't so. But while there are cases where a class might define none of those, typically a class should define at least one.
 
Master Rancher
Posts: 5291
87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also look more carefully at the exact error message.  If you're using a modern version of Java, you should see something like this:

In particular: Cannot invoke "String.length()" because "this.name" is null.  That's pretty specific, and helpful.
 
Campbell Ritchie
Marshal
Posts: 82158
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . the exact error message. . . .

I tried it and got more or less the same on JShell (Java25):-I hadn't noticed that error message before, and agree, it is much more helpful than the error messages I used to see. The line numbers, which have always been in the stack trace, are also helpful. They don't tell you where the error occurred, but they do tell you where it was detected.
 
Mike Simmons
Master Rancher
Posts: 5291
87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The more informative NullPointerException messages came as part of JDK 14: JEP 358: Helpful NullPointerExceptions
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A NullPointerException occurs here because while you created the Student object, the name variable inside it is still empty (null), and you cannot call .length() on something that doesn't exist.
 
Tim Holloway
Saloon Keeper
Posts: 29101
215
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The term "null pointer exception" is a bit of a misnomer in Java. Java does not have pointers, only object references. The term "null pointer" comes from Java's origins in C, which were discarded while the language was still being designed.

A more accurate term would be "NullReferenceException" or "NullObjectException".
 
Campbell Ritchie
Marshal
Posts: 82158
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, like in C#, “NullMarkerException”. But the name was created so long ago that we are stuck with it.
 
Tim Holloway
Saloon Keeper
Posts: 29101
215
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Or, like in C#, “NullMarkerException”. But the name was created so long ago that we are stuck with it.



C# did not exist when Java was created. The whole "#" language environment is, in fact, largely in response to the fact that Microsoft couldn't hijack Java via J++.

A more direct connection comes from C++, which made object references a key part of the ecosystem. But since object references in C++ are basically a shortcut to pointers, a null object reference in C++ is literally a null pointer value.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic
X Tutup