我真的很感谢我的程序的一些帮助
Exception in thread "Thread-4" java.lang.NullPointerException
at ServerConnect.replyChoice(BaseStaInstance.java:63)
at ServerConnect.run(BaseStaInstance.java:45)
at java.lang.Thread.run(Thread.java:619)
我的 ServerConnect 函数如下所示:-
class ServerConnect extends Thread {
Socket skt;
String sProcessId;
ServerConnect scnt = null;
ObjectOutputStream myOutput;
ObjectInputStream myInput;
ServerConnect(){}
ServerConnect(Socket connection, String sProcessNo) {
this.skt = connection;
this.sProcessId = sProcessNo;
}
public void run() {
try {
myInput = new ObjectInputStream(skt.getInputStream());
ServerConnect scnt = new ServerConnect();
while(true) {
try{
int ownTimeStamp = Global.iTimeStamp;
Object buf = myInput.readObject();
//if we got input, print it out and write a message back to the remote client...
if(buf != null){
LINE 45--> **scnt.replyChoice(buf);**
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch(IOException e) {
e.printStackTrace();
}
}
void replyChoice(Object buf){
try{
LINE 63 --> **myOutput = new ObjectOutputStream(skt.getOutputStream());**
System.out.println("Server read:[ "+buf+" ]");
myOutput.writeObject("got it");
myOutput.flush();
}catch(IOException e){
e.printStackTrace();
}
}
}
它基本上是一个套接字编程和多线程应用程序。在不同的终端上执行它以便让客户端和服务器建立连接时,我执行我的代码。但它在两个终端上都抛出了上面的错误。它只是与我在错误的位置声明 myOutput 变量有关。有人可以帮帮我。从错误消息中,我在附加的代码中突出显示了第 63 行和第 45 行。