Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Server Error in '/' Application.

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.

Source Error: 


Line 22:                 int messageNumber = int.Parse(Request.QueryString["MessageNumber"]);
Line 23:                 Message messag = pop3Client.GetMessage(messageNumber);
Line 24:                 MessagePart messagePar = messag.MessagePart.MessageParts[0];
Line 25:                 lblFrom.Text = messag.Headers.From.Address;
Line 26:                 lblSubject.Text = messag.Headers.Subject;

Source File: C:\Users\IT EXPERT\documents\visual studio 2010\Projects\Removels\Removels\UI\ShowMessage.aspx.cs    Line: 24 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   Removels.ShowMessage.Page_Load(Object sender, EventArgs e) in C:\Users\IT EXPERT\documents\visual studio 2010\Projects\Removels\Removels\UI\ShowMessage.aspx.cs:24
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
Posted
v2
Comments
PrashantSonewane 17-Apr-13 6:02am    
Check the request for this page have querystring named MessageNumber. Your request url should have something like this http://yoururl/default.aspx?MessageNumber=somenumber. Also make sure you are not making spelling mistakes while typing it.
Can you please debug your code and find out which line exactly throwing the error ?
I guess the below line...
MessagePart messagePar = messag.MessagePart.MessageParts[0];
is throwing the error.

In your code You write

int messageNumber = int.Parse(Request.QueryString["MessageNumber"]


befor writing it just check one condtion as per below.

if(Request.QueryString["MessageNumber"] != null )
{
  int messageNumber = int.Parse(Request.QueryString["MessageNumber"]
}
 
Share this answer
 
Comments
Abid Hussain-(Abid) 17-Apr-13 6:18am    
not working still same Error Dnyaneshwar Kondbale566
i just put only one email id same like smptemail ......... then its working waoooooooo thanks all
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-May-13 1:02am    
Not an answer, abuse.
—SA
Abid Hussain-(Abid) 5-May-13 1:17am    
you dont have any answer brother i think its your personal mind problem .... any way thanks for comments....
Sergey Alexandrovich Kryukov 5-May-13 1:41am    
You are welcome. Sorry, no, it's not my personal mind. Fake "answers" cause down-votes and abuse reports. At best, if you collect some abuse reports, the post can be automatically removed, but I saw many cases when people known for persistent abuse (no, you are not the one) get abuse reports on their account which led to the cancellation of the account. You don't want it. So, please consider yourself warned.

Hope you do well and wish you the best of luck.
—SA
If this exception was really thrown in line 24, it simply means that either messag is null, or, if it is not messag.MessagePart is null.

This kind of exception is one of the very easiest cases to detect and fix. You already got nearly all you needed in your hands.

It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 

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