471,605 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

object reference not set to instance of an object


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[i].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[i].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[i].status==true)

{

cl[i]= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl[i].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;

}

}
Dec 7 '05 #1
4 2813
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[i] = new client(), else change the test to deal with
cl[i]==null.

Marc
"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:uD***************@TK2MSFTNGP14.phx.gbl...

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[i].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[i].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[i].status==true)

{

cl[i]= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl[i].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;

}

}

Dec 7 '05 #2
// Enter the listening loop.

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

uncommenting these lines also gives the same error at same point

"Marc Gravell" <mg******@rm.com> wrote in message
news:ec**************@TK2MSFTNGP14.phx.gbl...
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[i] = new client(), else change the test to deal with
cl[i]==null.

Marc
"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:uD***************@TK2MSFTNGP14.phx.gbl...

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[i].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[i].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[i].status==true)

{

cl[i]= new client(server.AcceptTcpClient());

tcpthread=new Thread(new ThreadStart(cl[i].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;

}

}


Dec 7 '05 #3
Hence my comment "but adding cl[i] = new client()" i.e.

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

Marc

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
// Enter the listening loop.

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

uncommenting these lines also gives the same error at same point

"Marc Gravell" <mg******@rm.com> wrote in message
news:ec**************@TK2MSFTNGP14.phx.gbl...
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[i] = new client(), else change the test to deal with
cl[i]==null.

Marc
"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:uD***************@TK2MSFTNGP14.phx.gbl...
>
> 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[i].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[i].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[i].status==true)
>
> {
>
> cl[i]= new client(server.AcceptTcpClient());
>
> tcpthread=new Thread(new ThreadStart(cl[i].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;
>
> }
>
> }
>
>



Dec 7 '05 #4
Thanks very much
"Marc Gravell" <mg******@rm.com> wrote in message
news:#f**************@TK2MSFTNGP11.phx.gbl...
Hence my comment "but adding cl[i] = new client()" i.e.

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

Marc

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
// Enter the listening loop.

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

uncommenting these lines also gives the same error at same point

"Marc Gravell" <mg******@rm.com> wrote in message
news:ec**************@TK2MSFTNGP14.phx.gbl...
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[i] = new client(), else change the test to deal with
cl[i]==null.

Marc
"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:uD***************@TK2MSFTNGP14.phx.gbl...
>
> 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[i].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[i].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[i].status==true)
>
> {
>
> cl[i]= new client(server.AcceptTcpClient());
>
> tcpthread=new Thread(new ThreadStart(cl[i].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;
>
> }
>
> }
>
>



Dec 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
15 posts views Thread by Carlos Lozano | last post: by
4 posts views Thread by Luke Matuszewski | last post: by
12 posts views Thread by Andrew Poulos | last post: by
6 posts views Thread by Shailen Sukul | last post: by
14 posts views Thread by Philipp Reif | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.