Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object reference not set to an instance of an object.
I have Data inside a dictionary object,what I need is to
compare value pair to the value error and then display a message.
but it is not working I am getting the bellow error:
<<Object reference not set to an instance of an object>>

this the code I am using :

protected void ShowParameters(MRequestMessage request, MResponseMessage response)
   {
       StringBuilder requestStringBuilder = new StringBuilder();
       foreach (KeyValuePair<string, string> kvp in request.RequestFields)
       {
           requestStringBuilder.AppendLine(kvp.Key + " = " + kvp.Value);
       }

       StringBuilder responseStringBuilder = new StringBuilder();
       foreach (KeyValuePair<string, string> kvp in response.ResponseFields)
       {
           responseStringBuilder.AppendLine(kvp.Key + " = " + kvp.Value);
           if (kvp.Value.Contains("Error"))
           {
               msgError.Text = "Information does not match";
               msgError.ForeColor = Color.Red;
               break;
           }

       }
   }


By the way I am getting that error on this line :msgError.Text = "Information does not match";
Posted
Comments
db7uk 27-Apr-13 8:51am    
what is msgError? is this a control that is on the page? is it a control that lives inside a usercontrol that the page is hosting? please provide some HTML markup.
El Dev 27-Apr-13 9:03am    
yes it is on the page:this is it
<asp:Label ID="msgError" runat="server">

1 solution

If you are getting an error on
C#
msgError.Text = "Information does not match";
Then there can only be one cause: msgError is null.

Look to where it is assigned, and where your method is called - you may need to move the call to another location.
 
Share this answer
 
Comments
El Dev 27-Apr-13 9:42am    
Ok please help me for this scenario I am getting a response from a server which is send me data after sending a request and inside the collection of data it is sending I have to check it there is value that is equal to "error" if so I have to reply to the user the error message that I want to pass to the label control:

protected void PerformSale()
{
MRequestMessage request = new MRequestMessage();
MResponseMessage response = new MResponseMessage();

Dictionary<string,> rf = request.RequestFields;
rf.Add("mp_NameOnCard", "Dave R.");
rf.Add("mp_CardNumber", "41111111111111");
MHttpPost.PerformTransaction(request, response);
ShowParameters(request, response);
}
this where I am calling the method ShowParameter
OriginalGriff 27-Apr-13 9:57am    
I can't! I don't have access to you system...you are going to have sort this yourself. Start by looking for all references to msgError, and find where it is assigned to a control. You can then start looking for the relationship between that code and the code which uses it. Until you know that, we are both just whistling in the dark!
El Dev 27-Apr-13 10:14am    
But I do not understand this kind of error because I am testing if the valuepair is equal to "error" then display that error by assigned the text to the label.why I got that error?I can see the response contains error value in the data it is sending.Do you think this should be an error?
OriginalGriff 27-Apr-13 10:26am    
The problem is when you try to set the value of the label.Text: you said that when you said that the error is on the line:
msgError.Text = "Information does not match";
Since the right hand side of the equals sign is just a constant string, it cannot raise a null reference exception - it has to be the left hand side.

The LHS consists of two parts: msgError and a Property Get instruction.
If the problem was within the Get, that could raise a Null reference, but since the property name is "Text" it is almost certainly a standard string and the Get accessor for a string cannot raise a null reference - it just returns a value.
So, the problem has to be with trying to call a Get accessor on an object that is not assigned - it contains a null value.

So you need to find out why msgError has not been assigned a control value! That isn't in the code you have shown so far, so you need to look within the rest of your code to find out where it should be assigned, then work out why that code has not be executed.

Does that make sense?
El Dev 27-Apr-13 10:49am    
let me try...Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900