Link to home
Start Free TrialLog in
Avatar of SupremeBeing
SupremeBeing

asked on

server client communication

Hi,

i'm relatively new with java so don't be suprised if i ask stupid questions ;)
I'm working on a multi-user chat application that i'm making for a school project.

I already succeeded in making a connection with the server and exchange messages between that server and that client.
However when trying to write some code so that a client can receive messages from other clients too (not only from server or himself) i got stuck :(
the code i have written isn't complete to do this job but should be step 1
It all compiles very well, but when running i get some exception errors.... :(

I would be very greatfull if someone would take a quick look at my code and give me a solution or correction for the error
For experienced programmers this should only take 5 minutes i think.
And i wouldn't be asking this if i wasn't searching for this over a period of more then 2 weeks, i just can't find the answer myself, I need help :)

You can find the code on this hotmail-adres: chat_probs@hotmail.com
password:
expert
In the email you will also find compiling instructions
Btw you should adjust hostname (look in dos for your hostname =>ipconfig /all) in code (not by typing it in)
I used port: 4444
If you have any questions email me : levawa@vt4.net

NO SPAM PLEASE
Thanks in advance dudes,
SupremeBeing
Avatar of girionis
girionis
Flag of Greece image

 Post your exception errors here. By doing this other people with similar errors might be helped as well.
Avatar of SupremeBeing
SupremeBeing

ASKER

in client window
----------------
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java:323)
        at Chat.<init>(Chat.java:75)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)

in server window
----------------
Exception in thread "main" java.lang.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:29)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.java:18)

in client window
----------------
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java:323)
        at Chat.<init>(Chat.java:75)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)

in server window
----------------
Exception in thread "main" java.lang.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:29)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.java:18)

 The object you are trying to pass is null. Are you sure you have initialized it and assigned a non-null value?
I think it has something to do with the following
i send a String where i must send an Object
is there a way around without rewriting all my streams


public void sendinfo(){
         
          String gegevens = nick+" "+"test"+" "+"gegevens";
        sendData(gegevens);
    }

public boolean sendData(String data){
         try{
                   out = new ObjectOutputStream(socket.getOutputStream());
                   out.writeObject(data);
              }catch(IOException e ){System.out.println("test2");
                   return false;     }

             return true;
        }
> I think it has something to do with the following i send a String where i must send an Object

  It shouldn't have, no. "Object" is a superclass of all classes so you can freely pass a "String" type where there is an "Object" type expected.

  It is not a type problem but a pointer problem. The object you are pointing at is null. You probably forgot to initialize it somewhere.

  Can you post some sample code before the following lines:

Chat.java:75 (post from 70-75 lines)
Client.java:20 (post from 15-20)
MultiServerThread.java:29 (from 10-30)
Think this would be easier
=>
You can find the code on this hotmail-adres: chat_probs@hotmail.com
password:
expert

no?
 I mentioned it earlier. Other people might have similar problems. Better post the code here.
Oh ,i see ! sorry :)
In any case if the prob get solved i plan to post the solution here

Here is the code =>

Chat.java:75 (post from 67-78 lines)
------------------------------------
          aFrame = new JFrame("Simple Internet Talker");
          aFrame.setContentPane(aDesktopPane);
          aFrame.setSize(750,500);
          aFrame.setVisible(true);
          aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          zendGegevens();
          String message = " ";
        while (true) {
                message = leesData();
                aTextArea.append(message);
                System.out.println(message);
                }

Client.java:20 (post from 14-21)
--------------------------------

   public void connect(){
          Chat c;
               try{
                      socket = new Socket("server",4444);
                                           
                     }catch (Exception e) {System.err.println("test99");System.out.println(e);}
                     c = new Chat(nick,socket);
                }

MultiServerThread.java:29 (from 10-30)
--------------------------------------

    private String str=null;
   
     
     public MultiServerThread(Socket socket) {//constructor
          super("MultiServerThread");// zet titel in venster
          this.socket = socket;
          zendGegevens();
     }
     
     //public voegClientBij(Socket s,ObjectInputStream in,ObjectOutputStream out,String naam)
     //{
     //     Client
     //     ((Server) servers.elementAt(i)).addChatter(connection,input,output,name,info);
     //}    
     public void zendGegevens(){
          try{
          out = new ObjectOutputStream(socket.getOutputStream());
          in = new ObjectInputStream(socket.getInputStream());
          UserManagement um = new UserManagement();
          um.ConnectieGegevens(socket,in,out);
          }catch(IOException e ){System.err.println("test4"); System.out.println("test4");}          
     }    
 Can you please add a:

System.out.println("message is: " + message);

  statement right after the following line:

  message = leesData();

  It should look like:

 while (true) {
               message = leesData();
               System.out.println("message is: " + message);
               aTextArea.append(message);
               System.out.println(message);
               }

  Can you also post the code for this method: leesData();
 And please let us know of the message you get after the: System.out.println("message is: " + message); is executed.
leesData();
-----------

