-1

using "Replace" on the string clientNameStr causes an "Object Reference Not Found" error.

// Get client name
clientName = currentUser.GetValue("ClientName");
string clientNameStr = (string)clientName;
string clientURLStr = string.Empty;
clientURLStr = clientNameStr.Replace(' ', '-');
// clientURLStr = "ST9215-Stanic-Parts-Ltd";

If I substitute in the commented out string (and comment out the existing one) it works fine, so it must be something to do with the replace function, but what? Have tried it with both " and ' quote marks, to the same result.

Any help would be greatly appreciated.

Thanks, Oli.

Sinister Beard
  • 3,615
  • 10
  • 53
  • 90
  • 1
    Are you sure `currentUser.GetValue("ClientName")` isn't returning null? – Rawling Mar 20 '12 at 11:59
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – Grant Thomas Mar 20 '12 at 12:00
  • Step through it with a debugger – Henk Holterman Mar 20 '12 at 12:14
  • If I refresh the page, it returns the correct result; does this help? – Sinister Beard Mar 20 '12 at 12:18
  • Solved it - sort of. It was returning a Null value as you all suggested, as I was trying to get the value from CurrentUser, which wasn't generated until the next page refresh. The working code is as follows: string clientNameStr = custom.UserInfoMembership.UserName + " " + custom.UserInfoMembership.FullName; string clientURLStr = clientNameStr.Replace(" ", "-"); string ExtranetURL = "~/Extranet/" + clientURLStr + ""; Response.Redirect(ExtranetURL); Thanks for all your help. – Sinister Beard Mar 20 '12 at 12:46
  • It's bad that C# throws exception. Instead it should return null value. – Altaf Patel Jul 08 '20 at 07:38

3 Answers3

5

That basically shows that currentUser.GetValue("ClientName") is returning a null reference1. We can't tell what currentUser.GetValue("ClientName") does, but there are two options:

  • It's correctly returning null, and you should handle that
  • It shouldn't return null, and you need to fix it (possibly to throw an exception if it encounters this situation)

1 It's possible that it's returning a non-null reference and using a user-defined conversion to string in the next line which returns null - but unlikely. We can't tell for sure because we don't know the type of the clientName .

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Hmmmmm... it shouldn't be returning null. In this specific case it should return "ST9215 Stanic Parts Ltd". I'll look into that. – Sinister Beard Mar 20 '12 at 12:01
  • @OliJeffery: Can you debug into it, or add logging? I'm sure you'll find it's null - the next question is why... – Jon Skeet Mar 20 '12 at 12:10
3

Probably clientName (and thus clientNameStr) is null. You cannot call methods on the null object, even if you know that it should be a string.

Jens
  • 23,903
  • 6
  • 72
  • 114
0

It's possible that currentUser.GetValue("ClientName") is returning null, thus throwing an error when trying to execute the Replace.

Better coding would be

clientName = currentUser.GetValue("ClientName");
string clientNameStr = clientName ?? "";
string clientURLStr = clientNameStr.Replace(' ', '-');
Alejandro B.
  • 4,302
  • 2
  • 31
  • 54
  • 1
    That's only "better coding" if it's *legitimate* for it to return null *and* the result should be the same as it returning an empty string. I'd expect that quite possibly it should always return a non-null reference or throw an exception, or that if it *can* return null, the calling code should then go down a very different path. – Jon Skeet Mar 20 '12 at 12:11
  • @JonSkeet So true. But then again, the only thing we have is the question's code, the rest we just "assume"... – Alejandro B. Mar 20 '12 at 12:14
  • Yes - sorry, it should always return a value. Your code stops the error, but doesn't generate the right response - i.e. clientURLStr ends up empty. – Sinister Beard Mar 20 '12 at 12:16
  • @OliJeffery Then what you should be checking is the GetValue function, or perhaps the key is not "ClientUser" or something like that... – Alejandro B. Mar 20 '12 at 12:24