locked
HttpClient - System.NullReferenceException: Object reference not set to an instance of an object. RRS feed

  • Question

  • Hi, I am trying to get string from init.php, but I am getting this error while getting response.

    Please help!

    C#

    private HttpClient httpClient;
    
    		public void BlankPage()
    		{
    			this.InitializeComponent();
    			httpClient = new HttpClient();
    			// Limit the max buffer size for the response so we don't get overwhelmed
    			httpClient.MaxResponseContentBufferSize = 256000;
    			httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
    		}
            public MainPage()
            {
                this.InitializeComponent();
    			
    		}
    		
    		 private async void Start_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    string responseBodyAsText;
                    OutputView.Text = "";
                    StatusText.Text = "Waiting for response ...";
    
                    HttpResponseMessage response = await httpClient.GetAsync("http://melihsimsek.com/server/init.php");
                    response.EnsureSuccessStatusCode();
    
                    StatusText.Text = response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;
                    responseBodyAsText = await response.Content.ReadAsStringAsync();
                    responseBodyAsText = responseBodyAsText.Replace("<br>", Environment.NewLine); // Insert new lines
                    OutputView.Text = responseBodyAsText;
                }
                catch (HttpRequestException hre)
                {
                    StatusText.Text = hre.ToString();
                }
                catch (Exception ex)
                {
                    // For debugging
                    StatusText.Text = ex.ToString();
                }
            }

    PHP Side

    <?php
    echo "asd";
    ?>


    Melih Şimşek / Bilkent University

    Wednesday, January 23, 2013 6:44 PM

Answers

  • Where exactaly is raised the NullReferenceException? Is this at this line :

     HttpResponseMessage response = await httpClient.GetAsync("http://melihsimsek.com/server/init.php");

    There is something weird in your code:

    You have this method :

    public void BlankPage() { ... }

    and a constructor 

    public MainPage() { ... } 

    And in this BlankPage method, you call the InitializeComponent, as if it was a constructor of your page.

    All you code to initialize the HttpClient is in there. I may be wrong but in your code you never call BlankPage, so your HttpClient is never instanciated.

    Try removing the BlankPage method, and putting the intialization code in the MainPage constructor instead, like this :

    public MainPage()
    {
        this.InitializeComponent();
        httpClient = new HttpClient();
        // Limit the max buffer size for the response so we don't get overwhelmed
        httpClient.MaxResponseContentBufferSize = 256000;
        httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
    }
         

    --------------

    http://www.renauddumont.be
    • Edited by RenaudDumontMVP Wednesday, January 23, 2013 7:58 PM
    • Proposed as answer by Aaron Xue Friday, January 25, 2013 10:06 AM
    • Marked as answer by Aaron Xue Monday, February 4, 2013 7:17 AM
    Wednesday, January 23, 2013 7:57 PM