Exception nullpointerexception [duplicated]

0

Good day to everyone, I ask you for help because I am just starting to program in this wonderful language. I'm looking at the subject of sockets. The application that I am developing is an exercise of a course. It consists of two applications, a server and a client. The idea is to send the client's text to the server and visualize it in a JTextArea. The structure of the server, which is the most ordered from my humble point of view. Or separating into three classes, one for the frame, one for the panel and finally the main one (with its main method), then I present the code:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sockets;

import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *
 * @author ElioGabriel
 */
public class Servidor {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MarcoServidor srvAplicacion = new MarcoServidor();
        srvAplicacion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

class MarcoServidor extends JFrame implements Runnable{

    //private static final long serialVersionUID = -4936437215667429578L;

    public MarcoServidor() {
        setSize(280, 350);
        setLocation(1200, 300);
        setTitle("Aplicación servidor");
        PanelServidor placaPrincipal = new PanelServidor(pizarra);
        add(placaPrincipal);
        setVisible(true);
        Thread hiloEscucha = new Thread(this);
        hiloEscucha.start();
    }

    @Override
    public void run() {
        try {
            //System.out.println("Estoy a la escucha");
            ServerSocket servidor = new ServerSocket(666);
            Socket miConeccion = servidor.accept();
            DataInputStream flujoEntrada = new DataInputStream(miConeccion.getInputStream());
            String mensaje = flujoEntrada.readUTF();
            pizarra.append("\n" + mensaje);
            miConeccion.close();
        } catch (IOException ex) {
            Logger.getLogger(MarcoServidor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private JTextArea pizarra;
}

class PanelServidor extends JPanel {

    //private static final long serialVersionUID = -5656876162391517099L;

    public PanelServidor(JTextArea campo) {
        setLayout(new BorderLayout());
        campo = new JTextArea();
        add(campo, BorderLayout.CENTER);
    }

}

In line 58 it indicates to me that the JTextArea object has been initialized in null value, but I do not understand why, it was already initialized in the panel constructor, This query is very important for me because it is still not clear to me where to declare some objects, sometimes in the constructor of an object, sometimes in the body of the other. I would like to be able to resolve this issue. Thank you very much everyone for your help. Best regards.

    
asked by gabrieldrv 13.02.2018 в 02:32
source

1 answer

0

This line:

PanelServidor placaPrincipal = new PanelServidor(pizarra);

Pass the value of the reference of your JTextField to the constructor of PanelServidor , any modification made will not be seen by the method that makes the previous call. The value of pizarra will remain null at the end of the execution.

Try to refactor your code so that the above does not happen.

[Edition] I leave you a most complete explanation of the above described:

    
answered by 13.02.2018 в 02:49