2

I'm develop an application using ASP.NET in c#, and I hit the following error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

and this is my code fragment:

Match matchNew = Regex.Match(parts[0].ToString(), @"([0]\.\d*)|[1]\.[0]$", RegexOptions.IgnoreCase);

It was testing in my local the code was perfectly fine, but it hit this error when moved the same code to my virtual environment.

I'm guessing maybe it is due to the dot net framework in my virtual environment.

Please help and thank you in advanced.

2
  • 3
    I'm more inclined to believe there is a problem with your code. Commented Sep 21, 2011 at 4:15
  • thank you for the respond, I manage to changed the code from parts[0].ToString() to parts[0] + "", then it work fine...please explain... Commented Sep 21, 2011 at 7:24

5 Answers 5

4

Most likely parts[0] is null. This error is equivalent to a NullPointerException in Java.

Try setting a local variable to parts[0].ToString(); on the previous line, and see if the exception is thrown from that line instead.

I recommend only performing your match if you have something to perform it on:

if(parts[0] != null){
    Match matchNew = Regex.Match(parts[0].ToString(), @"([0]\.\d*)|[1]\.[0]$", RegexOptions.IgnoreCase);
}
Sign up to request clarification or add additional context in comments.

Comments

3

From the exception you're getting, it looks like either parts or parts[0] is null.

Perhaps consider introducing a variable for parts to see if that is null, and if so, handle either with a default value or display an error to the user, or throw an exception if that is appropriate in your situation.

object part = parts[0];
if (part == null)
{
    // part is null, either handle with default value or error.
}

Match matchNew = ...;

Comments

2

Most likely it is your array "parts" if its length is 0 then it will fail. Make sure to test it's length first.

Comments

1

replace the code by this :

if(parts != null && parts.Length>0)
{    

    if(parts[0] != null)
    {
       Match matchNew = Regex.Match(parts[0].ToString(), @"([0]\.\d*)|[1]\.[0]$", RegexOptions.IgnoreCase);
    }
}

and it would work fine. The array parts appears to be null in your case. But ideally you should also check if the array has elements and if the element in question is not null before you try to convert it to a string value.

Comments

0

It seems that your array "parts" has no elements and therefore parts[0] is null and the exception occurs due to .ToString() because null can not be converted to string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.