Note that there are some explanatory texts on larger screens.

plurals
  1. PONullreferenceException is unhandled
    text
    copied!<p>I am not connected to my server and i type some text in the send text and press send, there will be a warning showing in my program <code>serverStream.Write(outStream, 0, outStream.Length);</code> <code>NullreferenceException is unhandled</code>. is there anyway to prevent this warning from showing?</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Threading; namespace SocketClient { public partial class SocketClient : Form { System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient(); NetworkStream serverStream = default(NetworkStream); string readData = null; public SocketClient() { InitializeComponent(); } private void getMessage() { while (true) { serverStream = clientSocket.GetStream(); int buffSize = 0; byte[] inStream = new byte[10025]; buffSize = clientSocket.ReceiveBufferSize; serverStream.Read(inStream, 0, buffSize); string returndata = System.Text.Encoding.ASCII.GetString(inStream); readData = "" + returndata; msg(); } } private void msg() { if (this.InvokeRequired) this.Invoke(new MethodInvoker(msg)); else textDisplay.Text = textDisplay.Text + Environment.NewLine + " &gt;&gt; " + readData; } private void buttonConnect_Click(object sender, EventArgs e) { readData = "Conected to NYP Chat Server ..."; msg(); clientSocket.Connect("127.0.0.1", 8888); serverStream = clientSocket.GetStream(); byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textName.Text + "$"); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); Thread ctThread = new Thread(getMessage); ctThread.Start(); } private void buttonSend_Click(object sender, EventArgs e) { // send text byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textSend.Text + "$"); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); //clear textsend textbox textSend.Text = ""; } private void textDisplay_TextChanged(object sender, EventArgs e) { textDisplay.SelectionStart = textDisplay.Text.Length; textDisplay.ScrollToCaret(); textDisplay.Refresh(); } private void textSend_TextChanged(object sender, EventArgs e) { buttonSend.Enabled = !string.IsNullOrEmpty(textSend.Text); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload