#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Seattle, WA
    Posts
    18
    Rep Power
    0

    Unhappy When null isn't null or "" or ??


    OK . . I first searched the forum for an answer and didn't find one. I did find something close, but it didn't change matters. Here's my problem, besides being new at this servlet stuff:

    A page with a form that has two fields (name and age) get submitted with the submit button. I have code that looks like this:

    String Name = null;
    String Age = null;

    Name = request.getParameter("Name");
    Age = request.getParameter("Age");

    etc.....

    then I check to see if the fields had anything in them like this:

    if ( Name == null && Age == null)
    {
    error handler . . .
    }else{
    do what I want . .
    }


    Problem here is if they don't enter a Name and Age it does "do what I want . . " when in fact it should do "error handler". WHY? Why isn't null not null in this case? The example I found said to init the Name and Age fields to "" then check the request.getParameter("Name") & request.getParameter("Age") for null first. Did that . . by init'ing the fields to "" they get instan'd, so you'd think something like:

    if ( Name == "" && Age == "")
    {
    error handler . . .
    }else{
    do what I want . .
    }

    would trigger the error handler, right? Nope . . it doesn't. The only thing I've found to work so far is checking the field.length == 0. This works . . so, back to my question . . When is Null not Null but something entirely different? Any help, clue, link, article, psycho hotline number, ANYTHING would be much appreciated. Thanks

    Rachel
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    34
    Rep Power
    14
    if ( Name == "" && Age == "")
    {
    error handler . . .
    }else{
    do what I want . .
    }
    Strings are objects, pointers, not primatives you have to say if(Name.equals("") && Age.equals(""))
    {
    error handler . . .
    }else{
    do what I want . .
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Seattle, WA
    Posts
    18
    Rep Power
    0

    Smile Perfect!


    Thanks! That was the ticket . . I'm going to read that chapter on ==, != again.

    Rachel

IMN logo majestic logo threadwatch logo seochat tools logo