In the below code, I am getting null reference exception I didn't understand why I am getting that. Please help me to solve it.
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;
using System.Net.Sockets;
using System.Threading;
using System.Data.SqlClient;
using System.Configuration;
namespace TCPListener
{
    public partial class Form1 : Form
    {
        // Declare our worker thread
        private Thread workerThread = null;
        public Form1()
        {
            InitializeComponent();
            // Initialise and start worker thread
            this.workerThread = new Thread(new ThreadStart(this.ReceiveTcpData));
            this.workerThread.Start();
        }
        public void timer1_Tick(object sender, EventArgs e)
        {
        }
        public TcpListener server = null;
        public Int32 port = Convert.ToInt32(ConfigurationManager.AppSettings["PUERTO"].ToString());
        public IPAddress localAddr = IPAddress.Parse(ConfigurationManager.AppSettings["IP"].ToString());
        public void OpenSocket()
        {
            try
            {
                // TcpListener server = new TcpListener(port);
                server = new TcpListener(localAddr, port);
                // Start listening for client requests.
                server.Start();
            }
            catch (SocketException e)
            {
                Common.CommonControls.writeToLogFile("SOCKET ERROR: " + e.Message);
            }
            finally
            {
                Common.CommonControls.writeToLogFile("INICIO DE ESCUCHA EN " + DateTime.Now);
            }
        }
        private void ReceiveTcpData()
        {
            //Instancio los objetos
            Entities.Program oPosiciones = new Entities.Program();
            DataAccess.Program oPosicionesDA = new DataAccess.Program();
            try
            {
                // Buffer for reading data
                Byte[] bytes = new Byte[256];
                String data = null;
                // Enter the listening loop.
                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                //TcpClient client = server.AcceptTcpClient();
                TcpClient cliente = new TcpClient();
                try
                {
                    cliente = server.AcceptTcpClient();
                }
                catch (Exception e) { MessageBox.Show(e.ToString()); }
                data = null;
                // Get a stream object for reading and writing
                NetworkStream stream = cliente.GetStream();
                int i;
                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    // Process the data sent by the client.
                    data = data.ToUpper();
                    if (data.Substring(0, 2) == "##")
                    {
                        //SalidaMonitor("Paquete recibido: LOGON REQUEST del equipo");
                        cliente.Close();
                        this.workerThread.Interrupt();
                        return;
                    }
                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                    //Show data on monitor
                    SalidaMonitor("Paquete recibido " + DateTime.Now + ": " + data);
                    //Declare entities
                    oPosiciones.Paquete = data;
                    //Database action
                    oPosicionesDA.InsertarPosiciones(oPosiciones);
                    // Send back a response.
                    //stream.Write(msg, 0, msg.Length);
                    //SalidaMonitor("Paquete enviado: " + msg);
                }
                // Shutdown and end connection
                cliente.Close();
            }
            catch (SocketException e)
            {
                Common.CommonControls.writeToLogFile("SOCKET ERROR: " + e.Message);
            }
            catch (SqlException x)
            {
                Common.CommonControls.writeToLogFile("SQL ERROR: " + x.Message);
            }
            catch (Exception y)
            {
                Common.CommonControls.writeToLogFile("ERROR: " + y.Message);
            }
            finally
            {
                oPosiciones = null;
                oPosicionesDA = null;
                this.workerThread.Interrupt();
            }
        }
        private void SalidaMonitor(string data)
        {
            lstMensajes.Invoke(new MethodInvoker(delegate { lstMensajes.Items.Add(data.ToString()); }));
            lstMensajes.Invoke(new MethodInvoker(delegate { lstMensajes.SelectedIndex = lstMensajes.Items.Count - 1; lstMensajes.SelectedIndex = -1; }));
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            OpenSocket();
        }
        private void Form1_Close(object sender, FormClosingEventArgs e)
        {
            server.Stop();
        }
    }
}
In the above code, I am getting error at cliente = server.AcceptTcpClient();. I don't understand why it's happening. If you need any information, let me know. Thanks
 
     
     
    