gpt4 book ai didi

java - 抛出不可能的 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 02:02:02 26 4
gpt4 key购买 nike

在几年前编写的一个网络服务器项目中,我偶尔会遇到一个非常奇怪且在我看来不可能的NullPointerException。它发生在用于在控制台上记录输出的实用程序方法中。

这是该方法的错误摘录:

try {
Encoder encoder = Base64.getEncoder();
if(logWriter != null) {
logWriter.write(String.valueOf(System.currentTimeMillis()));
logWriter.write(" ");
logWriter.write(String.valueOf(level));
logWriter.write(" ");
logWriter.write(encoder.encodeToString(Thread.currentThread().getName().getBytes()).replaceAll("(?:\\r\\n|\\n\\r|\\n|\\r)", ""));
logWriter.write(" ");
logWriter.write(encoder.encodeToString(log.getBytes()).replaceAll("(?:\\r\\n|\\n\\r|\\n|\\r)", ""));
logWriter.write("\r\n");
logWriter.flush();
}

lastWriterActivity = System.currentTimeMillis();
} catch (IOException e) {
println("Failed to write log to file: " + e, Color.RED);

try {
logWriter.close();
} catch (IOException e1) {
println("### Possible resource leak; unable to close log writer", Color.RED);
}

logWriter = null;
}

其中logWriter是一个BufferedWriter。 NPE 在调用 logWriter.close() 的第一个 catch block 中抛出。

但是:当我的 logWriter 为 != null 时,如何在我的 try block 中抛出 IOExceptionBase64.getEncoder() 无法抛出 IOException 并且没有其他代码要执行。

这是我的堆栈跟踪:

Exception in thread "connection_0:0:0:0:0:0:0:1@1544725509" java.lang.NullPointerException
at org.jpuzzle.main.Logger.write(Logger.java:347)
at org.jpuzzle.main.Logger.verbose(Logger.java:187)
at org.jpuzzle.protocol.http.HttpRequest.onRequest(HttpRequest.java:1090)
at org.jpuzzle.network.ConnectionListener$Connection.proceed(ConnectionListener.java:438)
at org.jpuzzle.network.ConnectionListener$Connection.run(ConnectionListener.java:408)

我的方法是同步,因此互斥应该不会造成任何困难,而且我不知道为什么会发生这种情况。

最佳答案

简短回答:

重写您的 catch block 以避免 NullPointerException

try {
if(logWriter != null) {
logWriter.close();
}
} catch (IOException e1) {
println("### Possible resource leak; unable to close log writer", Color.RED);
}

长答案:

Yes, logWriter is static.... Doesn't synchronized prevent parralel method execution for a method?

没有。

需要证明吗?

运行此代码:

public class NowImFeelingZombified {
static Object logWriter = new Object();

public static void main(String[] args) {
final NowImFeelingZombified zombie1 = new NowImFeelingZombified();
final NowImFeelingZombified zombie2 = new NowImFeelingZombified();

Thread t1 = new Thread("zombie1 ") {
@Override
public void run() {
zombie1.syncedMethod();
}
};
Thread t2 = new Thread("zombie2 ") {
@Override
public void run() {
zombie2.syncedMethod();
}
};
t1.start();
t2.start();
}

private synchronized void syncedMethod() {
try {
System.out.println(Thread.currentThread().getName() + logWriter.toString());

Thread.sleep(2000L);

System.out.println(Thread.currentThread().getName() + logWriter.toString());

logWriter = null;
}
catch (InterruptedException e) {
}
}
}

输出是这样的:

zombie1 java.lang.Object@1c34796b
zombie2 java.lang.Object@1c34796b
zombie1 java.lang.Object@1c34796b
Exception in thread "zombie2 " java.lang.NullPointerException
at NowImFeelingZombified.syncedMethod(NowImFeelingZombified.java:44)

怎么样?

同步方法会获取方法调用者的锁。这意味着 t1 不关心 t2 (当然也不关心 logWriter)。

就是这样。阅读此处了解 full story .

此外,很久以前,我为我一直使用的 Util 类编写了这样的 5 行代码。它使我无需编写用于关闭 Closeables

catch block
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
}
catch (IOException e) {
//logging
}
}
}

不管怎样,Java 1.7已经推出了AutoCloseable (由 BufferedWriter 实现)您可能想探索一下。

关于java - 抛出不可能的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535064/

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