English 中文(简体)
C#在处理精简问题时的忽略性?
原标题:NullReferenceException in C# when dealing with Streams?

因此,我对这个网络的所有方案拟订工作都很新,我有几个问题。

在服务器运行、客户与服务器连接、客户向服务器发送电文后,服务器将其转发给所有客户。 服务器是一种黄色应用,客户是Windows Form Application。

我所看到的错误是我的客户,在我表格的顶端,我有一个文字箱,以用户名和一个纽子来“提交”,并与服务器连接。

任何途径,这都是我顿的连接守则:

    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).

从根本上说,当我管理我的节目并试图连接时,这个问题就成了一种例外,因为它无法连接起来。 它对案文投了新字:

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

甚至在我的服务器运行和听从8888港时也是如此。 因此,我应该做些什么来纠正?

如果你需要我更多的法典来解决这个问题,那么,让我在评论中知道,我把它放在后面。

2. 显示该守则在什么地方出现:

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);
        ...
最佳回答
TcpClient sck_client = default(TcpClient);
...
sck_client.Connect("127.0.0.1", 8888);

在某些时候,您需要给出除<代码>null<>/code>以外的数值(default,TcpClient> 。 另外,你可能不需要一个<代码>StreamWriter,而只是要发出string——我看望使用<代码>Encoding(以获得tes;通常为UTF8)和长处(用 by)。

问题回答

页: 1

TcpClient sck_client = default(TcpClient);

具体地说:

default(TcpClient);

default() will return the default value for a given type. If the type is a reference type (eg. class), then it will return null. If the type is a value type (eg. int) then it will attempt to set it to 0.

The default keyword does NOT create a new instance of the the class for you, you need to the use the new keyword for that.

我将认真阅读:http://msdn.microsoft.com/en-us/library/fa0ab757.aspx” rel=“nofollow”http://msdn.microsoft.com/en-us/library/fa0ab757.aspx。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签