1

I have this line of code in my page load.

string sId = Request["sId"];
//couple of more initializations
 if (!string.IsNullOrEmpty(sId))
            {
                Guid GuidThingId = Guid.Parse(sId); //line 73
//method continues.

In my production logs, for some users I see Null Exception at line 73. Object reference not set to an instance of an object.

Any idea of what's going on?

-Edit- By looking at the $Guid.Parse$ I see that it can only returns below Exceptions.

// Exceptions:
    //   System.ArgumentNullException:
    //     input is null.
    //
    //   System.FormatException:
    //     input is not in a recognized format.
    //
    //   System.Exception:
    //     An internal type conversion error occurred.
CH81
  • 387
  • 4
  • 12
  • 1
    Why is this tagged [tag:c]? And I don't know ASP.NET or anything but it seems like your question lacks the relevant and important information. – Iharob Al Asimi Oct 06 '15 at 20:54
  • sorry, wanted to tag C# – CH81 Oct 06 '15 at 20:55
  • @David Again, I don't know this language but I would say that is unlikely because there is no direct access to `sId` on that line, that's what it seems to me. – Iharob Al Asimi Oct 06 '15 at 20:56
  • 1
    I suspect that that should be `String.isNullOrEmpty` rather than `string.isNullOrEmpty` – CollinD Oct 06 '15 at 20:56
  • @David - `if (!string.IsNullOrEmpty(sId))` should catch `sId` being null. – Tim Oct 06 '15 at 20:56
  • @David but the line above that checks whether this is null or empty. So how is 'sId' can come as null to .Parse – CH81 Oct 06 '15 at 20:57
  • 1
    Are you *sure* that is the line throwing the exception? Did you just count the lines, or are you actually checking the stack trace? What's the stack trace look like? – David Oct 06 '15 at 20:57
  • 3
    @CollinD `String.IsNullOrEmpty` is as valid as `string.IsNullOrEmpty`. – Tim Oct 06 '15 at 20:57
  • @David checking the stack. 'System.NullReferenceException: Object reference not set to an instance of an object. at Web.mypage.Page_Load(Object sender, EventArgs e) in C:\website\mypage.aspx.cs:line 73 at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive()' – CH81 Oct 06 '15 at 21:02
  • The most likely cause is that `sId` is null, which is not possible given the snippet you posted. I'm wondering if you have a syntax error that prevents this check from working as expected. This should easily be solved though. Just step through the code in the debugger and look at all the variables when you get to that line. – Jonathan Wood Oct 06 '15 at 21:05
  • @JonathanWood unfortunately I can't reproduce this in my local environment. I'm seeing these errors in the production logs. – CH81 Oct 06 '15 at 21:07
  • Then can you post more of the actual code? I still think that `sId` might be null somehow. – Jonathan Wood Oct 06 '15 at 21:08
  • @JonathanWood right after the if null check I have the `Guid.Parse` – CH81 Oct 06 '15 at 21:10
  • AFAIC, the only way that line (if that is the line) could cause that error is if `sId` is null. You ask me to assume that there are no syntax errors of any type that could effect the scope of your check for null. I wouldn't make that assumption about my own code. – Jonathan Wood Oct 06 '15 at 21:11
  • 1
    My best guess is still that line 73 isn't what you think it is. – David Oct 06 '15 at 21:16
  • @David: And I have actually had cases where the line number is one off. – Jonathan Wood Oct 06 '15 at 21:18
  • sId is bad format -- missing some characters maybe. – Hogan Oct 06 '15 at 21:22
  • 1
    @Hogan: No, that is a different exception. The exception reported is a null-reference exception. – Jonathan Wood Oct 06 '15 at 21:22
  • But when sId is null, it won't throw a nullref exception either. No calls on sId are made, and in Guid.Parse it will produce an ArgumentNullException instead. My best bet is the sources are out of sync with the production bits. Rule that out first. – Niels V Oct 06 '15 at 21:35
  • Or there might be a variable or property named Guid in scope. – Niels V Oct 06 '15 at 21:37
  • @CH81 - humor me... change string.IsNullOrEmpty() to string.IsNullOrWhitespace(). Then... if nothing changes... Change Guid.Parse to Guid.TryParse, which is a bool, and look at the output (true for success, false for failure)... then... if still nothing changes... add a breakpoint (or something) after the parse, to eliminate the possiblity of the "one-off" issue. – Joe Oct 06 '15 at 22:23
  • 1
    I forgot this was in a live environment. How about simply logging the incoming guid, so when you see the exception in your logs, you can see what guid actually caused the exception – Joe Oct 06 '15 at 22:35
  • @JoeH: You're off track here. We have enough information to know that's not the issue. It is extremely likely that either `sId` is null, or the error is actually being triggered on a different line. – Jonathan Wood Oct 07 '15 at 05:00
  • Is there even any remote possibility that `Guid` being `null` ? – CH81 Oct 07 '15 at 21:13
  • @CH81 - What was the resolution to this issue? – Joe Oct 22 '15 at 13:32

2 Answers2

1

Try it with Guid.TryParse or Guid.TryParseExact. If your example is correct, Your error is probably internal to the parsing, and your string might not be a valid Guid.

Joe
  • 1,091
  • 1
  • 11
  • 23
  • 2
    Suggestions to try should really be a comment, not an answer. Moreover, if the input string was not null but was invalid, then he would get an entirely different exception from the one reported. – Jonathan Wood Oct 06 '15 at 21:26
  • @Jon - part 1, you're right, sorry. part 2, given all of the info we have (or the info we're lacking), you gotta try something. At the very least, it would eliminate one possible issue. – Joe Oct 06 '15 at 22:16
0

Are you sure you sId is correctly formatted?

Guid GuidThingId = new Guid(sId); 
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • According to the answer [here](http://stackoverflow.com/questions/6915966/guid-parse-or-new-guid-which-is-better) both are same – CH81 Oct 06 '15 at 21:04
  • Not the same, those are of type var -- this is of type Guid – Hogan Oct 06 '15 at 21:20
  • 3
    @Hogan: There's no such thing as "type var". They are, in fact, the same thing. – David Oct 06 '15 at 21:27
  • @David only if the expression evaluates to the same type. I could easily write code such that the line `Guid GuidThingId = new Guid(sId);` would not be the same as `var GuidThingId = new Guid(sId);` as I'm sure you could too. But yeah -- **your statement is more correct than mine** :) – Hogan Oct 06 '15 at 21:53
  • 1
    @Hogan: Exactly how many different types do you think "new Guid()" can evaluate to? Constructors are usually pretty consistent about the types they construct. – David Oct 06 '15 at 22:00