线程“thread-4”中的异常 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2348122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "thread-4" java.lang.NullPointerException
提问by leba-lev
I'd really appreciate some help with my program
我真的很感激我的程序的一些帮助
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)
my ServerConnect function looks like :-
我的 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();
}
}
}
Its basically a socket programming and multithreaded application. On executing it on different terminals inorder to have the client and server establish connections, I execute my code. But it throws up the error above on both terminals. Its just got something to do with my declaring the myOutput variable at the wrong place. Could someone help me out. From the error message, I've highlighted line 63 and line 45 in the piece of code attached.
它基本上是一个套接字编程和多线程应用程序。在不同的终端上执行它以使客户端和服务器建立连接时,我执行我的代码。但它在两个终端上都抛出了上述错误。它只是与我在错误的位置声明 myOutput 变量有关。有人可以帮我吗。在错误消息中,我在所附代码中突出显示了第 63 行和第 45 行。
回答by whiskeysierra
- Drop the default constructor
- Make your instance fields (stk and sProrcessId) final
- See how your compiler complains and fix those issues
- 删除默认构造函数
- 使您的实例字段(stk 和 sProrcessId)成为最终字段
- 查看您的编译器如何抱怨并解决这些问题
These instructons help you trading runtime errors like your NPE to compile time errors, which is the best thing you can do. Note: This trickis meant to be used in general.
这些说明可帮助您将 NPE 等运行时错误与编译时错误进行交易,这是您能做的最好的事情。注意:此技巧适用于一般情况。
回答by Samir Talwar
Your object is being initialised with the first constructor, which takes no parameters. As a result, sktis never initialised and is therefore null. When you call skt.getOutputStream(), it throws a null pointer exception because it cannot dereference skt.
您的对象正在使用第一个构造函数进行初始化,该构造函数不带任何参数。因此,skt永远不会被初始化,因此是null。当您调用时skt.getOutputStream(),它会抛出一个空指针异常,因为它无法取消引用skt。
回答by n00b
ServerConnect(){}
ServerConnect(Socket connection, String sProcessNo) {
this.skt = connection;
this.sProcessId = sProcessNo;
}
what constructor do you use ? cause skt might be uninitialised
你用什么构造函数?原因 skt 可能未初始化
//Edit : oh i see now you use the wrong constructor
//编辑:哦,我现在看到你使用了错误的构造函数
ServerConnect scnt = new ServerConnect();
to
到
ServerConnect scnt = new ServerConnect(skt,sProcessId);

