Today, I made a long mistake at the company. It may be due to my carelessness.
Segment firstCode:
String id = Params. Get ("ID") + "";
If (null = ID | "". Equals (Id. Trim ())){
// When ID is null
..................
} Else {
...................
}
Let's see what is wrong with this code. I couldn't think of it for a long time. However, when ID is null, it always enters the else block. Where is the error of IF (null = ID | "". Equals (Id. Trim?
In fact, the code is correct, and the error lies in the details. I return an ID from the background from the page. The ID is placed in map, Map <string, Object> Params. So I performed this operation: String id = Params. get ("ID") + ""; after obtaining the ID, add "" as a conversion, if you use string id = (string) Params. get ("ID") may not happen.
Well, back to this story: the front-end sometimes does not pass the ID back, that is, Params. Get ("ID") may get a null value. I have already processed the null value and the empty string. Why is it not the result I want. At this time, I debug and print the output in the console: But sadly, when you see the variable in debug, the Null Output is also null. The problem persists.
Result: In fact, it was found that null + "" = "null" is the ID = "null"; if (null = ID | "". equals (ID. trim () is false, and the output from the console is null. However, the debug variable is "null", which was my carelessness and was discovered later.
Conclusion: If you like to directly convert the double quotation marks to the string type, you should pay more attention to it. This is a fatal error when writing the code.