有时使用下面的代码片段获取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)
答案 0 :(得分:1)
因为您尝试阅读的资源(dictionary.txt
)不在根路径的类路径中并且正在解析null
答案 1 :(得分:0)
您正在访问无法找到的资源,请尝试使用此代码进行说明:
InputStream is = getClass().getClassLoader().getResourceAsStream("dictionary.txt");
System.out.println(is == null);