Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6643857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:05:57+00:00 2026-05-26T00:05:57+00:00

So, I’m pretty new to all this network programming, and I have a few

  • 0

So, I’m pretty new to all this network programming, and I have a few questions…

I’m building a client-server chat application, wherein the server is running, the client(s) connect(s) to the server, and then when a client sends a message to the server, the server relays it to all the clients. The server is a console application, and the client is a Windows Form Application.

The error I’m getting is in my client, at the very top of my form, I have a textbox to take in a user’s name and a button to “submit” it and connect to the server with that username.

Anyways, this is my button connect code:

    private void btnConnect_Click(object sender, EventArgs e)
    {
        readData = "Connecting to chat server...";
        msg();

        try
        {
            sck_client.Connect("127.0.0.1", 8888);
            sw = new StreamWriter(sck_client.GetStream());
            string toSend = txtUsername.Text;

            sw.Write(toSend);
            sw.Flush();

            chatThread = new Thread(GetMessages);
            chatThread.Start();
        }
        catch (Exception ex)
        {
            readData = ex.ToString(); 
            msg();
        }
    }

msg() quite simply takes the value in readData and prints it to the screen (in a richtextbox). sw is a streamwriter that has been declared publicly, outside of the method, so is sck_client (a TcpClient) and chatThread (a Thread).

Basically, the issue is, when I run my program and try to connect, it throws Exception ex, as though it cannot connect. It throws a NullReferenceException with the text:

System.NullReferenceException: Object reference not set to an instance
of an object. at Chat_Client.Main.btnConnect_Click(Object sender,
EventArgs e) in filepath\Chat_Client\Main.cs:line36

That occurs even when my server is running and listening to port 8888. So, what should I do to fix it?

If you need any more of my code to solve the problem, let me know in a comment and I’ll post it.

To show where the code is instantiated:

public partial class Main : Form // new class for the form itself
    {
        // all of these are declared outside any method:
        TcpClient sck_client = default(TcpClient);
        Thread chatThread = default(Thread);
        string readData = null;
        StreamWriter sw = default(StreamWriter);
        StreamReader sr = default(StreamReader);
        ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:05:57+00:00Added an answer on May 26, 2026 at 12:05 am
    TcpClient sck_client = default(TcpClient);
    ...
    sck_client.Connect("127.0.0.1", 8888);
    

    at some point, you will need to give it a value other than null (the default for TcpClient is null). Also, you probably don’t need a StreamWriter just to send a string – I’d look at using Encoding (to get the bytes; typically UTF8), and a length-prefix of the size (in bytes).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.