Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at crifanLib.getUrlRespHtml(String url, Dictionary`2 headerDict, String charset, Dictionary`2 postDict, Int32 timeout, String postDataStr)
   at crifanLib.getUrlRespHtml(String url, Dictionary`2 headerDict, String charset, Dictionary`2 postDict, String postDataStr)
   at crifanLib.getUrlRespHtml(String url, Dictionary`2 headerDict, Dictionary`2 postDict)
   at crifanLib.getUrlRespHtml(String url, Dictionary`2 headerDict)
   at SEOBacklinkSoftware.frmSEOBacklinkSoftware.searchAndParse(String searchStr)
   at SEOBacklinkSoftware.frmSEOBacklinkSoftware.btnSearch_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
SEOBacklinkSoftware
    Assembly Version: 1.0.0.0
    Win32 Version: 1.1.0.0
    CodeBase: file:///C:/Program%20Files%20(x86)/Microsoft/Setup1/SEOBacklinkSoftware.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
HtmlAgilityPack
    Assembly Version: 1.4.6.0
    Win32 Version: 1.4.6.0
    CodeBase: file:///C:/Program%20Files%20(x86)/Microsoft/Setup1/HtmlAgilityPack.DLL
----------------------------------------
System.Web
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_32/System.Web/2.0.0.0__b03f5f7f11d50a3a/System.Web.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitdebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Posted
Updated 17-Jan-14 12:59pm
v2
Comments
Ron Beyer 16-Jan-14 21:16pm    
You need to post the code including the line that the error occurs on. We cannot help you from just an error message.
rameshk14 17-Jan-14 0:27am    
yeah sure....


here is my code

public string getUrlRespHtml(string url,
Dictionary<string,> headerDict,
string charset,
Dictionary<string,> postDict,
int timeout,
string postDataStr)
{
string respHtml = "";

//HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout);
HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout, postDataStr);

//long realRespLen = resp.ContentLength;

StreamReader sr;
if ((charset != null) && (charset != ""))
{
Encoding htmlEncoding = Encoding.GetEncoding(charset);
sr = new StreamReader(resp.GetResponseStream(), htmlEncoding);

}
else
{
sr = new StreamReader(resp.GetResponseStream()); //Here I am Getting Run time Error Like "object reference not set to an instance of an object. in c#"
}
respHtml = sr.ReadToEnd();

return respHtml;
}
rameshk14 17-Jan-14 5:43am    
yeah sure....


here is my code

public string getUrlRespHtml(string url,
Dictionary<string,> headerDict,
string charset,
Dictionary<string,> postDict,
int timeout,
string postDataStr)
{
string respHtml = "";

//HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout);
HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout, postDataStr);

//long realRespLen = resp.ContentLength;

StreamReader sr;
if ((charset != null) && (charset != ""))
{
Encoding htmlEncoding = Encoding.GetEncoding(charset);
sr = new StreamReader(resp.GetResponseStream(), htmlEncoding);

}
else
{
sr = new StreamReader(resp.GetResponseStream()); //Here I am Getting Run time Error Like "object reference not set to an instance of an object. in c#"
}
respHtml = sr.ReadToEnd();

return respHtml;
}
Philippe Mori 16-Jan-14 22:23pm    
Use a debugger. It help a lot to find the source of such problems.
rameshk14 17-Jan-14 0:14am    
thank you for this suggestion...

i tried in that way...

here is my code

public string getUrlRespHtml(string url,
Dictionary<string, string=""> headerDict,
string charset,
Dictionary<string, string=""> postDict,
int timeout,
string postDataStr)
{
string respHtml = "";

//HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout);
HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout, postDataStr);

//long realRespLen = resp.ContentLength;

StreamReader sr;
if ((charset != null) && (charset != ""))
{
Encoding htmlEncoding = Encoding.GetEncoding(charset);
sr = new StreamReader(resp.GetResponseStream(), htmlEncoding);
respHtml = sr.ReadToEnd();
}
else
{
sr = new StreamReader(resp.GetResponseStream()); //Here I am Getting Run time Error Like "object reference not set to an instance of an object. in c#"
}
respHtml = sr.ReadToEnd();

return respHtml;
}

This is the most common error in c#
It will arise when you try to access the members of an Object which is null

Try to trace the error

Keep Break Point at the line you are getting the Null Reference error

add the below condition whenever you are trying to access an object

C#
Object anyobject ;

if( anyobject != null )
{
  anyobject.Property;
anyobject.Method();
}



helpful links on the topic:
http://www.dotnetperls.com/nullreferenceexception[^]
http://stackoverflow.com/questions/1943465/avoiding-null-reference-exceptions[^]
http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it[^]
 
Share this answer
 
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. 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
 
Comments
Maciej Los 17-Jan-14 18:59pm    
+5
Sergey Alexandrovich Kryukov 17-Jan-14 19:02pm    
Thank you, Maciej.
—SA
Thanks7872 24-Jan-14 15:05pm    
+5...Most popular answer to this question on CP...!
Sergey Alexandrovich Kryukov 24-Jan-14 18:01pm    
Thank you, Rohan. Maybe it's not so good to repeat the same things, but what to do if they ask the same things? :-)
—SA
Member 12734119 1-Oct-16 8:17am    
i don't understand what are you saying.Just tell me easiest way.
 
Share this answer
 
Comments
Maciej Los 17-Jan-14 19:00pm    
+5
Thank you, Maciej. :)

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