object reference not set to instance of an object

A

Ankit Aneja

giving following error
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.

it is breaking on line:if(cl.status==true)
code for listen class


public class listen

{

TcpListener server=null;

Thread tcpthread=null;

client[] cl=new client[5];


public listen()

{

//

// TODO: Add constructor logic here

//

}

public void startlisten()

{

Int32 port = 3310;

IPAddress localAddr = IPAddress.Parse("192.168.0.5");


// TcpListener server = new TcpListener(port);

server = new TcpListener(localAddr, port);

// Start listening for client requests.

server.Start();


// Enter the listening loop.

// for(int i=0;i<5;i++)

// {

// cl.status=true;

// }

Boolean flag;

while(true)

{ flag=false;

// Perform a blocking call to accept requests.

// You could also user server.AcceptSocket() here.

for(int i=0;i<5;i++)

{

if(cl.status==true)

{

cl= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl.getClient));

tcpthread.Start();

flag=true;

break;

}

}

if(flag!=true)

{

//display error message

}



}

}

}



code for client class

public class client

{

TcpClient tcpClient;

public Boolean status;

// Buffer for reading data

Byte[] bytes = new Byte[256];

String data = null;



public client()

{ //

// TODO: Add constructor logic here

//

//status=true;

}

public client(TcpClient Client)

{

tcpClient =Client;

//

// TODO: Add constructor logic here

//

status=false;

}

public void getClient()

{

try

{

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpClient.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.

string replyMsg = data;

clamdCommand x=new clamdCommand();

replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.

stream.Write(msg, 0, msg.Length);

//Console.WriteLine(String.Format("Sent: {0}", data));

}

}

catch(Exception se)

{

MessageBox.Show(se.ToString());

}

// Shutdown and end connection

tcpClient.Close();

status=true;

}

}
 
M

Marc Gravell

The problem is that you have created an array to hold some client objects,
but you haven't actually created any [client objects] at this point, you
just have an array containing five "null"s; you then test a value on an
object that *cannot* exist yet, and only *after* that do you actually create
one; I would suggest that you uncomment the "Enter the listening loop" code,
but adding cl = new client(), else change the test to deal with
cl==null.

Marc


Ankit Aneja said:
giving following error
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.

it is breaking on line:if(cl.status==true)
code for listen class


public class listen

{

TcpListener server=null;

Thread tcpthread=null;

client[] cl=new client[5];


public listen()

{

//

// TODO: Add constructor logic here

//

}

public void startlisten()

{

Int32 port = 3310;

IPAddress localAddr = IPAddress.Parse("192.168.0.5");


// TcpListener server = new TcpListener(port);

server = new TcpListener(localAddr, port);

// Start listening for client requests.

server.Start();


// Enter the listening loop.

// for(int i=0;i<5;i++)

// {

// cl.status=true;

// }

Boolean flag;

while(true)

{ flag=false;

// Perform a blocking call to accept requests.

// You could also user server.AcceptSocket() here.

for(int i=0;i<5;i++)

{

if(cl.status==true)

{

cl= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl.getClient));

tcpthread.Start();

flag=true;

break;

}

}

if(flag!=true)

{

//display error message

}



}

}

}



code for client class

public class client

{

TcpClient tcpClient;

public Boolean status;

// Buffer for reading data

Byte[] bytes = new Byte[256];

String data = null;



public client()

{ //

// TODO: Add constructor logic here

//

//status=true;

}

public client(TcpClient Client)

{

tcpClient =Client;

//

// TODO: Add constructor logic here

//

status=false;

}

public void getClient()

{

try

{

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpClient.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.

string replyMsg = data;

clamdCommand x=new clamdCommand();

replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.

stream.Write(msg, 0, msg.Length);

//Console.WriteLine(String.Format("Sent: {0}", data));

}

}

catch(Exception se)

{

MessageBox.Show(se.ToString());

}

// Shutdown and end connection

tcpClient.Close();

status=true;

}

}
 
M

Marc Gravell

Hence my comment "but adding cl = new client()" i.e.

for(int i=0;i<5;i++)
{
cl = new client();
cl.status=true;
}

Marc

Ankit Aneja said:
// Enter the listening loop.

for(int i=0;i<5;i++)
{
cl.status=true;
}

uncommenting these lines also gives the same error at same point

Marc Gravell said:
The problem is that you have created an array to hold some client
objects,
but you haven't actually created any [client objects] at this point, you
just have an array containing five "null"s; you then test a value on an
object that *cannot* exist yet, and only *after* that do you actually create
one; I would suggest that you uncomment the "Enter the listening loop" code,
but adding cl = new client(), else change the test to deal with
cl==null.

Marc


Ankit Aneja said:
giving following error
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.

it is breaking on line:if(cl.status==true)
code for listen class


public class listen

{

TcpListener server=null;

Thread tcpthread=null;

client[] cl=new client[5];


public listen()

{

//

// TODO: Add constructor logic here

//

}

public void startlisten()

{

Int32 port = 3310;

IPAddress localAddr = IPAddress.Parse("192.168.0.5");


// TcpListener server = new TcpListener(port);

server = new TcpListener(localAddr, port);

// Start listening for client requests.

server.Start();


// Enter the listening loop.

// for(int i=0;i<5;i++)

// {

// cl.status=true;

// }

Boolean flag;

while(true)

{ flag=false;

// Perform a blocking call to accept requests.

// You could also user server.AcceptSocket() here.

for(int i=0;i<5;i++)

{

if(cl.status==true)

{

cl= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl.getClient));

tcpthread.Start();

flag=true;

break;

}

}

if(flag!=true)

{

//display error message

}



}

}

}



code for client class

public class client

