Typical example:
...
Student student = new Student();
if ( student.name.equals("foo")) { ... }
...
If the constructor doesn't create a String object for the student's
name, then student.name == null. Thus, the equals() method is requested
for no object (for null). This is a typical situation that generates the
NullPointerException.
Hope it helps
mihai
JKay a écrit :
I keep getting the following error when I run my program:
Exception in thread "main" java.lang.NullPointerException
at StudentRecordExample.main(StudentRecordExample.java:35)
I looked up what java.lang.NullPointException is in the API and found
this:
"public class NullPointerException
extends RuntimeException
Thrown when an application attempts to use null in a case where an
object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other
illegal uses of the null object."
Which as far as I understand means that I'm trying to do something
with a null object. Said object is object anna of class Student, and
the method on the line that is throwing the exception is me attempting
to set the name variable for anna to "Anna"
Any ideas?
James
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en