Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default problem using ObjectOutputStream in multithreading

    hi...
    m developing a chatting software using sockets..
    in that i want to use ObjectOutputStream and ObjectInputStream to send and recieve object...
    the main problem is that i m using multithreading in server..when it is single server and single client application then it works well..but when i m using multithreading at serverside it is giving "NullPointerException"
    and the thing is that when i m using DataOutput and DataInput Streams its working well with the same server supporting multithreading...plz help me...as i need ObjectOutputStream and ObjectInputStream to send and recieve files and other objects.

    the clientside code is:
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class site1 extends JFrame implements ActionListener
    {
    JButton B;
    JTextField tf;
    TextArea ta;
    JLabel l;
    Socket skt;
    ServerSocket servskt;
    ObjectInputStream in;
    ObjectOutputStream out;
    JMenuBar mbar;
    JMenu file,edit,view;
    JMenuItem i1,i2,i3,i4,i5,i6,i7,i8,sconv;
    //StatusBar s;
    public site1()
    {
    .....
    ......
    try
    {
    skt=new Socket("127.0.0.1",4444);
    in=new ObjectInputStream(skt.getInputStream());
    out=new ObjectOutputStream(skt.getOutputStream());
    while(true)
    {
    Object s2=in.readObject();
    ta.append("\nSENDER::\t"+s2.toString()+"\n");
    System.out.println(s2);
    }
    }catch(Exception e9){JOptionPane.showMessageDialog(null,"The other System is offline");}

    }

    public void actionPerformed(ActionEvent ae)
    {
    String st=ta.getSelectedText();
    if(ae.getSource()==B || ae.getSource()==tf)//if i click button
    { String s1=tf.getText();

    try
    {
    //out.writeObject("hhh");

    if(s1.equals(""))
    JOptionPane.showMessageDialog(null,"enter valid text");
    else
    {
    ta.append("\nME:\t"+s1);
    System.out.println(s1);
    tf.setText("");
    System.out.println(s1);
    out.writeObject(s1);
    //out.flush();
    }
    }catch(Exception e3)
    {
    JOptionPane.showMessageDialog(null,"The other System is not reachable.\n Try later");
    System.out.println("33"+e3); }
    /*catch(IOException e3){
    JOptionPane.showMessageDialog(null,"io ex.\n Try later");
    System.out.println("33"+e3); }*/
    }

    if(ae.getSource()==i4)
    {
    JOptionPane.showMessageDialog(null,"Thanks for using Synergy Talk");
    System.exit(1);
    }
    if(ae.getSource()==sconv)
    {
    try
    {
    String s5=ta.getText();
    String sr=JOptionPane.showInputDialog("Enter the name of file");
    byte[] b=s5.getBytes();
    //System.out.println(conv);
    FileOutputStream fout=new FileOutputStream(sr+".txt");
    fout.write(b);
    }catch(Exception E8){ System.out.println("ttt"+E8); }
    }
    if(ae.getSource()==i6)
    {
    //String st=ta.getSelectedText();
    System.out.println("copied"+st);
    }

    if(ae.getSource()==i7)
    {
    System.out.println("test1");
    // String st=ta.getSelectedText();
    String sr=tf.getText()+st;
    tf.setText(sr);
    System.out.println("pasted"+st);
    }
    }

    public static void main(String args[])
    {
    site1 s1=new site1();

    s1.show();
    //s1.setVisible(true);
    s1.setSize(200,200);
    }


    }//end class


    and


    the server code is:
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JOptionPane.*;

    public class synergyserver
    {
    private ServerSocket server;
    private int port = 4444;


    public synergyserver()
    {
    try {
    server = new ServerSocket(port,14);
    } catch (IOException e)
    {
    e.printStackTrace();
    }
    }

    public static void main(String[] args)
    {
    synergyserver example = new synergyserver();
    example.handleConnection();
    }

    public void handleConnection()
    {
    System.out.println("Waiting for client message...");

    //
    // The server do a loop here to accept all connection initiated by the
    // client application.
    //
    while (true)
    {
    try {
    Socket socket = server.accept();
    new ConnectionHandler(socket);
    } catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }
    }

    class ConnectionHandler implements Runnable
    {
    private Socket socket;
    ObjectInputStream din;
    ObjectOutputStream dout;
    //DataOutputStream dout;
    //DataInputStream din;

    public ConnectionHandler(Socket socket)
    {
    this.socket = socket;

    Thread t = new Thread(this);
    t.start();
    }

    public void run() {
    try
    {
    //
    // Read a message sent by client application
    //
    // ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    din=new ObjectInputStream(socket.getInputStream());
    // String message1 = (String) din.readUTF();
    //System.out.println("Message Received: " + message1);

    while(true)
    {

    String message = (String) din.readObject();
    System.out.println("Message Received: " + message);

    //
    // Send a response to the client application
    // ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    dout=new ObjectOutputStream(socket.getOutputStream());

    //dout.writeUTF("Hi...");

    //din.close();
    //dout.close();
    //socket.close();
    }

    //System.out.println("Waiting for client message...");
    } catch (Exception e54545)
    {
    System.out.println("kjhkjhkjhkjhkjh"+e54545);
    //e.printStackTrace();
    }
    //catch (ClassNotFoundException e)
    //{
    //e.printStackTrace();
    //}
    }
    }


    well this was just the demo program i was trying if it works well i can move further..plz help me...
    Last edited by sanjeevbindroo; 10-14-2009 at 10:11 AM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Hard to read your code without code tags [ c o d e ] [ / c o d e] (no spaces)

    Can you give more info on exactly where you are getting the null exception?
    Last edited by mrmatt1111; 10-14-2009 at 04:47 PM.
    My Hobby Project: LegacyClone

  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    well i m having problem with the ObjectOutputStream,when i use writeObject..well here is where i get the problem
    i have a button "send" and when i click on that the program should send the message from textfield to server..
    m storing the text from textfield to String and then i m sending it via ObjectOutputStream


    and the exact problem is here:
    if(ae.getSource()==B || ae.getSource()==tf)//if i click button B or press enter
    { String s1=tf.getText();

    try
    {
    //out.writeObject("hhh");

    if(s1.equals(""))
    JOptionPane.showMessageDialog(null,"enter valid text");
    else
    {
    ta.append("\nME:\t"+s1);
    System.out.println(s1);
    tf.setText("");
    System.out.println(s1);
    out.writeObject(s1);//here is where i get exception
    //out.flush();
    }
    }catch(Exception e3)
    {
    JOptionPane.showMessageDialog(null,"The other System is not reachable.\n Try later");
    System.out.println("33"+e3); }


    now can u suggest anything..
    waiting for ur reply..

  4. #4
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    hey i wrote a different code and its working fine for that but the problem still persists with multithreaded server.....

Similar Threads

  1. Servlet Multithreading
    By Saurabh321 in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-18-2009, 05:13 AM
  2. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 04:18 PM
  3. ObjectOutputStream Example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-20-2008, 09:21 AM
  4. question on ObjectOutputStream
    By money123 in forum New To Java
    Replies: 5
    Last Post: 07-27-2007, 10:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •