• 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

A null pointer that I dont quite understand

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a new user sign up form that sends info to a servlet which intern checks for dupe usernames, emails etc. It works fine but constantly throws a null pointer exception, NOT a fatal one...the servlet still runs but my logs are full of these exceptions. Im 99.9% sure its from non-javascript enabled browsers so I built a catch for any of the info that would come in as null telling them they have to go back & do the from again....What I DONT understand is why the null pointers are happening & Im getting real sick of seeing them.....HOw does checking for a null value intern throw a null pointer??? Ive included the form code & the section of the servlet that throws the exception (And commented it as such) Any help here would do wonders for my sanity!! Thanks
///Sign Up form code
<FORM METHOD=POST action="/servlet/NewUserSignUp" onSubmit="return newUserVal();">
<input type="text" name="userName" size="15" maxLength="10">  Please Select A Username.
(Letters, Numbers & underscores (_) or dashes (-) ONLY)
<br> <br>
<input type="text" name="emailAddy" size="15">  Please enter a valid email address
(your password will be emailed to this address).
<br> <br>
<select name="gender" size="1">
<option value="" Selected>Select
<option value="1"> Male
<option value="2"> Female
<option value="3"> Couple
</select> Please select your gender/martial status.
<br> <br>
<input type="submit" value="Sign Me Up">
</FORM>
//Exception throwing section of servlet (Throws at if != null)
try{
if (((req.getParameter("userName") != null) | | (!req.getParameter("userName").trim().equals(""))) | | ((req.getParameter("emailAddy") != null) | | (!req.getParameter("emailAddy").trim().equals(""))) | | (((req.getParameter("gender") != null)) | | (!req.getParameter("gender").trim().equals("")))) {
String userName = null;
String emailAddy = null;
int genderStat;
String passWord = null;
String custIPAddress = req.getRemoteAddr();
String custHost = req.getRemoteHost();
userName = req.getParameter("userName").toUpperCase().trim();
emailAddy = req.getParameter("emailAddy").toUpperCase().trim();
genderStat = Integer.parseInt(req.getParameter("gender"));
res.setContentType ("text/html");
out = res.getWriter();
ONce again thanks for the look see, maybe there is something stupid Im missing here & Ive just plain stared at it too much!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tch! If just one of those short-cut ORs is true the rest of the statement is not checked.
I hate really long if clauses - I would do this one thing at a time:
String userName = req.getParameter("userName")
if( userName == null | |
userName.trim().length() == 0 ){
... process error
}
Bill
------------------
author of:
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now wait a minute here! If I attempt to assign the parameter first as you have shown won't that in itself will throw the exception if its null!
String userName = req.getParameter("userName")
if( userName == null | |
userName.trim().length() == 0 ){
... process error
}
I understand the overcomplicated concerns of this test but the way you have suggested to fix this doesnt look correct at all! Maybe Im wrong but I seem to remember having massive problems with this type thing when I didnt first check to see if the parameter is null....
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I don't recommend doing all those getParameter()'s - If for some reason you want to change the parameter name, you have to run up and down the code. Worse, if you type like *I* do, you'll transpose some letters and the code won't work and you'll spend hours reading it the way you meant rather than the way you typed.
Also, since I believe that all the errors should be reported at once rather than make the user do a page request for each error, my mechanism ends up looking more like this:

If I'm feeling especially picky, I may replace the string literals with a final String constant.
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats an interesting concept but, and correct me if Im wrong here, cant parameters come in in any order they wish. Im not positive but i think I remember ready somewhere that there is no guarantee that parameters from a form will come back in the correct order.........could be wrong.....may have been something else Im thinking of. Neat idea though
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using getParameterNames() which returns an enumeration of all the parameters on the form, then there is no guaramtee that they will be returned in any particular order. Otherwise, the exact parameter requestd should be returned, if it exists, else, null will be returned.

Bosun
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup that would have been the one I was thinking of! Shouldnt have answered right after waking up!
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the code around to somewhat like you suggested and Im STILL getting null pointers all over the place.....what the heck am i doing wrong here? Im am so frustrated I could SCREAM over this. Here is how I changed the code:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
PrintWriter out = null;
final int htmlTrigger = 1;
LogInErrorMessages logMess = new LogInErrorMessages();
HtmlTop top = new HtmlTop();
String userName = req.getParameter("userName").toUpperCase();
String emailAddy = req.getParameter("emailAddy").toUpperCase();
String genderStatA = req.getParameter("gender");
int genderStat = 0;
int nullTrigger = 0;
try{
if ((userName == null) | | (userName.length() == 0)){
nullTrigger = 1;
}
if ((emailAddy == null) | | (emailAddy.length() == 0)){
nullTrigger = 1;
}
if ((genderStatA == null) | | (genderStatA.length() == 0)) {
nullTrigger = 1;
}
else{
genderStat = Integer.parseInt(genderStatA);
}
if (nullTrigger != 1){
String passWord = null;
String custIPAddress = req.getRemoteAddr();
The null pointer is happening at the String userName = rreq.getParameter("userName").toUpperCase();
WHAT the heck! .Please drop me a note back..Im at my wits end here!!
 
Bosun Bello
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling a method "toUpperCase()" on a variable that could potentially be null...
String userName = req.getParameter("userName").toUpperCase();
If userName is null, you will get a NPE.


------------------
Bosun
SCJP for the Java� 2 Platform
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a feeling that might be it but wasnt sure......Im going to change it now and see what happens....thanks for the confirmation
 
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you were talking earlier about changing the parameter name and having to run up and down the code. if you put the parameter name in a constant at the top of the code, (or even in a seperate class or library if it's being passed around alot) then you only have to define it once, and you get a compile error if you write the wrong name in the getParameter(), rather than a puzzling run-time error.

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DC - here's what I typically do. Init the variable, check if param is null, if not assign it to var then do error handling if needed.
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all folks I got it under control now....talk about frustrating......freaking non javascript broswers should be outlawed!
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FYI, sometimes corporate firewalls will actively filter out JavaScript from pages. This happens in my case. It seems to be 'smart' though, as it lets some JS trough, and other times not. Applets and ActiveX are stripped out 24/7 regardless here. Glad you figured out your problem!
 
reply
    Bookmark Topic Watch Topic
  • New Topic