Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java网络NullPointerException_Java_Nullpointerexception - Fatal编程技术网

Java网络NullPointerException

Java网络NullPointerException,java,nullpointerexception,Java,Nullpointerexception,所以,我认为VGR可能已经回答了这个问题。下面是我为ServerClient类编写的代码,但是,为什么这段代码会将this.out设为null?如果你还有其他问题要问我,尽管问。提前谢谢 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public cla

所以,我认为VGR可能已经回答了这个问题。下面是我为ServerClient类编写的代码,但是,为什么这段代码会将this.out设为null?如果你还有其他问题要问我,尽管问。提前谢谢

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerClient implements java.lang.AutoCloseable, Runnable{
    public static int port = 59519;
    public static String ip = "192.168.0.3";
    public boolean running;
    public Socket socket;
    public PrintWriter out;
    public BufferedReader in;
    public String name = "Unknown";

    public ServerClient(Socket socket){
        this.socket = socket;
        this.running = true;
        System.out.println("Client created: " + name);
    }

    private void code(){
        try (
                PrintWriter out =
                    new PrintWriter(this.socket.getOutputStream(), true);                   
                BufferedReader in = new BufferedReader(
                    new InputStreamReader(this.socket.getInputStream()));
            ) {
                String inputLine;
                inputLine = in.readLine();
                this.name = inputLine;
                System.out.println("Listening for messages from " + this.name + ".");
                while ((inputLine = in.readLine()) != null) {
                    System.out.println("Message from " + this.name + ": " + inputLine);
                    validate(inputLine);
                }
            } catch (IOException e) {
                System.out.println("Exception caught when trying to listen on port "
                    + port + " or listening for a connection");
                System.out.println(e.getMessage());
                this.running = false;
            } catch (Exception e1){
                e1.printStackTrace();
                this.running = false;
            }
    }

    private void validate(String msg){

    }

    public void send(String msg){
        System.out.println("ServerClient received: " + msg);
        this.out.println(msg);
    }

    @Override
    public void close() throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        code();
    }

    public void start() {

    }
}
谢谢大家!

试试这个

public void send(String msg){
    System.out.println(msg);
}

我希望这会有所帮助。

发生异常,因为this.out为null。它为null,因为没有代码将其设置为值。在Java中,字段最初为null/zero/false

我想你要替换这个:

PrintWriter out =
    new PrintWriter(this.socket.getOutputStream(), true);
为此:

this.out =
    new PrintWriter(this.socket.getOutputStream(), true);

我很确定整个语句都应该移到构造函数中。

所以VGR回答了我的问题。我不得不对代码进行一些修改,直到我能让它工作为止,但这是该类的最终产品:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerClient implements java.lang.AutoCloseable, Runnable{
    public static int port = 59519;
    public static String ip = "192.168.0.3";
    public boolean running;
    public Socket socket;
    public PrintWriter out;
    public BufferedReader in;
    public String name = "Unknown";

    public ServerClient(Socket socket){
        this.socket = socket;
        this.running = true;
        System.out.println("Client created: " + name);
    }

    private void code(){
        try (
                PrintWriter out =
                    new PrintWriter(this.socket.getOutputStream(), true);                   
                BufferedReader in = new BufferedReader(
                    new InputStreamReader(this.socket.getInputStream()));
            ) {
            this.out = out;
                String inputLine;
                inputLine = in.readLine();
                this.name = inputLine;
                System.out.println("Listening for messages from " + this.name + ".");
                while ((inputLine = in.readLine()) != null) {
                    System.out.println("Message from " + this.name + ": " + inputLine);
                    validate(inputLine);
                }
            } catch (IOException e) {
                System.out.println("Exception caught when trying to listen on port "
                    + port + " or listening for a connection");
                System.out.println(e.getMessage());
                this.running = false;
            } catch (Exception e1){
                e1.printStackTrace();
                this.running = false;
            }
    }

    private void validate(String msg){

    }

    public void send(String msg){
        System.out.println("ServerClient received: " + msg);
        this.out.println(msg);
    }

    @Override
    public void close() throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        code();
    }

    public void start() {
        run();
    }
}

谢谢

Msg可能为null,只需使用调试器。在不进行调试的情况下,如何确定?如果this.out是PrintStream,则Msg是否为null无关紧要。更有可能是this.out本身为空。@VGR我更新了帖子。如果你能看看,那就太好了。对我来说,我不确定它到底出了什么问题,但我很确定你指出的是问题所在。TIANPEs是由基本编程错误引起的,而不是由网络引起的。ServerClient实际上是一个用于存储客户端信息和套接字的对象。当我写下这个.out.printlnmg;它通过套接字向客户端发送消息。我必须脱机,但是,稍后我将尝试添加完整代码。facepalm为什么我没有看到这一点。无论如何,谢谢你的帮助。