Java Forum / General / October 2007
nullPointerException in abstract class
OldPro - 29 Sep 2007 15:51 GMT 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
Matt Humphrey - 29 Sep 2007 16:17 GMT | Why is this giving an error? birthDate is properly initialized, isn't | it? [quoted text clipped - 17 lines] | 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/
Lew - 29 Sep 2007 16:43 GMT > 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 ...
 Signature Lew
Tris Orendorff - 29 Sep 2007 18:16 GMT >> 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.
 Signature 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]
Matt Humphrey - 29 Sep 2007 20:54 GMT | > 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.
Mark Space - 30 Sep 2007 03:08 GMT > 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.
OldPro - 01 Oct 2007 14:22 GMT > > 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. [quoted text clipped - 14 lines] > 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.
Lew - 01 Oct 2007 14:43 GMT > 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.
 Signature Lew
OldPro - 02 Oct 2007 16:55 GMT > > As far as the > > Date class; it is a custom class that I was required to use from the [quoted text clipped - 9 lines] > -- > Lew 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.
Daniel Pitts - 03 Oct 2007 06:51 GMT >>> As far as the >>> Date class; it is a custom class that I was required to use from the [quoted text clipped - 16 lines] > 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.
 Signature ---- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Roedy Green - 04 Oct 2007 03:23 GMT >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.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Lew - 04 Oct 2007 14:37 GMT >> 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 [quoted text clipped - 8 lines] > 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.
 Signature Lew
Daniel Pitts - 29 Sep 2007 16:26 GMT > Why is this giving an error? birthDate is properly initialized, isn't > it? [quoted text clipped - 17 lines] > 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.
Roedy Green - 30 Sep 2007 01:30 GMT >Why is this giving an error? see http://mindprod.com/jgloss/sscce.html especially the part about quoting error messages verbatim and telling people which line they point too. You are asking for a virtuoso performance with handcuffs.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Roedy Green - 30 Sep 2007 01:33 GMT > 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.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|