java.lang.NullPointerException
[nobr]plz check my code as to why its throwing java.lang.NullPointerException
thanks & best regards
if(rname.equals("")){
error[0]="Registrant name required";
}
elseif(rday.equals("") || rmonth.equals("") || ryr1.equals("") || ryr2.equals(""))
error[1]="Date of Birth required";
}
else{}
for(i=0;i<error.length;i++){
if(error[i]!=null || !error[i].equals("null") || !error[i].equals("")){
out.println(error[i]+"><br>");
}}
output:
0 0 0
java.lang.NullPointerException[/nobr]
[1305 byte] By [
farakha] at [2007-11-27 11:24:26]

null check should be there
One of your references is null. The error message will give you the line; it's up to you to figure out which reference is null.
http://www.tmorris.net/pubs/npe/
~
[nobr]>>null check should be there
there is null check
for(i=0;i<error.length;i++){
if(error[i]!=null || !error[i].equals("null") || !error[i].equals("")){
out.println(error[i]+"><br>");
}}
[/nobr]
> >>null check should be there
> there is null check
Read reply #3.
~
one simple trick to avoid this is to re-write your equals checks
turning
rname.equals("")
into
"".equals(rname)
will no longer nullpointer if rname is null, keep in mind though that this will not actually null test
from the look of your code though, id say you might want to replace the rname.equals("") with rname == null. Check to see if an empty field passes a null or an empty string
> Check to see if an empty field passes a null or an empty string
An empty string will not cause a NullPointerException.
~
> > Check to see if an empty field passes a null or
> an empty string
>
> An empty string will not cause a
> NullPointerException.
>
> ~
i know, my point was that if an empty field in his app always returns a null reference, then he could replace all the .equals("") tests with == null
[nobr]with the following code null pointer problem solved but the output is strange:
String[] error=new String[4];
if(rname==null){
error[0]="Registrant name required";
}
else if(remail.equals("")){
error[2]="Registrant email required";
}
else{}
for(i=0;i<error.length;i++){
out.println(error[i]+"><br>");
}else{
out.println("Hello");
}
}
output:
0 0 0[/nobr]
from what i can see, that code can't even be compiled succesfully (for () {} else {} ) doesnt exist
also, could you please fix your indenting? im not sure if its the nobr tags you use, but it looks horrible
[nobr]if(rname==null || rname.equals("") || "".equals(rname)){
error[0]="Registrant name required";
}
if(remail==null || remail.equals("") || "".equals(remail)){
error[1]="Registrant email required";
}
for(i=0; i<error.length; i++){
if(!error[i].equalsIgnoreCase("null") || !error[i].equals("") || error[i] !=null ){
out.println(error[i]+"><br>");
}
}
output:
Registrant name required
Registrant email required
java.lang.NullPointerException[/nobr]
in your print loop , you need to change the || operators to && operators
the way you have it right now, errors[3] (you made it 4 elements long IIRC) will be null, so your statement will evaluate to (true || true || false), changing your operators to && will ensure that if the array element if either null, "" or "null" it will fail
that should solve it
i would propose the following
if (errors[i] != null && !"".equals(errors[i])) { ...... }
for optimal protection against nullpointers
> output:
> Registrant name required
> Registrant email required
> java.lang.NullPointerException
And where's the important stack trace with that message? Are you 'swallowing' it, by handling the exception yourself and only printing the exception itself somewhere? Stop doing that. It looks like the runtime default exception handler (by you not doing anything about exceptions instead) would have done a better job at showing the exception and lead you to where it is occurring in the code. You've only shot yourself in the foot as it stands right now.
thanks a lot
problem solved