nullPointerException in abstract class

O

OldPro

Why is this giving an error? birthDate is properly initialized, isn't
it?

public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthDate;

// three-argument constructor
public Employee( String first, String last, String ssn, String
birth )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = new Date(); // Here is where the error occurs (it
doesn't stop the program)
birthDate.setDate(birth);
} // end three-argument Employee constructor
 
M

Matt Humphrey

| Why is this giving an error? birthDate is properly initialized, isn't
| it?
|
| public abstract class Employee
| {
| private String firstName;
| private String lastName;
| private String socialSecurityNumber;
| private Date birthDate;
|
| // three-argument constructor
| public Employee( String first, String last, String ssn, String
| birth )
| {
| firstName = first;
| lastName = last;
| socialSecurityNumber = ssn;
| birthDate = new Date(); // Here is where the error occurs (it
| doesn't stop the program)
| birthDate.setDate(birth);
| } // end three-argument Employee constructor
|

I don't see how the line you've marked could cause a NullPointerException.
Furthermore, if it did, it would certainly stop the program at that point.
Are you sure this is the code you're running--I don't think it is. The
following line would probably produce NPE when birth is null, but the
exception will be thrown in the normal way. The error is probably in parts
of code that are not shown, like whomever calls this. Also, you're
three-argument constructor has four arguments.

Matt Humphrey http://www.iviz.com/
 
D

Daniel Pitts

Why is this giving an error? birthDate is properly initialized, isn't
it?

public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthDate;

// three-argument constructor
public Employee( String first, String last, String ssn, String
birth )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = new Date(); // Here is where the error occurs (it
doesn't stop the program)
birthDate.setDate(birth);
} // end three-argument Employee constructor

Please provide an SSCCE <http://physci.org/codes/sscce/>, so that we
can help you identify the real problem.

You might find the problem yourself in the process of creating the
SSCCE.

Good luck,
Daniel.
 
L

Lew

Matt said:
Also, [your] three-argument constructor has four arguments.

Our three chief arguments are surprise, fear, a ruthless efficiency, an almost
fanatical devotion to the Pope ...
 
T

Tris Orendorff

Matt said:
Also, [your] three-argument constructor has four arguments.

Our three chief arguments are surprise, fear, a ruthless efficiency, an almost
fanatical devotion to the Pope ...

Nobody expects the Spanish Inquisition.

--
Tris Orendorff
[ Anyone naming their child should spend a few minutes checking rhyming slang and dodgy sounding
names. Brad and Angelina failed to do this when naming their kid Shiloh Pitt. At some point, someone at
school is going to spoonerise her name.
Craig Stark]
 
M

Matt Humphrey

| Matt Humphrey wrote:
| > Also, [your] three-argument constructor has four arguments.
|
| Our three chief arguments are surprise, fear, a ruthless efficiency, an
almost
| fanatical devotion to the Pope ...

Yes, I can see you now in the scarlet cape and hat and I'm hoping you've
brought the Comfy Chair.
 
R

Roedy Green

birthDate = new Date(); // Here is where the error occurs (it
doesn't stop the program)

I don't see anything mechanism that could create a
NullPointerException where you claim. Please quote your complete
exact program. I think you have inadvertently hidden the error in
quoting the code.
 
M

Mark Space

Matt said:
I don't see how the line you've marked could cause a NullPointerException.
Furthermore, if it did, it would certainly stop the program at that point.

The program might continue running if the exception is being caught
somewhere else. This method would certainly terminate, however.


