2

我关于套接字的代码分为三个类,我已经研究了几个小时(并学习了 Socket Api),我已经把它归结为只剩下一个错误(我可以看到)。从服务器接收到的消息根本不会打印,而是抛出此错误。

线程“Thread-14”中的异常 java.lang.NullPointerException main.Listen.run(Listen.java:11) java.lang.Thread.run(Unknown Source)

控制台类

package main;

public class Console extends Handle {

    String nextl = System.getProperty("line.separator");
    String info = "[INFO] ";
    String ser = "[SERVER] ";
    String err = "[ERROR] ";
    String name = "[" + userName + "] ";

    public void log() {
        startupCon();
        Network network = new Network();
        int allgood = network.connect();
        if (allgood == 34) {
            Listen listen = new Listen();
            new Thread(listen).start();
        }
    }

    void startupCon() {

        System.out.println("===========");
        System.out.println("=+CONSOLE+=");
        System.out.println("===========" + nextl);
    }

    void networkCon(String net) {
        System.out.println(net);
    }

}

网络类:

package main;

import java.net.*;
import java.io.*;

public class Network extends Console {

    int port = 7771;
    String host = "localhost";
    Socket client;

    OutputStream outToServer;
    DataOutputStream out;
    InputStream inFromServer;
    DataInputStream in;

    public int connect() {
        int getOpen = open();
        System.out.println(getOpen);
        int getStream = trans(getOpen);
        System.out.println(getStream);
        return getStream;
    }

    int open() {
        try {

            networkCon(info + "Attemping to connect to /" + host + ":" + port);
            client = new Socket(host, port);
            System.out.println(info + "Established connection with "
                    + client.getRemoteSocketAddress());
            return 42;
        } catch (SocketTimeoutException s) {
            networkCon(err + "Socket timed out!");
            return 9;
        } catch (IOException e) {
            System.out.println(err + "Could not connect to /" + host + ":"
                    + port);
            return 9;
        }
    }

    int trans(int hostup) {
        if (hostup == 42) {
            try {
                outToServer = client.getOutputStream();
                out = new DataOutputStream(outToServer);
                inFromServer = client.getInputStream();
                in = new DataInputStream(inFromServer);
                return 34;
            } catch (SocketTimeoutException s) {
                networkCon(err + "Socket timed out!");
                return 8;
            } catch (IOException e) {
                System.out.println(err + "Connection lost!");
                return 8;
            }
        }
        return 7;
    }

}

听课(我存在的祸根)

package main;

import java.io.*;
import java.net.*;

public class Listen extends Network implements Runnable {

    public void run() {
            while (true) {
                try {
                    networkCon(in.readUTF());
                } catch (SocketTimeoutException s) {
                    networkCon(err + "Socket timed out!");
                } catch (IOException e) {
                    System.out.println(err + "Connection lost!");
                }
            }

        }
    }

我猜 Stream 没有在 Listen 类中设置为变量“in”,但我不知道这是问题所在还是如何解决。

4

1 回答 1

4

你无处调用类的方法connect()..Network这就是为什么你DataInputStream in永远不会初始化..它总是null在你的程序中..

我猜您应该在Listen调用connect超类方法的类中定义一个构造函数,Network如下所示:

public Listen()
{
  connect();
}
于 2013-04-04T22:09:10.307 回答