When would null pointer exception occurs ?

  • Thread starter Thread starter Tony Cheng
  • Start date Start date
T

Tony Cheng

for (int i=0; i<_request.Form.Count; i++ )
{
string key = _request.Form.GetKey(i);
if ( !key.Equals("formCode")
&& !key.Equals("formLanguage")
&& !key.Equals("__VIEWSTATE")
&& !key.Equals("__EVENTARGUMENT")
&& !key.Equals("__EVENTTARGET")
&& key.IndexOf("sendButton")<0
&& key.IndexOf("clearButton")<0
&& key.IndexOf("HeaderControl")<0
&& key.IndexOf("PostingListControl")<0 )
{
formVO.addFormData(key,
StringUtil.extractStringFromStringArray(_request.Form.GetValues(key)));
}
}

here is a part of my code. After several postback due to invalid validation,
null pointer exception is caught. What I wonder is request.Form.Count means
number of name-value pairs in Form collection. Then variable i would be
within the range of count. The null pointer exception seem to point to the
variable key. Is that possible a name-value pair can contain a null as key ?
Thx
 
Look at the trace.axd to get more information and you will probably see
what's going wrong.

It is also possible that one of the key don't have an associated value.
This happens frequently with the QueryString information. You should store
the value of request.Form.GetValues(key) as an object and make that it's not
null before using it.

S. L.
 
Where I can get the trace.axd ?
And also, the exception thrown from code lied in section

if ( !key.Equals("formCode")
&& !key.Equals("formLanguage")
&& !key.Equals("__VIEWSTATE")
&& !key.Equals("__EVENTARGUMENT")
&& !key.Equals("__EVENTTARGET")
&& key.IndexOf("sendButton")<0
&& key.IndexOf("clearButton")<0
&& key.IndexOf("HeaderControl")<0
&& key.IndexOf("PostingListControl")<0 )

the null pointer exception seem to point to variable key. That's why I asked
would it possible the key would be a null value ? Thx
 
Tony:

Does this happen on a development machine? The quickest way to find
out the root of the problem is to use the debugger.
 
You open a new browser window and you replace the name of the aspx file with
trace.axd. For exemple, if you asp.net file is
http://localhost/LxpNet1/Lxp7_Infragistic1/welcome.aspx; then the new name
will be http://localhost/LxpNet1/Lxp7_Infragistic1/trace.axd .

Here are the parameters that I'm using in the web.config file:
<trace enabled="true" requestLimit="10" pageOutput="false"
traceMode="SortByTime" localOnly="true" />



If pageOutput is set to true instead of false, the output of the trace will
get mixed with the HTML written by the aspx file; clearly something that you
don't want must of the time. You can also use Trace.Write (...) and
Trace.Warn (...) to output data to the trace file.

S. L.
 
thx so much

Sylvain Lafontaine said:
You open a new browser window and you replace the name of the aspx file
with trace.axd. For exemple, if you asp.net file is
http://localhost/LxpNet1/Lxp7_Infragistic1/welcome.aspx; then the new name
will be http://localhost/LxpNet1/Lxp7_Infragistic1/trace.axd .

Here are the parameters that I'm using in the web.config file:
<trace enabled="true" requestLimit="10" pageOutput="false"
traceMode="SortByTime" localOnly="true" />



If pageOutput is set to true instead of false, the output of the trace
will get mixed with the HTML written by the aspx file; clearly something
that you don't want must of the time. You can also use Trace.Write (...)
and Trace.Warn (...) to output data to the trace file.

S. L.
 
Back
Top