Programming tips to avoid nullpointerexception in Java

Source: Internet
Author: User







When writing a program, especially when we writeJava
The most common exception isNullpointerexception
Exception. the most depressing thing for a newbie is that after over lines of code are produced and then debug, a bunchNullpointerexception
.


What's most depressing is that this exception is still difficult to find. Simply put, this exception is very concealed!


So what can we do to avoid such exceptions? Where does this exception occur?

 


FirstJava
Is string. In the manual provided by Sun, we can see that string hasNullpointerexception
The exception is thrown. In addition, the exception is thrown by the system for the class we write.

 

Why does this problem occur?


First, the NULL pointer is referenced because when we create an object, the assignment statement is not used to assign the object to another object.Java
To save memory overhead, implicitly direct the object to be assigned a value to the object we want to assign a value to, andJava
No pointer is provided to the user at the top layer, so we feel that our object is assigned a value during program execution.

What does this mean? For example

String test1 = "this is a test ";

Test1 is not a string object. The true object of this string is "This
Is
Test ", this object is placed in a location in the memory, and test1 is used to point to this location, so we can equivalent understanding that test1 is the string object. Note
Live, in most cram schools or professional courses, when talking about the string class, the lecturer should have this introduction, so I will not talk too much here.


Well, since this is a pointer, the problem arises. That is to say, when you receive a value, the value actually does not exist, in other words, if this object does not exist in the memory, your string pointer will not know where it points to the memory, so it willNullpointerexception
Throw, telling the user to check your program code.

 


That is to say, when you use this method to use this class and call the methods in it, the system will throw an exception.

 

A simple example

Public void test (string input ){

System. Out. println (input. Equals (
"Test ");

};

Then, call this method in a method.

Public void check (){

String get = NULL; // warning!

Test (get
);//Nullpointerexception
!!!!

}


If you don't throw an exception in this place, you just need to kill me. The problem lies here. The system is assigned a value or object that does not exist at all (only in your concept but not in memory). Then the system will throw an exception because it cannot find a location!

 

I would like to give some comments to you here

1) when an object may be pointed to a null value
Try-catch operation

 


2) If the first method is too troublesome, try replacing the object's bits. For example, replace the example with "test" in the above example ". equals (input); then the program will not throwNullpointerexception
This exception, of course, looks a little strange.

 

3)
Check the validity of the type you entered. If either of the preceding two is not satisfactory, perform a type check in each place where you receive the input, remember to first check if the pointer is null!


For example, to check whether the target variable of the string type is valid, you must first check

If (target! = NULL
&&! Target. isempty ())

It must be written in this way, rather than if (! Target. isempty ()
& Target! = NULL
In this case, the program preferentially executes the judgment Statement on the left, resulting inNullpointerexception
Occurred !!! Remember this!

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.