preface
It's a little interesting to change someone else's bug.
problem
Let's start with a code:
public static void main(String[] args) { String sourceStr = ""; JSONObject jsonObject = JSONObject.parseObject(sourceStr); System.out.println(jsonObject); }
Q:
- When sourceStr is null, will null pointer exception be reported?
- Will parsing exceptions be reported when sourceStr is an empty string?
- What error will be reported when sourceStr is "null"?
code
public static void main(String[] args) { String sourceStr = null; JSONObject jsonObject = JSONObject.parseObject(sourceStr); System.out.println(jsonObject); sourceStr = ""; jsonObject = JSONObject.parseObject(sourceStr); System.out.println(jsonObject); sourceStr = "null"; jsonObject = JSONObject.parseObject(sourceStr); System.out.println(jsonObject); }
Output result of the above code:
null null null
summary
The reason for this problem is that a colleague used if (str == null) when judging the null of the string, but the incoming STR is an empty string '', so a null pointer appears later when operating on the parsed jsonObject. In fact, there are tool classes such as StringUtils in many packages, including isEmpty() method, which integrates (str == null) and str.length = 0. It is generally recommended to use this empty judgment method. Of course, it should also be combined with specific business scenarios.
Postscript
In fact, I spent a lot of time looking for this problem, because there are some twists and turns...
First post the general code:
class JsonUtils { private static final Logger log = LoggerFactory.getLogger(JsonUtils.class); public void test(String sourceStr) { log.info("sourceStr: {}", sourceStr); if (sourceStr == null) { return; } JSONObject jsonObject = JSONObject.parseObject(sourceStr); System.out.println(jsonObject.getString("a")); } }
From the above code, we can see that sourceStr is a parameter. We don't know what it is, but there is a log record. At the same time, this log makes me go a long detour.
The general process is as follows:
--Found jsonObject Null pointer exception in getString ("a"), guess jsonObject is null;
--Immediately asked the colleague in charge of this area to see what sourceStr printed in the log;
--The print of the colleague's reply is: sourceStr: null;
--I began to wonder. If sourceStr is null, it will directly return and will not go to the place of the null pointer below;
--At this time, my little head melon seeds didn't know where I thought, and suddenly felt that what came in was the string "null"?
--I immediately wrote code to verify my guess:
public static void main(String[] args) { JsonUtils.test("null"); }
The result surprised me a little:
sourceStr: null java.lang.NullPointerException
It should be noted that when the log is printed, the results of null and string "null" are the same.
--Is this really the reason??
--Later, it was confirmed with relevant parties that it was impossible to pass in a string "null", and there was an impasse here
--So I went to the corresponding log myself and found...... Tell me that the colleague whose sourceStr is null read the wrong log at the wrong time..., When he saw that sourceStr was null, there was no null pointer exception at all. When null pointer exception occurs, the passed in sourceStr is null string ''
--Problem solving.