1

I'm trying to create a chat application and for some reason whenever I click on the button, I get a null pointer exception at outgoing.

Here is the client:

public class Person1 extends JFrame{
  static String con="";
  static JTextArea conversation;
  static JButton button;
  static JTextField message;
  static DataInputStream incoming;
  static DataOutputStream outgoing;
  static Socket s;
  Person1(){
      setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE);
      JFrame f= new JFrame("Person1");
      message=new JTextField();
      button=new JButton("Send");
      conversation=new JTextArea();
      f.setLayout(null);
      f.setVisible(true);
      f.setSize(800,800);
      button.setFocusPainted(false);
      button.setBounds(350,650,100,30);
      conversation.setBounds(100,100,600,400);
      message.setBounds(200,600,400,30);
      message.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      conversation.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
      conversation.setEditable(false);
      f.add(conversation);
      f.add(button);
      f.add(message);
      button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              String str=message.getText();
              addText(str);
              try {
                  outgoing.writeUTF(str);
                  outgoing.flush();
              } catch (IOException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
          }

      });
  }
  private static void addText(String msg) {
      // TODO Auto-generated method stub
      System.out.println(msg);
      if(!msg.equals("")) {
      con+=msg+"\n";
      conversation.setText(con);
      }}
public static void main(String args[]) {
  EventQueue.invokeLater(new Runnable() {
      public void run() {
          new Person1();
      }
  });
  try {
      s=new Socket("127.0.0.1",1000);
      incoming=new DataInputStream(s.getInputStream());
      outgoing=new DataOutputStream(s.getOutputStream());

      while(true) {
      String msg=incoming.readUTF();
      if(!msg.trim().equals(""))
      addText(msg);
      }
      }catch(Exception e) {

      }


  }
}

Here is the server:

public class Person2 extends  JFrame {
    static String con="";
    static JTextArea conversation;
    static JButton button;
    static JTextField message;
    static DataInputStream incoming;
    static DataOutputStream outgoing;
    static Socket socket;
    static ServerSocket s;
Person2(){
    setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE);
    JFrame f= new JFrame("Person2");
    message=new JTextField();
    button=new JButton("Send");
    conversation=new JTextArea();
    f.setLayout(null);
    f.setVisible(true);
    f.setSize(800,800);
    button.setFocusPainted(false);
    button.setBounds(350,650,100,30);
    conversation.setBounds(100,100,600,400);
    message.setBounds(200,600,400,30);
    message.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    conversation.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    conversation.setEditable(false);
    f.add(conversation);
    f.add(button);
    f.add(message);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String str=message.getText();
            addText(str);
            try {

                outgoing.writeUTF(
                        str);
                outgoing.flush();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });
}
private static void addText(String msg) {
    // TODO Auto-generated method stub
    System.out.println(msg);
    if(!msg.equals("")) {
    con+=msg+"\n";
    conversation.setText(con);
    }}
public static void main(String args[]) {
    try {
        s=new ServerSocket(1000);
        socket=s.accept();

        incoming=new DataInputStream(socket.getInputStream());

        outgoing=new DataOutputStream(socket.getOutputStream());

        while(true) {
        String msg=incoming.readUTF();
        if(!msg.trim().equals(""))
        addText(msg);
        }
        }catch(Exception e) {

        }
EventQueue.invokeLater(new Runnable() {
    public void run() {
        new Person2();
    }
});

}
}


This is the error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Person2$1.actionPerformed(Person2.java:43)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Nah, i know when a null pointer exception happens, but, being in the beginning of the main function, the assignment of dataoutputstream is supposed to happen before any event listener is invoked, right? Then why is it always null. I guess maybe socket itself is null, but then why again – Mohammed raeesul irfan Nov 15 '19 at 18:42
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) I put that because if you understand how to use a stack trace, it should be **obvious** that the person debugging it needs to align the code lines reported in it, back to the source code. That is not possible without the source appearing here (e.g. with imports) to be the same as the source appearing in your IDE. So here are some general tips: – Andrew Thompson Nov 16 '19 at 04:25
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 16 '19 at 04:26
  • 3) Just noticed both examples both extend and use a `JFrame`. Continue with the former, discontinue the latter. – Andrew Thompson Nov 16 '19 at 04:28
  • You havent initialized `outgoing` in time for the action method. – user207421 Nov 16 '19 at 06:39

0 Answers0