I’m using the properties -D method to give arguments to a jar file on the command line. Here’s the code:
String prop1 = "one";
String val1 = System.getProperty(prop1);
System.out.println(val1);
This works, but when I try to implement this in a larger program, strange things happen. Without arguments, this will print null, which is fine. But if I then do something like boolean test = val1.equals(null); , I get problems that don’t seem to relate to the code I’ve provided. Any thoughts?
You are encoutering a
NullPointerExceptionbecause you try to evaluate a method on a null reference. If val1 is null, you cannot invoke a method on it. If you want to test for null, you should test with==.or