Object reference not set to an instance of an object

W

weird0

This error as pointed out earlier by one of the MVP's comes when
some value is being passed as null. But when i debug, i cant find out
which value is going null as there are hell lot of values. Cant really
check all of them. Only three of them get displayed in locals i.e. "
e,Sender and xxx".

Here is where the exception comes.....( PLease lemme know how can i
fix this bug?? Really stuck): The exception comes on the if statement.

if
(RegistrationManager.RegisterUser(Session["passwordtextbox"].ToString(),DateTime.Parse(Session["passlastchangedtemp"].ToString()),
Session["firstnametextbox"].ToString(),
Session["lastnametextbox"].ToString(),
Session["telnotextbox"].ToString(),
DateTime.Parse(Session["dob"].ToString()),
Session["addresstextbox"].ToString(),
accnametextbox.Text,accnotextbox.Text,pincodetextbox.Text,Session["usernametextbox"].ToString(),
Session["questtextbox"].ToString(),
Session["answertextbox"].ToString(),
Session["FileUpload1"].ToString(), Session["FileUpload2"].ToString(),
Session["FileUpload3"].ToString()) &&
RegistrationFormTable.InsertIntoAccounts(accnametextbox.Text,accnotextbox.Text,balancetextbox.Text,pincodetextbox.Text,branchtextbox.Text,System.DateTime.Now.ToString(),Session["CustomerId"].ToString())==true)
&& (RegistrationFormTable.Insert_ATM_Information(pincodetextbox.Text)
== true) &&
(RegistrationFormTable.Insert_CreditCard_Information(pincodetextbox.Text)
== true))

{
Alert.Show("You are registered successfully");
Response.Redirect("Login.aspx");
}
else
{
Alert.Show("You could not be registered");
}
 
N

Nicholas Paldino [.NET/C# MVP]

With all due respect, you are going to have to do the debugging here.
You are making a lot of chained calls, in the nature of calling ToString on
a number of items returned from the session. You are going to have to check
if the item exists in the session, and if it does, return the value,
otherwise, do something with it (or make sure that the values are not null).

Short of stepping through and evaluating every call to get a value from
the session in the watch window, there is little that can be done here. We
don't have the knowledge of how the Session is populated, so we can't even
begin to guess which one is giving you an error.
 
G

Guest

You *can* check them all. In fact, you don't have a choice, because you
haven't tested for null before attempting to
use the .ToString() static method on each Session item.
So set a breakpoint at the very beginning, and single - step through each
line examining
the value. if nulls are "acceptable", then an example pattern might look
like so,
using the ternary operator:
string firstNameTextBox =
Session["firstnametextbox"]!=null?(string)Session["firstnametextbox"]:"";
Peter
 
C

Chris Dunaway

This error as pointed out earlier by one of the MVP's comes when
some value is being passed as null. But when i debug, i cant find out
which value is going null as there are hell lot of values. Cant really
check all of them. Only three of them get displayed in locals i.e. "
e,Sender and xxx".

Here is where the exception comes.....( PLease lemme know how can i
fix this bug?? Really stuck): The exception comes on the if statement.

if
(RegistrationManager.RegisterUser(Session["passwordtextbox"].ToString(),DateTime.Parse(Session["passlastchangedtemp"].ToString()),
Session["firstnametextbox"].ToString(),
Session["lastnametextbox"].ToString(),
Session["telnotextbox"].ToString(),
DateTime.Parse(Session["dob"].ToString()),
Session["addresstextbox"].ToString(),
accnametextbox.Text,accnotextbox.Text,pincodetextbox.Text,Session["usernametextbox"].ToString(),
Session["questtextbox"].ToString(),
Session["answertextbox"].ToString(),
Session["FileUpload1"].ToString(), Session["FileUpload2"].ToString(),
Session["FileUpload3"].ToString()) &&
RegistrationFormTable.InsertIntoAccounts(accnametextbox.Text,accnotextbox.Text,balancetextbox.Text,pincodetextbox.Text,branchtextbox.Text,System.DateTime.Now.ToString(),Session["CustomerId"].ToString())==true)
&& (RegistrationFormTable.Insert_ATM_Information(pincodetextbox.Text)
== true) &&
(RegistrationFormTable.Insert_CreditCard_Information(pincodetextbox.Text)
== true))

{
Alert.Show("You are registered successfully");
Response.Redirect("Login.aspx");
}
else
{
Alert.Show("You could not be registered");
}

Firstly, I suggest you refactor that if statement into a method that
returns a bool, because it is very hard to read.

Secondly, do a Response.Write, or somehow check, the values of the
Session variables. Perhaps one of them is null. If
RegistrationManager and RegistrationFormManager are not null, then
it's probably a session variable.

Good luck

Chris
 
S

sloan

You can do 1 of 2 things:

Write a "Safe Wrapper"


public static string
SafeSessionValueGet(System.Web.SessionState.HttpSessionState sess, string
sessionKey)
{
if (sess[sessionKey] == null)
{
return string.Empty;
}
else
{
return sess[sessionKey].ToString();
}
}


OR
Always check for Null

if ( null != Page.Session["questtextbox"] )
{
string x = Convert.ToString ( Page.Session["questtextbox"] )
}


You need to clean that code up dude. That's a maintenance nightmare you got
going.

Simply setting some
string abc = Page.Session["mykey"] ; //
would help your cause out alot.






weird0 said:
This error as pointed out earlier by one of the MVP's comes when
some value is being passed as null. But when i debug, i cant find out
which value is going null as there are hell lot of values. Cant really
check all of them. Only three of them get displayed in locals i.e. "
e,Sender and xxx".

Here is where the exception comes.....( PLease lemme know how can i
fix this bug?? Really stuck): The exception comes on the if statement.

if
(RegistrationManager.RegisterUser(Session["passwordtextbox"].ToString(),Date
Time.Parse(Session["passlastchangedtemp"].ToString()),
Session["firstnametextbox"].ToString(),
Session["lastnametextbox"].ToString(),
Session["telnotextbox"].ToString(),
DateTime.Parse(Session["dob"].ToString()),
Session["addresstextbox"].ToString(),
accnametextbox.Text,accnotextbox.Text,pincodetextbox.Text,Session["usernamet
extbox"].ToString(),
Session["questtextbox"].ToString(),
Session["answertextbox"].ToString(),
Session["FileUpload1"].ToString(), Session["FileUpload2"].ToString(),
Session["FileUpload3"].ToString()) &&
RegistrationFormTable.InsertIntoAccounts(accnametextbox.Text,accnotextbox.Te
xt,balancetextbox.Text,pincodetextbox.Text,branchtextbox.Text,System.DateTim
e.Now.ToString(),Session["CustomerId"].ToString())==true)
&& (RegistrationFormTable.Insert_ATM_Information(pincodetextbox.Text)
== true) &&
(RegistrationFormTable.Insert_CreditCard_Information(pincodetextbox.Text)
== true))

{
Alert.Show("You are registered successfully");
Response.Redirect("Login.aspx");
}
else
{
Alert.Show("You could not be registered");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top