Xiaobai, take a look! Help you easily solve Java's null pointer exception

Follow the "Java Architecture Stack" WeChat official account and reply with the password [Java Interview Questions] to get the interview questions from big companies

 Exceptional cases

For many Java beginners, it is easy to encounter various exceptions in the early stages of learning, such as the null pointer exception we are going to talk about today. As the saying goes, "teaching a man to fish is worse than teaching him to fish." Bogo will explain it to you today with a practical case, so that you can truly understand the cause of the abnormality and become proficient in its solution process. Let’s first take a look at the following code example:

public class Test {
    public static void main(String[] args) {
        String str=null;
        if(str.equals("qianfeng")){
            System.out.println("没问题,相等");
         }else {
            System.out.println("有问题,不相等");
         }
    }
 }

If we run the above code, the following exception will appear:

 Exception in thread "main" java.lang.NullPointerException //空指针异常
        at Test.main(Test.java:4)

 Abnormal

So how did the above exception occur? How to solve it? For beginners, this may be really difficult to start.

In fact, it is not difficult to solve this kind of exception, but many beginners usually do not check the exception information when they encounter an exception, but plan to check the code directly to find out the cause. Of course, this is fine if you have very little code. But this will develop a bad habit, that is, in the subsequent process of writing code, you will often ignore checking the exception information. If you still search for exceptions directly in the code when there is a lot of code, it will be extremely inefficient, and you may not be able to find the cause of the exception.

In fact, the correct way to solve exceptions is to first check the exception information on the console. For example, the NullPointerException exception prompt appears in the console in this case, and the console also tells us the specific location where the exception occurs:

at Test.main(Test.java:4)

So as long as everyone carefully checks this exception information, we can determine from this line of exception information that a null pointer exception occurred in the 4th line of the Test class, then we only need to directly check the 4th line of code:

if(str.equals("qianfeng"))

In other words, a null pointer exception occurred when executing str.equals("qianfeng") in the if condition. We only need to carefully observe this line of code to see which object is empty. Through careful inspection, we found that the str object was empty and we forgot to assign a value to the object, so next we made a little change, the code is as follows:

public class Test {
    public static void main(String[] args) {
        String str=null;
        if("qianfeng".equals(str)){
             System.out.println("没问题,相等");
         }else {
             System.out.println("有问题,不相等");
         }
    }
 }

In addition, everyone should note that when we use the equals() method, we must follow the principle of "constants first and variables last", otherwise a null pointer exception will easily occur.

 summary

Through the above case, we will find that in the process of learning, it is not scary to encounter problems. The most important thing is to form your own set of ideas for analyzing and solving problems! In addition, children with weak basic knowledge can also check information online to consolidate their learning. For example, you can find many free Java learning materials on the official website of Qianfeng Education. Of course, you can also search for Qianfeng Java on site B, and you can also find many free Java learning videos.

Finally, if you have any other questions, just leave me a message in the comment area, and Brother Bo will answer it for you in time. Pay attention to the Java architecture stack, and you will get useful information every day.

Guess you like

Origin blog.csdn.net/weixin_41692221/article/details/131304756