{

TcpClient tcpClient;

public Boolean status;

// Buffer for reading data

Byte[] bytes = new Byte[256];

String data = null;



public client()

{ //

// TODO: Add constructor logic here

//

//status=true;

}

public client(TcpClient Client)

{

tcpClient =Client;

//

// TODO: Add constructor logic here

//

status=false;

}

public void getClient()

{

try

{

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpClient.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.

string replyMsg = data;

clamdCommand x=new clamdCommand();

replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.

stream.Write(msg, 0, msg.Length);

//Console.WriteLine(String.Format("Sent: {0}", data));

}

}

catch(Exception se)

{

MessageBox.Show(se.ToString());

}

// Shutdown and end connection

tcpClient.Close();

status=true;

}

}


 
A

Ankit Aneja

// Enter the listening loop.

for(int i=0;i<5;i++)
{
cl.status=true;
}

uncommenting these lines also gives the same error at same point

Marc Gravell said:
The problem is that you have created an array to hold some client objects,
but you haven't actually created any [client objects] at this point, you
just have an array containing five "null"s; you then test a value on an
object that *cannot* exist yet, and only *after* that do you actually create
one; I would suggest that you uncomment the "Enter the listening loop" code,
but adding cl = new client(), else change the test to deal with
cl==null.

Marc


Ankit Aneja said:
giving following error
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.

it is breaking on line:if(cl.status==true)
code for listen class


public class listen

{

TcpListener server=null;

Thread tcpthread=null;

client[] cl=new client[5];


public listen()

{

//

// TODO: Add constructor logic here

//

}

public void startlisten()

{

Int32 port = 3310;

IPAddress localAddr = IPAddress.Parse("192.168.0.5");


// TcpListener server = new TcpListener(port);

server = new TcpListener(localAddr, port);

// Start listening for client requests.

server.Start();


// Enter the listening loop.

// for(int i=0;i<5;i++)

// {

// cl.status=true;

// }

Boolean flag;

while(true)

{ flag=false;

// Perform a blocking call to accept requests.

// You could also user server.AcceptSocket() here.

for(int i=0;i<5;i++)

{

if(cl.status==true)

{

cl= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl.getClient));

tcpthread.Start();

flag=true;

break;

}

}

if(flag!=true)

{

//display error message

}



}

}

}



code for client class

public class client

{

TcpClient tcpClient;

public Boolean status;

// Buffer for reading data

Byte[] bytes = new Byte[256];

String data = null;



public client()

{ //

// TODO: Add constructor logic here

//

//status=true;

}

public client(TcpClient Client)

{

tcpClient =Client;

//

// TODO: Add constructor logic here

//

status=false;

}

public void getClient()

{

try

{

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpClient.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.

string replyMsg = data;

clamdCommand x=new clamdCommand();

replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.

stream.Write(msg, 0, msg.Length);

//Console.WriteLine(String.Format("Sent: {0}", data));

}

}

catch(Exception se)

{

MessageBox.Show(se.ToString());

}

// Shutdown and end connection

tcpClient.Close();

status=true;

}

}

 
A

Ankit Aneja

Thanks very much
Marc Gravell said:
Hence my comment "but adding cl = new client()" i.e.

for(int i=0;i<5;i++)
{
cl = new client();
cl.status=true;
}

Marc

Ankit Aneja said:
// Enter the listening loop.

for(int i=0;i<5;i++)
{
cl.status=true;
}

uncommenting these lines also gives the same error at same point

Marc Gravell said:
The problem is that you have created an array to hold some client
objects,
but you haven't actually created any [client objects] at this point, you
just have an array containing five "null"s; you then test a value on an
object that *cannot* exist yet, and only *after* that do you actually create
one; I would suggest that you uncomment the "Enter the listening loop" code,
but adding cl = new client(), else change the test to deal with
cl==null.

Marc



giving following error
System.NullReferenceException' occurred in
Additional information: Object reference not set to an instance of an
object.

it is breaking on line:if(cl.status==true)
code for listen class


public class listen

{

TcpListener server=null;

Thread tcpthread=null;

client[] cl=new client[5];


public listen()

{

//

// TODO: Add constructor logic here

//

}

public void startlisten()

{

Int32 port = 3310;

IPAddress localAddr = IPAddress.Parse("192.168.0.5");


// TcpListener server = new TcpListener(port);

server = new TcpListener(localAddr, port);

// Start listening for client requests.

server.Start();


// Enter the listening loop.

// for(int i=0;i<5;i++)

// {

// cl.status=true;

// }

Boolean flag;

while(true)

{ flag=false;

// Perform a blocking call to accept requests.

// You could also user server.AcceptSocket() here.

for(int i=0;i<5;i++)

{

if(cl.status==true)

{

cl= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl.getClient));

tcpthread.Start();

flag=true;

break;

}

}

if(flag!=true)

{

//display error message

}



}

}

}



code for client class

public class client

{

TcpClient tcpClient;

public Boolean status;

// Buffer for reading data

Byte[] bytes = new Byte[256];

String data = null;



public client()

{ //

// TODO: Add constructor logic here

//

//status=true;

}

public client(TcpClient Client)

{

tcpClient =Client;

//

// TODO: Add constructor logic here

//

status=false;

}

public void getClient()

{

try

{

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpClient.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.

string replyMsg = data;

clamdCommand x=new clamdCommand();

replyMsg=x.Command(replyMsg);

byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);

// Send back a response.

stream.Write(msg, 0, msg.Length);

//Console.WriteLine(String.Format("Sent: {0}", data));

}

}

catch(Exception se)

{

MessageBox.Show(se.ToString());

}

// Shutdown and end connection

tcpClient.Close();

status=true;

}

}


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top