> Hi,
>
> i am developing a chat application .Here i want
> to transfer the message in byte array format and
> received message in bytearray format .how is it
> possible.
1 open sockets,
2 send byte arrays,
3 receive byte arrays.
.4.
5profit
> you told like this.
>
>InputStream in=socket.getInputStream();
> n.read(byte[]);
>
> It's make null pointer exception.while i am starting
> the application
>
> Message was edited by:
> Mani_cs
What is making a null pointer exception?
when client is connect to server the receive thread will be run.
public void run() {
byte[] message=null;
try {
while (true) {
din.read(message);
if(message.length > 0) {
StyledDocument receiveDoc=getMessage(message);
setMessage(receiveDoc);
message=null;
}
}
}catch( IOException ie ) { System.out.println( ie ); }
}
Here read methods makes null pointer exception
Quote from read(byte[] b) method if InputStream in API
If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.
> After client connect to server.Thread will be run.So
> that time client cannot send message.surely it will
> be null.when they client send message it will not be
> null
>
> Message was edited by:
> Mani_cs
what will and will not be null?
> Did you understand my pblm or not.
Do you
>
>
> When client connect to server client Thread is
> started to listen receive message.if client is send
> any message then read it and display otherwise it's
> listen receive message.
So what part of this is not working, where is it not working and in what way is it not working
PLEASE learn how to define a problem with sufficient information.
saying "it is not working" is not good enough
Actually what is chatting program
1.server starts to listen client;
2.if any client starts application that is connected server.
3.Client side one Thread is continuously running to receive the message if it's come or not.
4.if any message you want sent you write client socket output stream.Then server readsmessage from the client socket inputstream.In server side one thread is running to listen any message to transfer.if any message it's come it will be send to other client otherwise no message in the client inputstream but here server thread is continuouslyrunning it's read the stream, that no message in the stream so it's throw null pointer exception.
>it's read the stream, that no message in
> the stream so it's throw null pointer exception.
NO how many ******* times do I have to tell you. You are passing a null byte array which is causing the null pointer exception,
If you bothered to read the API you would have found that read() blocks until data is available.