gpt4 book ai didi

java - 输入流读取器空指针异常

转载 作者:行者123 更新时间:2023-12-01 13:16:26 41 4
gpt4 key购买 nike

下面的代码片段有时会出现 NullPointerException 的原因是什么。

BufferedReader reader = new BufferedReader(new InputStreamReader
(getClass().getClassLoader().getResourceAsStream(this.getClientHost()+".txt")));

奇怪的是,它有时可以正常工作,但有时会出现异常。我的代码我尝试多次读取该文件。一般来说,第一次读取成功,第二次尝试时出现此异常。

这是所有代码:

public class DictionaryImp extends RemoteServer implements Dictionary {
static FileOutputStream log;
public DictionaryImp(){
super();
}
public String statictics() {
// TODO Auto-generated method stub
try {
setLog("Statistic", this.getClientHost());
} catch (ServerNotActiveException e1) {
// TODO Auto-generated catch block
System.err.println("server is not active!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//some statistic

return msg;
}

public String log() throws RemoteException {

try {
setLog("Log", this.getClientHost());
} catch (ServerNotActiveException e1) {
// TODO Auto-generated catch block
System.err.println("Client is not active!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String msg = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader
(getClass().getClassLoader().getResourceAsStream(this.getClientHost()+".txt")));
String line = null;

while((line = reader.readLine()) != null){
msg += line + "\n";
}
reader.close();
} catch (ServerNotActiveException | IOException e) {
// TODO Auto-generated catch block
System.err.println("Client is not active");
}

return msg;
}

private static void createLogFile(String logFileName){
File logFile = null;
logFile = new File(logFileName);

if(!logFile.exists())
try {
logFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
System.err.println("Log file couldn't created!!");
}
try {
log = new FileOutputStream(logFile, true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.err.println("Log file not found!!");
}
}

private static void setLog(String event, String ip) throws IOException{
createLogFile("Resources/"+ip+".txt");
String time = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(Calendar.getInstance().getTime());
String msg = ip + "\t " +event + "\t " + time + "\n";
try {
log.write(msg.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("Log file not found!!");
}
log.close();
}
}

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at DictionaryImp.search(DictionaryImp.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.search(Unknown Source)
at ClientRMI.calcThroughput(ClientRMI.java:200)
at ClientRMI.main(ClientRMI.java:65)

最佳答案

因为您尝试读取的资源 (dictionary.txt) 不在根类路径中并且正在解析 null

关于java - 输入流读取器空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431392/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com