Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code

C#
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;
    }
Posted
Updated 16-Jan-14 20:34pm
v2
Comments
Maarten Kools 17-Jan-14 2:36am    
Sounds to me that resp is null. Did you debug your code and stepped through the getUrlResponse method? I would assume that returns null somewhere.

1 solution

To avoid null reference passed to the StreamReader you need to restructure your code like this.

C#
string respHtml = "";
 
HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout, postDataStr);
if (resp != null)
{
    var respStream = resp.GetResponseStream();

    if (respStream != null)
    {
        using (var sr = String.IsNullOrEmpty(charset)
            ? new StreamReader(respStream)
            : new StreamReader(respStream, Encoding.GetEncoding(charset)))
        {
            respHtml = sr.ReadToEnd();
        }
                
    }
}
 
return respHtml;
 
Share this answer
 
Comments
rameshk14 17-Jan-14 5:58am    
thank you for your reply, you said that change my code as follows....


string respHtml = "";

HttpWebResponse resp = getUrlResponse(url, headerDict, postDict, timeout, postDataStr);
if (resp != null)
{
var respStream = resp.GetResponseStream();

if (respStream != null)
{
using (var sr = String.IsNullOrEmpty(charset)
? new StreamReader(respStream)
: new StreamReader(respStream, Encoding.GetEncoding(charset)))
{
respHtml = sr.ReadToEnd();
}

}
}

return respHtml;




after changing my code as above i am getting the same error at as Follows.....



private void parseSinglePageHtml(string singlePageGoogleHtml)
{
//

Type-2 Diabetes an Autoimmune Disease? - Hemoblogin - School ...


HtmlAgilityPack.HtmlDocument htmlDoc = crifanLib.htmlToHtmlDoc(singlePageGoogleHtml);
HtmlNode rootHtmlNode = htmlDoc.DocumentNode;
HtmlNodeCollection h3aHtmlNodes = rootHtmlNode.SelectNodes("//h3[@class='r']/a");
foreach (HtmlNode h3aNode in h3aHtmlNodes) //Here is an error
{
if (needContinueSearch)
{
searchItemInfo singleItemInfo = new searchItemInfo();

//InnerHtml
//"Losing weight and belly fat improves sleep - Harvard Health ..."
//InnerText:
//"Losing weight and belly fat improves sleep - Harvard Health ..."
string undecodedTitle = h3aNode.InnerText;
//singleItemInfo.title = HttpUtility.HtmlDecode(undecodedTitle);

singleItemInfo.url = h3aNode.Attributes["href"].Value;

processEachSearchItem(singleItemInfo);
}
else
{
break;
}
}

}


even if i try for any changes in this code i am getting same error some where else

please try to help me....
Richard MacCutchan 17-Jan-14 6:03am    
You need to add proper error checking in your program, and use your debugger to find out where it fails. It is no use making a request in your code and just assuming that you will get a valid response. You need to check each response/result and take appropriate action when it fails.
rameshk14 17-Jan-14 6:24am    
thank you just now the problem get solved...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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