-2

I have to do a chat client/server project, but i am getting a Null Pointer Exception on pr1.println(msg) on the ChatServer1 class. Any help would be appreciated.

public class ChatClient1 extends JFrame{

    private JPanel contentPane;
    private JPanel panel_1 = new JPanel();
    private static JTextArea textArea = new JTextArea();
    private static JTextField Txt1 = new JTextField();
    private JButton DisconnectBtn = new JButton("DISCONNECT");
    private static JButton SendTxt = new JButton("SEND");
    private JLabel lbl1 = new JLabel("Message to send");

    ServerSocket serversocket;
    static BufferedReader br1;
    static PrintWriter pr1;
    Socket socket;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChatServer1 frame = new ChatServer1();
                    frame.setVisible(true);
                    frame.setTitle("CLIENT");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the frame.
     */
    public ChatClient1() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 542, 383);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        panel_1.setBackground(Color.GRAY);
        panel_1.setBounds(0, 0, 536, 355);
        contentPane.add(panel_1);
        panel_1.setLayout(null);

        DisconnectBtn.setBounds(29, 220, 183, 33);
        panel_1.add(DisconnectBtn);

        textArea.setBounds(235, 11, 291, 242);
        panel_1.add(textArea);

        Txt1.setBounds(29, 303, 387, 41);
        panel_1.add(Txt1);
        Txt1.setColumns(10);

        SendTxt.setBounds(437, 303, 89, 41);
        panel_1.add(SendTxt);

        lbl1.setBounds(29, 278, 123, 14);
        panel_1.add(lbl1);

        try {
            Socket socket = new Socket("LocalHost" , 5000);
            pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
            br1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while(true){
                textArea.append("Server: " + br1.readLine() + '\n' );
            }

        } catch (IOException e) {

        }
        SendTxt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = Txt1.getText();
                pr1.println(msg);
                pr1.flush();
                textArea.append(msg + '\n');
                Txt1.setText("");

            }
        });
    }
}

ChatServer1 class:

public class ChatServer1 extends JFrame{

    private JPanel contentPane;
    private JPanel panel_1 = new JPanel();
    private static JTextArea textArea = new JTextArea();
    private static JTextField Txt1 = new JTextField();
    private JButton DisconnectBtn = new JButton("DISCONNECT");
    private static JButton SendTxt = new JButton("SEND");
    private JLabel lbl1 = new JLabel("Message to send");

    static BufferedReader br1;
    static PrintWriter pr1;
    Socket socket;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChatServer1 frame = new ChatServer1();
                    frame.setVisible(true);
                    frame.setTitle("SERVER");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
    /**
     * Create the frame.
     */
    public ChatServer1() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 542, 383);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        panel_1.setBackground(Color.GRAY);
        panel_1.setBounds(0, 0, 536, 355);
        contentPane.add(panel_1);
        panel_1.setLayout(null);

        DisconnectBtn.setBounds(29, 220, 183, 33);
        panel_1.add(DisconnectBtn);

        textArea.setBounds(235, 11, 291, 242);
        panel_1.add(textArea);

        Txt1.setBounds(29, 303, 387, 41);
        panel_1.add(Txt1);
        Txt1.setColumns(10);

        SendTxt.setBounds(437, 303, 89, 41);
        panel_1.add(SendTxt);

        lbl1.setBounds(29, 278, 123, 14);
        panel_1.add(lbl1);

        try {
            ServerSocket serversocket = new ServerSocket(5000);
            socket = serversocket.accept();
            pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
            br1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while(true){
                textArea.append("Client: " + br1.readLine() + '\n' );
            }

        } catch (IOException e) {

        }
        SendTxt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = Txt1.getText();
                pr1.println(msg);
                pr1.flush();
                textArea.append(msg + '\n');
                Txt1.setText("");

            }
        });
    }
}
MasterOdin
  • 7,117
  • 1
  • 20
  • 35
trant73
  • 1
  • 2
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Krease Dec 15 '15 at 16:09
  • The error of https://stackoverflow.com/questions/72744594/null-pointer-exception-in-java-and-eclipse case? Please help – osmanedip Jul 29 '22 at 13:19

1 Answers1

0

your problem is with the ChatServer1 code, here you have the following:

ServerSocket serversocket = new ServerSocket(5000); socket = serversocket.accept();

When you call accept, its waiting and listening for an incoming connection so never actually reaches your instantiation of pr1. Hence pr1 stays as null. Easy to fix simply move the pr1 instantiation above the accept call like this:

        pr1 = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
        ServerSocket serversocket = new ServerSocket(5000);
        socket = serversocket.accept();

Hope this helps!