Now that I've got a fairly good handle on my current program, I've got a bug in my CDInputTestDrive class.
Here's the code, updated for two extra fields I've added:
The program compiles fine and runs fine if I enter all the information for the fields. But if I just press 'return' (leave a blank line in case I don't have any info to enter for that field, the program stops and I get a 'null pointer exception error.' It obviously has something to do with the fact that there's no value, but I want the program to accept a blank field as an entry and go to the next.. How do I get the program to recognize a blank for a field entry? I tried making each field entry coding a separate method, putting them in a separate class and then calling the method, but that didn't work. For now, I'd just like to leave the code in this class.
Can you tell me how to get a stack trace of the exceptions? Unless you mean this:
And I'm not sure how to code what you gave me -- unless I need to know exactly what is meant by a null pointer exception and what's wrong in my code. I know that if I press the space bar and hit return, it works fine.
Judging from what I'm seeing, the exeception occurs because there's no object that's being referenced? Or is it something else?
I'm unfamiliar with this method -- I'm getting another message trying to use it.
Not sure what I'm doing wrong -- i tried 'equals' as well and it didn't work. I imported java.lang.String.* as well, but no change.
I'm not sure how to check for null -- what I want to do is make the program ignore an entry -- that is, if the use just hits return if there's no information for the field, rather than having to press the space bar to enter some kind of string so the program won't give a null pointer exception, the meaning of which I'm not sure, so I don't know how to fix it.
A very important thing is to learn to use the API. The error messages produced by the compiler are very helpful. In your case, you can check the API for the java.lang.String class. You'll see that there is no method called equalIngoreCase, neither equalIgnoreCase. There's equalsIgnoreCase.
Forgive me if I'm being dense, but I'm still unclear as to how to fix the code if in fact the entry is null. I get that the exception means that there's no value being referenced; and, if there's a way to deal with that, I haven't been able to find it in the documentation.
And while I'm here, can anyone explain how to make sense of the API's? I have to say it's a little confusing.
I apologize again for asking the same question, but I'm just a newbie.
Also can someone tell me how to produce a stack trace, if I haven't done it already by posting the exception message I got when I ran the program?
Have you looked at the API, for example for the method which was misspelt?
Have you tried (from the stack trace or otherwise) to work out where you are getting the null? You don't always get a null String if you simply push the enter key; you sometime get the empty String "" which doesn't count as a null. You would have to go through the API for what you are using to read and see whether it can return null.
If it is to do with "F", you know you can get rid of that Exception by swapping the order like this?
But that may still mean you have a null reference wandering around which may cause problems later on.
Get rid of the null assignment, and the return null bit.
Why are you opening BufferedReaders and not closing them?
Use System.err, not system.out, for printing anything to do with Exceptions.
What follows started off as a copy-and-paste of your code, before I changed it.And which page in HFJ is that on?
Creating a new Reader for each call to that method is inefficient. You should have a field in that class for the Reader. And note that I have made the method static. I shall let you work out why for yourself.
I'm using this code because it's what I came across in HFJ, pages 112 and 152 - it's the input code from the DotCom game. It was given so the user could input information, but the code is too complicated to explain at this point in the book, so I didn't understand a lot of it. I changed the return value to "" instead of null and it works fine. I haven't come across anything like 'buffered reader' or 'system.err', so I'm not sure how it works yet , which means I don't understand when you mention the use of a new reader for each call to the method and your recommendation about a new field. All I know right now is with the change I made, it works. I'll make changes to the code as I learn.
Yes, that is what it says in my copy of HFJ page 112 and 152. I still don't like it returning null; your change to returning "" looks very sensible. I think I would do well to shut up for a bit, for fear of confusing you more than helping. At least you have got it all working
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad: