📎 AI Summary:
The thread discusses an issue faced by a developer using an API in a Windows desktop application, where clicking the Send button results in a runtime error "Object reference not set to an instance of an object." Contributors suggest debugging, particularly checking if variables like the StreamWriter are properly initialized, but the original poster remains unsure how to resolve the problem. Overall, the conversation reflects a troubleshooting effort with some advice given, but the core issue persists with no definitive fix provided.

Ayush JAin

New Member
Member details
Joined
Apr 12, 2022
Messages
3
Thread Author #1
Hello Team,

I am using API in my desktop windows application and while clicking on Send button Getting this Error message "Object reference not set to an instance of an object." previously this below code was working fine.

C#:
private void btn_Send_Click(object sender1, EventArgs e1)
        {
            String result;
            string apiKey = "Zs3TRsRIFCs-tRyNcGbbKpVO8otloaz2lspUS44tBk";
            string numbers = txt_Contactno.Text;
            string message = txt_MessageBox.Text;
            string sender = "DASHAN";

            String url = "https://api.textlocal.in/send/?apikey=" + apiKey + "&numbers=" + numbers + "&message=" + message + "&sender=" + sender;
            //refer to parameters to complete correct url string

            StreamWriter myWriter = null;         
            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);

            objRequest.Method = "POST";
            objRequest.ContentLength = Encoding.UTF8.GetByteCount(url);
            objRequest.ContentType = "application/x-www-form-urlencoded";
            try
            {
                myWriter = new StreamWriter(objRequest.GetRequestStream());
                myWriter.Write(url);
            }
            catch (Exception e)
            {

            }
            finally
            {
                myWriter.Close();
            }
            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                result = sr.ReadToEnd();
                // Close and clean up the StreamReader
                sr.Close();
                MessageBox.Show("Message Sent");
            }

        }
 

Solution
The error should indicate which line is throwing the error.

Most likely though one of the following is not initialized.
Mywriter, Sr or objresponse

Neemobeer

Windows Forum Team
Staff member
Member details
Joined
Jul 4, 2015
Messages
8,995
The error should indicate which line is throwing the error.

Most likely though one of the following is not initialized.
Mywriter, Sr or objresponse
 

Solution

Ayush JAin

New Member
Member details
Joined
Apr 12, 2022
Messages
3
Thread Author #3
Hello Neemobeer,

Plesae check the error message.
An image from 'Object reference not set to an instance of an object.' Getting this error message'. Code snippet sending a POST request triggers a NullReferenceException on StreamWriter object.
 

Last edited by a moderator:

Neemobeer

Windows Forum Team
Staff member
Member details
Joined
Jul 4, 2015
Messages
8,995
I would add a break at myWriter = new StreamWriter... and debug and make sure the writer is actually being initialized.
 

Ayush JAin

New Member
Member details
Joined
Apr 12, 2022
Messages
3
Thread Author #5
I think writer is not initialized, i am not getting any clue ho wto solve this issue, please help me out .Thanks in Advance
An image from 'Object reference not set to an instance of an object.' Getting this error message'. Visual Studio IDE showing C# code handling HTTP POST request and response stream.
 

Last edited by a moderator: