Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: check for null parameters


Message #1 by "Tim Lurowist" <tlurowist@e...> on Mon, 4 Jun 2001 14:01:42
Tim Lurowist wrote:
> 
> String[] playerScores = request.getParameterValues("score");
> 
> and then checking for the "prescence" of a score value.  If a score is
> present. ie. "filled in" DO SOMETHING else check for the prescence of the
> next score value using:
> 
>   if (playerScores[i] == null {
>     continue;  //get the next score
>   }else{
>     DO SOMETHING;
>   }
> 
> This code never evaluates to true.  Does the request.getParameterValues()
> method initialize the String array to null before filling it with
> parameter values?  If not, how can I check for the prescence/abscence of a
> String[] object?


If any html fields have the name "score" AND the fields were filled
(textfield, textarea) or checked (radio, checkbox) or selected (select list,
etc), then getParameterValues will return a non-null array. Each entry in the
array will have one of the entered, checked, or selected values. No value will
be null. So all you need to do is this

  String[] playerScores = request.getParameterValues("score");
  if (playerScores != null) { 
    for (int i = 0; i < playerScores.length; i++) {
      DO SOMETHING;
    }
  }

The return value is null only when no html element has the given name, or no
items were entered, checked, or selected for the given name.

  Return to Index