DateTime mydt = new DateTime();
mydt = Convert.ToDateTime(com.Decrypt(Request.QueryString["Time"].ToString(), com.KeyCode.ToString()));
What am I doing wrong ? Its giving NullReferenceException.
DateTime mydt = new DateTime();
mydt = Convert.ToDateTime(com.Decrypt(Request.QueryString["Time"].ToString(), com.KeyCode.ToString()));
What am I doing wrong ? Its giving NullReferenceException.
Well, it's hard to say exactly what's going on because you've got lots of stuff going on in one statement.
As a simple aid to debugging, break that statement up into several separate ones - it'll make it a lot easier to find out what's going wrong. (Also note that your initial value of mydt
is overwritten in the next statement anyway, so there's no point in it.)
Here's how I would rewrite your code:
// This already returns a string... you don't need to call ToString() on it
string encryptedTime = Request.QueryString["Time"];
// We don't know what "com" is here...
string key = com.KeyCode.ToString();
string decryptedTime = com.Decrypt(encryptedTime, key);
DateTime mydt = Convert.ToDateTime(decryptedTime);
(I'd also usually use DateTime.TryParseExact
, but that's a different matter.)
A NullReferenceException occurs when you try to access a member of a null reference.
When you have a series of member accesses (using the '.' operator) and any reference is null, this will occur.
Any of these could be null:
com
Request.QueryString["Time"]
com.KeyCode
You can debug this by stopping on this line with a breakpoint (click on the line, press F9), and hover on each item in the above list. One of them is bound to be null.
Here's how you might redo your code in order to make it easier to read and debug issues like these:
DateTime mydt; // You don't need to initialize this with a new DateTime
if (com == null)
{
// Do something else, since nothing below this will work
}
var keyCode = com.KeyCode;
var time = Request.QueryString["Time"];
if (keyCode == null || time == null)
{
// Do something else, since nothing below this will work
}
mydt = Convert.ToDateTime(com.Decrypt(time.ToString(), keyCode.ToString()));
There are many possible null references here, but the most likely one is the Time query string variable. Make sure it exists.
Also, is your com variable set? And the com.KeyCode?
Several things can be null..
com, Request.QueryString["Time"], com.KeyCode
Set a breakpoint and find out ;)
One of your objects is null and when you try to access a property of a object that's null you receive a NullReferenceException.
Break out the code into multiple lines and test as either com
, Request.QueryString["Time"]
or com.KeyCode
is null.
NullReferenceException come when a operation work on a object who have a null value or not have a valid value. you need to check that object have valid value or not null before making a operation on them.
if you parse it from anything that check that it is valid in some case if value not valid then he set the object as null in .net
As its name indicates, NullReferenceException is thrown because you are calling methods or properties on something that is null.
Therefore, you need to debug that expression to see which object is null at runtime, in the page you are testing.
We don't have enough info to answer your question directly. But...
There might not be a "Time" parameter in the query string?
Your variable com might be null? Was it properly instantiated?
As we don't see what page you are calling, we cannot tell.
So fire up visual studio and debug the expressions in there. Anyone of them could be null for any number of reasons.
NullReference essentially means that you are using a reference to an object when that object is null.