If (null = ID | ". Equals (Id. Trim () Where is the error?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.