我真的很感激一些幫助,我的計劃異常在線程 「線程4」 顯示java.lang.NullPointerException
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行。
thk u for ur help – 2010-02-27 17:27:55