Avoid NullPointerException when trying to get an exception message
Wile going over our parser logs I found this interesting bit:
2023-12-16 11:50:51,346 WARN o.t.m.d.u.DateTimeHelper:149 VoteParser run().
java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
at java.base/java.util.Formatter.parse(Formatter.java:2717)
at java.base/java.util.Formatter.format(Formatter.java:2671)
at java.base/java.util.Formatter.format(Formatter.java:2625)
at java.base/java.lang.String.format(String.java:4145)
at org.torproject.metrics.descriptorparser.parsers.VoteParser.addNetworkStatusVote(VoteParser.java:226)
at org.torproject.metrics.descriptorparser.parsers.VoteParser.run(VoteParser.java:85)
at org.torproject.metrics.descriptorparser.Main.run(Main.java:147)
at org.torproject.metrics.descriptorparser.Main.exec(Main.java:36)
at org.torproject.metrics.descriptorparser.Main.main(Main.java:32)
I guess what that means is we should be a bit more careful with blocks like
} catch (Exception ex) {
logger.warn("Exception. {}".format(ex.getMessage()));
ex.printStackTrace();
}
making sure we only try to get a message if it is not null
(NullPointerException
s have no messages, for instance, IIRC).
Edited by Georg Koppen