Goodbye NullPointerException. Processing of null in Kotlin (KAD 19), kotlinnull

Source: Internet
Author: User

Goodbye NullPointerException. Processing of null in Kotlin (KAD 19), kotlinnull

 

By Antonio Leiva

Time: Apr 4, 2017

Link: https://antonioleiva.com/nullity-kotlin/

 

One of the most important parts of Kotlin: It took me a long time to write an article.

 

The creator of Tony Hoare's "null" concept,Billions of errors". When writing Java code, null is the most error-prone pointer.

 

 

If you are observing the Bug manager, I am sure that the NullPointerException error you have seen will exceed 90%.

 

Thanks to Kotlin, you will work in a safer environment (even using a Java Library), which minimizes these issues.

 

Null of Kotlin

 

Unless you set it, Kotlin does not exist Null.

 

That is to say, the default value of no variable can be setNull. Remember, in Kotlin, all types are objects.

 

 

Therefore, compilation is not allowed:

 

1 val x: Int = null

 

 

To accept Null for a variable, you must add a question mark (?) to the type (?) :

1 val x: Int? = null

 

Compile-time check

 

 

However, from this point, the compiler forces you to check whether the variable isNull. This ensures that NullPointerException does not occur.

 

 

For example:

 

1 val y = x.toDouble()

 

 

If you do not check whether it is null first, you cannot compile it. It must be like this:

1 if (x != null) {2     val y = x.toDouble()3 }

 

Secure Access

 

 

There is an easier way to represent the previous example. When calling a method, add a question mark (?) Before (?).

 

If the variable is not null, it performs the operation. Otherwise, it does not do anything:

1 val y = x?.toDouble()

 

In this case, if x is null, the expression returnsNull. So, y will be Double? Type.

 

Elvis Operator

 

However, what if we do not want a variable that can be empty as the operation result? In this case, the Elvis operator allows us to return a value:

1 val y = x?.toDouble() ?: 0.0

 

This line of code is equivalent:

 

1 val y = if (x != null) {2     x.toDouble()3 } else {4     0.05 }

 

 

Note:

As you can see, most of the statements in Kotlin are converted to expressions. For example, you can assign the if result to the variable.

 

Avoid null check

 

If you are sure that the variable cannot beNull, You can use the operator (!!) This avoids null checks.

 

 

In my opinion,This operator is rarely required.. There are always better solutions.

 

 

However, you can do this:

 

1 val x: Int? = null2 val y = x!!.toDouble()

 

 

This can be compiled and NullPointerException is generated.

 

That's why I said: Be very careful when using this operator.

Java support

 

When we use the Java library, we may find that we have to face differentNullCheck status.

 

Library Annotation

 

 

If the @ Nullable and @ NotNull annotations are properly used in Java and Android, Kotlin can be well connected with them, and,Can indicate when the variable isNullWhen is it not.

 

 

Many parts of the Android framework have been correctly annotated, so this is a huge advantage in combination with Kotlin.

 

Library has no annotation

 

 

However, if there is no annotation in the library, the type will use a special operator (single !) Annotation, which meansThis determines whether a parameter or return value is accepted.Null.

 

 

If we read the source code, we 'd better check the code to determine whether to allow null.

 

In the example where Android does not have annotations, RecyclerView supports libraries. When you create an adapter and a generation method, the default type is ask.

 

However, if you view the source code, you will find that nothing can be null in the method to be overwritten.

 

Conclusion

 

 

For all JavaDeveloper,NullPointerExceptionIt's a nightmare.In most cases, this indicates that your code is incorrect.

 

 

In Kotlin, it is easy to reduce such errors to almost zero, even if the Java framework and library are used.

 

In this way, you can avoid unnecessary debugging for a long time and make the code more stable.

 

 

If you want to learn more about this and be fluent in creating your own Android APP, I suggest you find this free book to learn how to create your first project, or you can directly obtain this book and learn how to create a complete APP from the beginning.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--- Restore content end ---

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.