Atitit avoids NULL pointer exception solution, while atitit avoids pointer

Source: Internet
Author: User

Atitit avoids NULL pointer exception solution, while atitit avoids pointer

Atitit solution to avoid NULL pointer exceptions

 

 

1. Null Question 1

2. Force distinguish between common references vs void references vs forced references, or non-empty references 2

3 .? Operator (question mark operator )! Exclamation mark operator to avoid NULL pointer exceptions, 2

4. Optional Class 2 in Java 8

4.1.1. Why is Optional better than common null checks? 3

5. Another savior! Objects. requireNonNull3

5.1.1. Why is it worse than if (myObj! = Null) better? 3

6. Refer to 4

 

1. Null

A programming language containing null references is an error worth billions of dollars (Note: Tony Hoare, winner of the Turing Award, said ). But why? Of course, they may cause NullReferenceException, But what then? Any element of a language can cause errors if it is improperly used.

In other words, there are two cases where an empty statement is returned:

· The Return Value of null is a normal return value according to the search convention.

· The Return Value of null is not a normal return value.

The second case is simple. You can use assert to determine or allow the program to report an error (that is, throwing NullPointerException ). Assertion is a fully-utilized Java feature. This feature is added to analyticdb 1.4. Syntax:

The main problem with NullPointerException is that there is no biz description.

It is much better to explain an exception than to throw an NullPointerException.

Author: nickname: old wow's paw (full name: Attilax Akbar Al Rapanui Attila Akba Arla Panui) Name: AI long, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://www.cnblogs.com/attilax/

 

 

2. Forcibly differentiate between common references and void references. 3 .? Operator (question mark operator )! The exclamation mark operator avoids NULL pointer exceptions,

The root cause is that C # cannot express the concept of non-null reference, which also makes it a heavy task for the compiler to force a null check.

To address this issue, we recommend that you use a forced reference and a clear definition of a null reference. In the proposal, can empty references be used? The suffix is defined, just as the null type can be defined. And force reference, or non-empty reference will be used! Suffix.

Both forced reference and null reference should be considered a concept limited to the language itself. They only change the behavior of the compiler, but do not change the generated IL code.

Before the compiler allows access to any methods or attributes of an empty reference object, you must explicitly check the empty reference. In addition, you must check empty references before converting an empty reference to a forced reference.

Forced reference requires the compiler to confirm that the included value cannot be null. Because this is a rule that is limited to compilers, it cannot be guaranteed to take effect in deserialization and other scenarios.

 

When you read the complete content of this proposal, you will notice a term "general reference" mentioned in it ". It refers to a common reference in C #. It is neither mandatory nor clearly defined as void. Since such a reference is considered as a legacy code, you can use the AllowGeneralReferences attribute to tell the compiler that general references are not allowed in the code.

When combined with implicit variable definition, you can use it in the var keyword! Or? Suffix.

 

 

4. Optional Class in Java 8

The simple method is to check whether the Optional package really has a value (using the isPresent method) -- you will doubt this and use if (myObj! = Null. Don't worry, I will explain it clearly.

You can use the orElse method to return a default value if the encapsulation is actually a null value. The benefits of this method are obvious. When extracting the actual value, you can avoid calling the ifPresent method, which is obviously redundant.

4.1.1. Why is Optional better than common null checks?

· The biggest advantage of using Optional is that you can express your intention more clearly-If null is returned, the consumer will be confused (when NPE is actually returned) if it is intentionally returned, therefore, you must check javadoc for further positioning. Optional is quite clear.

· With Optional, you can completely avoid NPE-as mentioned above, using Optional. ofNullable, orElse and orElseGet can keep us away from NPE.

5. Another savior! Objects. requireNonNull

If NPE is thrown, how can we determine which one is null?

Objects. requireNonNull (key, "Key is null ");

 

RequireNonNull Method

· If the object is not null, it is returned.

· If the value is null, the returned NPE will contain the specified message

5.1.1. Why is it worse than if (myObj! = Null) better?

The stack trace information you see clearly shows the method call of Objects. requireNonNull. This works with your own error log to help you locate the problem more quickly... At least it seems faster to me.

6. Reference

Empty pointer savior-blog-Bole on-line .htm

 

How to Avoid "! = Null? -ImportNew.htm

C # Future: Tracking empty reference .htm

Related Article

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.