jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes A null pointer that I dont quite understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "A null pointer that I dont quite understand" Watch "A null pointer that I dont quite understand" New topic
Author

A null pointer that I dont quite understand

DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
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!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: May 28, 2001
Posts: 287
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....
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
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
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Yup that would have been the one I was thinking of! Shouldnt have answered right after waking up!
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
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

Joined: Nov 06, 2000
Posts: 1510
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

Joined: May 28, 2001
Posts: 287
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
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
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.


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
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.


CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Thanks all folks I got it under control now....talk about frustrating......freaking non javascript broswers should be outlawed!
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A null pointer that I dont quite understand