• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null pointer exception when trying to enter a blank for a response -- how to fix?

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check if a variable is null using the following :
From there, can you guess what to do next ?

(As a convention, it would be better not to capitalize variable names. Artist->artist, Album->album...)
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been looking over this but can't figure out what to do - and when I tried this code, I still got a null pointer exception. Can you elaborate?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christopher Laurenzano wrote:.. - and when I tried this code, I still got a null pointer exception.


How the code looks now after the changes?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and when I tried this code, I still got a null pointer exception.


You actually have to use it the other way, and check that the variable is not null. Which means, calling if ( myVar != null ).

You should have a stack trace of the NullPointerException. Please post it here.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Christopher,

I would like to add few comments on your code -
1)You should start variable name with lower case - eg. artist instead of Artist

2) Artist.equals("F")||Artist.equals("f") instead of this please use Artist.equalIgnoreCase("F");

3) You're getting null pointer at line 37 i.e Artist.equals("F")||Artist.equals("f") so you should check here whether Artist != null && Artist.equalIgnoreCase("F");

Hope this helps.

Regards,
Patricia.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, patricia.

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.

Found the typo -- need to go to sleep
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Christopher,

See the message is very explanatory in itself. It says that it could not found the method. Just check API and you will find equalsIgnoreCase is the method name.

Replace it in your code and you will not get any compilation error in the relevance.

Cheers
Patricia.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

"F".equalsIgnoreCase(something)

But that may still mean you have a null reference wandering around which may cause problems later on.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I think you usually get "" if you push enter and nothing else, so I am not sure where your null is coming from.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the code I'm using to enter the information (ready bake from HFJ):

I don't understand it all, so if it's here, I don't know what's causing it.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic