1
DateTime mydt = new DateTime();
mydt = Convert.ToDateTime(com.Decrypt(Request.QueryString["Time"].ToString(), com.KeyCode.ToString()));

What am I doing wrong ? Its giving NullReferenceException.

Serenity
  • 4,744
  • 19
  • 61
  • 104
  • what is com? Perhaps it is null? – Tokk Sep 29 '10 at 08:52
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine Oct 04 '15 at 09:08

7 Answers7

4

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.)

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Beautiful example of why some sort of Hungarian notation is a good thing. The variable *com* would be understandable if it were prefixed with some indicator of what is supposed to contain. (I couldn't resist!) – CesarGon Sep 29 '10 at 09:16
  • 2
    @CesarGon: It would be understandable if it were just named with something vaguely semantic. It doesn't *really* need to indicate the type directly. It's not like posting snippets in a forum is the primary reading environment for code. – Jon Skeet Sep 29 '10 at 09:17
  • I am not saying that one should use a prefix indicating the language-level type itself of the variable (int, string, DateTime...), but something that indicates what the variable is supposed to contain. I agree with you that we don't need to indicate the type directly. And regarding reading snippets in forums, I find myself and my colleagues doing it more often than I would imagine! – CesarGon Sep 29 '10 at 10:01
4

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()));
codekaizen
  • 25,796
  • 7
  • 81
  • 132
2

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?

Rune Grimstad
  • 33,897
  • 9
  • 58
  • 75
1

Several things can be null..

com, Request.QueryString["Time"], com.KeyCode

Set a breakpoint and find out ;)

Arcturus
  • 25,273
  • 10
  • 88
  • 101
0

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.

Catch22
  • 2,991
  • 26
  • 33
0

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

0

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...

  1. There might not be a "Time" parameter in the query string?

  2. 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.

awrigley
  • 13,121
  • 9
  • 78
  • 125