Looking at the source for Date(), it calls System.currentTimeMillis() to
set a default date. Maybe the platform you have doesn't support
currentTimeMillis() (it's a native method), or is having some error
related to it?

I would try initializing birthDate with something besides the default
constructor, see if that makes a different. Also, you might want to add
some logging and error trapping. If the current error message isn't
enough to allow you to figure out whats going on, add some of your own.

You could try birthDate = new Date( 0L ); which will avoid calling the
native method, plus any overhead it has.
 
O

OldPro

The program might continue running if the exception is being caught
somewhere else. This method would certainly terminate, however.

Looking at the source for Date(), it calls System.currentTimeMillis() to
set a default date. Maybe the platform you have doesn't support
currentTimeMillis() (it's a native method), or is having some error
related to it?

I would try initializing birthDate with something besides the default
constructor, see if that makes a different. Also, you might want to add
some logging and error trapping. If the current error message isn't
enough to allow you to figure out whats going on, add some of your own.

You could try birthDate = new Date( 0L ); which will avoid calling the
native method, plus any overhead it has.

Thank you for all of your comments. I am new to Java although I have
been programming for more than a twenty years. I was working on the
premise that the problem would be obvious to someone else (i.e.
someone who actually knew what they were doing). The fact that no
obvious error could be found comforts me somewhat, as I now know that
I'm not missing some arcane Java rule about instatiating inside an
abstract class. I'm taking an online class, and Sat. was that
assignment's deadline.
As to what caused the error; it bothers me that the program can report
and error and keep on running... if it can only do so with error
handling, and I don't know that I am using any, then perhaps it is an
old error message, and I need to learn more about the compiler
(BlueJ)... Either way, I can follow up on this clue. As far as the
Date class; it is a custom class that I was required to use from the
textbook, not the official Java class.
BTW, after an error exception, there follows a list of classes and
line numbers. Should I be looking at the first in the line or the
last? Or could the error be generated by any of them? The first one
is the one that I reported in the previous example.
 
L

Lew

OldPro said:
As far as the
Date class; it is a custom class that I was required to use from the
textbook, not the official Java class.

You might have seen this as worth mentioning in the first place.

Had you provided an SSCCE (Short Self-Contained Complete Example) that would
have been revealed, along with the likely source of the error. Leaving out
critical details, like that the line where the error occurs isn't using the
class you seem to be showing, disempowers everyone from helping you.
 
O

OldPro

You might have seen this as worth mentioning in the first place.

Had you provided an SSCCE (Short Self-Contained Complete Example) that would
have been revealed, along with the likely source of the error. Leaving out
critical details, like that the line where the error occurs isn't using the
class you seem to be showing, disempowers everyone from helping you.

When you are new, it is difficult to know what is worth mentioning. I
am reluctant to use an SSCCE, except as a last ditch effort, as it is
the most time consuming way to isolate an error. I seldom had to do
this in Visual Basic. Perhaps I should rethink this with Java; it may
be too complex to debug using the method with which I am accustomed.
I don't think anyone ever asked me for a SSCCE at the Visual Basic
site... errors were usually obvious to the more experienced...

Let me affirm, lest you think me ungrateful, that I appreciate your
attention and the attention of all those who responded. I requested
help only after doing a thorough Use Net search, which brought me no
clue as to the cause of my predicament.

Again, thank you for your efforts.
 
D

Daniel Pitts

OldPro said:
When you are new, it is difficult to know what is worth mentioning. I
am reluctant to use an SSCCE, except as a last ditch effort, as it is
the most time consuming way to isolate an error. I seldom had to do
this in Visual Basic. Perhaps I should rethink this with Java; it may
be too complex to debug using the method with which I am accustomed.
I don't think anyone ever asked me for a SSCCE at the Visual Basic
site... errors were usually obvious to the more experienced...
The problem is that Java is a bit more complex and expressive than
Visual Basic, and therefor the same "snippet" of code can have a
different effect depending on context. Generally, the very least you
should do is show every line in the stack trace up to the point where
*your* code is mentioned, and then present that entire section of your
source code. If you are using anything non-standard (such as a Date
class that isn't java.util.Date), that should also be mentioned, and an
appropriate reference should be given if possible (source code or website)
Let me affirm, lest you think me ungrateful, that I appreciate your
attention and the attention of all those who responded. I requested
help only after doing a thorough Use Net search, which brought me no
clue as to the cause of my predicament.

Again, thank you for your efforts.
And we all appreciate that you've tried to solve this on your own. It
can be frustrating to "do someones homework", but its rewarding to help
someone understand how to do their own homework. You know, teach a man
to fish.

Good luck,
Daniel.
 
R

Roedy Green

When you are new, it is difficult to know what is worth mentioning. I
am reluctant to use an SSCCE, except as a last ditch effort, as it is
the most time consuming way to isolate an error.


But when you post, you are spending not only your time, but the time
of dozens of other people. The balance point then tips toward
preparing the SSCCE.

When keep your own notes, scribbled pencil is fine. When you prepare
the minutes to a meeting, laser print is fine. When you write a best
seller text book, it had better be typeset and well proof-read. The
wider the audience, the more is expected of the writer.
 
L

Lew

Roedy said:
But when you post, you are spending not only your time, but the time
of dozens of other people. The balance point then tips toward
preparing the SSCCE.

When keep your own notes, scribbled pencil is fine. When you prepare
the minutes to a meeting, laser print is fine. When you write a best
seller text book, it had better be typeset and well proof-read. The
wider the audience, the more is expected of the writer.

Besides, preparing an SSCCE often finds your problem before you even post.

And it avoids the error in what should have been obvious: that if the Date
class you display is not the Date class *everyone* else uses, you should tell us.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Needs help in editing 14
NullPointerException 2
NullPointerException 33
Abstract Base Class Initialisation 5
Abstract Classes 8
NullPointerException when passing an array 2
Derived class question 9
abstract class 5

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top