private String leesData(){
           String s=null;
        try{
                  in = new ObjectInputStream(socket.getInputStream());
                     s = (String)in.readObject();
                  System.out.print(s);
             }catch(Exception e){System.out.println("test3");
                 //removeSelf();
                return null;     }

        return s;
        }//einde leesData()
---------------------------------------------------------
message i get after the: System.out.println("message is: " + message); is executed.

=>message is: null
hmmmm Strange

before i tried to make a communication between different clients this method worked without a problem...
 Ok we got it leaking somewhere... Can you also do a:

System.out.println("socket: " + socket + ", in: " + in + ", out: " + out);

just before the:

um.ConnectieGegevens(socket,in,out);

 line and let us know the result?

 Also in the leesData() method I assume you are not getting the "test3" error message and therefore the exception is not thrown.

  Before the:

in = new ObjectInputStream(socket.getInputStream());

  statement can you do a

System.out.println("socket: " + socket);

  and also before the:

return s;

  System.out.println("s is: " + s)

  and let us know the result?
Actually now you mention it ,i do get the "test3" error message ....

soo i assume i only should do the first thing

in server window
-----------------

socket: Socket[addr=/192.168.0.100,port=2802,localport=4444], in: java.io.Object
InputStream@119298d, out: java.io.ObjectOutputStream@f72617
Exception in thread "main" java.lang.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:30)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.java:18)
Press any key to continue...

in client window
-----------------
Please type in your nick: lennert
Please type in the host address: server
Please type in the host port number: 4444
test3
message is: null
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java:323)
        at Chat.<init>(Chat.java:77)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)
when i do it any way i get

Please type in your nick: test
Please type in the host address: server
Please type in the host port number: 4444
socket: Socket[addr=server/192.168.0.100,port=4444,localport=2810]
test3
message is: null
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java:323)
        at Chat.<init>(Chat.java:77)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)

> Actually now you mention it ,i do get the "test3" error message ....

  If you had told us this from the beginning now we would probably have solved your problem. :-)

  Anyhow... Do a:

  System.out.println("Exception: " + e);
  e.printStackTrace();

  inside your:

catch(Exception e){System.out.println("test3");
                //removeSelf();
               return null;}

  It shoudl look like this:

catch(Exception e)
{
  System.out.println("test3");
  System.out.println("Exception: " + e);
  e.printStackTrace();
  //removeSelf();
  return null;
}

  and tell me the exact error message you are getting.
Oops, i'm soo sorry man, you may call me stupid :)

client side
-----------
Please type in your nick: test
Please type in the host address: server
Please type in the host port number: 4444
socket: Socket[addr=server/192.168.0.100,port=4444,localport=2831]
test3
Exception: java.net.SocketException: Connection reset
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at java.net.SocketInputStream.read(SocketInputStream.java:182)
        at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java
:2133)
        at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream
.java:2426)
        at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputSt
ream.java:2436)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1245)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
        at Chat.leesData(Chat.java:107)
        at Chat.<init>(Chat.java:75)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)
message is: null
Exception in thread "main" java.lang.NullPointerException: null string
        at sun.awt.windows.WTextAreaPeer.insertText(Native Method)
        at java.awt.TextArea.insertText(TextArea.java:306)
        at java.awt.TextArea.appendText(TextArea.java:332)
        at java.awt.TextArea.append(TextArea.java:323)
        at Chat.<init>(Chat.java:77)
        at Client.connect(Client.java:20)
        at Client.main(Client.java:40)

server side
-----------
socket: Socket[addr=/192.168.0.100,port=2831,localport=4444], in: java.io.Object
InputStream@119298d, out: java.io.ObjectOutputStream@f72617
Exception in thread "main" java.lang.NullPointerException
        at UserManagement.addClient(UserManagement.java:50)
        at UserManagement.ConnectieGegevens(UserManagement.java:44)
        at MultiServerThread.zendGegevens(MultiServerThread.java:30)
        at MultiServerThread.<init>(MultiServerThread.java:16)
        at MultiServer.main(MultiServer.java:18)
Press any key to continue...
> Chat.leesData(Chat.java:107)

  Which exactly is the above line (107)?

  Is it this:

  n = new ObjectInputStream(socket.getInputStream());

  or this:

  s = (String)in.readObject();
s = (String)in.readObject();
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Meanwhile i totally rewrote my server on this example
=> http://www.acm.org/crossroads/xrds6-1/ovp61.html

and it works great now!!!

Girionis,Thank you very very very much for your help and quick responses :)
i learnt alot concerning error-messages thxs too you
I can't thank you enough m8



hopefully not until soon ;)
cu
Meanwhile i totally rewrote my server on this example
=> http://www.acm.org/crossroads/xrds6-1/ovp61.html

and it works great now!!!

Girionis,Thank you very very very much for your help and quick responses :)
i learnt alot concerning error-messages thxs too you
I can't thank you enough m8



hopefully not until soon ;)
cu
he really earned those points :)
 Finally, I am glad it's solved :-)

  Please if you feel I have helped you accept an answer otherwise ask a moderator (https://www.experts-exchange.com/Community_Support/) to delete the question and refund the points back to you.
 Thank you for the points :-)