0

am just an intermediate in java lang.iam working on a client(s) server program ,whenever I try to JTEXTAREA.APPEND() (in the client code) it

throws a nullpointerexception

what could be the problem.,

below is the code

server

    package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Server {
ServerSocket serversocket;

public Server(int port){

    try {
        this.serversocket=new ServerSocket(port);
    } catch (IOException ex) {
        Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
}
//thread to accept connections
    public void connections(){
        while(true){
            try {

                Socket clientsocket=serversocket.accept();

                 new Thread(new Handleserver(clientsocket)).start();
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
      } 
    }
    public static void main(String[] args) {
        Server obj=new Server(9999);
     obj.connections();
    }

}
//client handler class

package server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Handleserver implements Runnable {
    Socket clientsoc;
    public Handleserver(Socket clientsoc){
        this.clientsoc=clientsoc;

    }
    //thread to read and write streams
    public void run(){
        try {

            DataInputStream din=new DataInputStream(new BufferedInputStream(clientsoc.getInputStream()));

            DataOutputStream dout=new DataOutputStream(new BufferedOutputStream(clientsoc.getOutputStream()));
            while(true){
             String message=   din.readUTF();

              System.out.println(message);
             dout.writeUTF(message);
             dout.flush();
            }
        } catch (IOException ex) {
            Logger.getLogger(Handleserver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

}

Client

    package daggychatapp.Client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Client extends javax.swing.JFrame implements Runnable {
    DataOutputStream dout;
    DataInputStream din;
    Thread thread1;
    String ip;
    static String  text="failed to send";
    static String tttext="not appending";
    int port;

    public Client(OutputStream out,InputStream in){
        this.din=new DataInputStream(new BufferedInputStream(in));
        this.dout=new DataOutputStream(new BufferedOutputStream(out));

        thread1=new Thread(this);

          thread1.start(); 
           try {

            dout.writeUTF(text);
            dout.flush();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            thread1.stop();
        }
    }
  public void apend(String s){
     txtoutput.append("\n\n"+s+"\n"); 
  }
  //thread to read and append user info
public void run(){
 while(true){
     try {

       this.tttext=din.readUTF();

        System.out.print(tttext);
           txtoutput.append (tttext );
     } catch (IOException ex) {
         Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);

     }
     //safely closing of client windows
     finally{
     thread1=null;
     try {
     din.close();
     } catch (IOException ex) {
     Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
     }
     }
 }   
}

    public Client() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jRadioButton1 = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        txtinput = new javax.swing.JTextField();
        btnsend = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtname = new javax.swing.JTextField();
        txtport = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtip = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtoutput = new javax.swing.JTextArea();

        jRadioButton1.setText("jRadioButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(102, 153, 0));

        txtinput.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N

        btnsend.setBackground(new java.awt.Color(102, 102, 255));
        btnsend.setFont(new java.awt.Font("Trebuchet MS", 0, 18)); // NOI18N
        btnsend.setText("Send");
        btnsend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsendActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel1.setText("Name");

        txtname.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N

        txtport.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel2.setText("Port");

        txtip.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel3.setText("Ip");

        txtoutput.setEditable(false);
        txtoutput.setColumns(20);
        txtoutput.setRows(5);
        txtoutput.setText("nh");
        jScrollPane2.setViewportView(txtoutput);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(txtinput, javax.swing.GroupLayout.PREFERRED_SIZE, 574, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnsend, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtname)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtip, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtport, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(68, 68, 68))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtip, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtport, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(71, 71, 71)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtinput)
                    .addComponent(btnsend, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        
//get ip and port from user inputboxes
    private void btnsendActionPerformed(java.awt.event.ActionEvent evt) {                                        
        this.ip=txtip.getText();
        this.port=Integer.parseInt(txtport.getText());
        this.text=txtinput.getText();
        try {
            Socket socket=new Socket(this.ip,this.port);
            new Client (socket.getOutputStream(),socket.getInputStream());

        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);

        }
    }                                       


    public static void main(String args[]) throws Exception {

        java.awt.EventQueue.invokeAndWait(new Runnable() {
            public void run() {
                new Client().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnsend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField txtinput;
    private javax.swing.JTextField txtip;
    private javax.swing.JTextField txtname;
    private javax.swing.JTextArea txtoutput;
    private javax.swing.JTextField txtport;
    // End of variables declaration                   }

forgive my English ..thank you in advance
  • Don't forget, Swing is NOT thread, updating the UI from outside the context of the EDT will result in unpredictable behaviour – MadProgrammer Jun 28 '17 at 04:04
  • 2
    Debugging NullPointerExceptions (or any other runtime exception) is a skill you need to learn and the duplicate explains that. But I just noticed that you are only calling your `initComponents` method from the constructor with zero arguments. You need to ensure that you also call it from the other constructor. The nicest way to do that is to call `this();` as the very first line in the constructor with 2 arguments. – Erwin Bolwidt Jun 28 '17 at 04:08

0 Answers0