java null pointer exception: the root of the problem and the solution

@Java null pointer exception Exception in thread "main" java.lang.NullPointerException In the previous writing experimen...
Problems encountered
Solution
View source code
summary

@Java null pointer exception
Exception in thread "main" java.lang.NullPointerException
In the previous writing experiment, we encountered a java null pointer exception problem. After multiple queries of data, China and Vietnam solved the exception, so we decided to record the solution of this exception.

Problems encountered

The basic idea of the program is to read the file, separate the words one by one, encapsulate the words into Word class, then judge the type of words, and write the type and the Word itself into the output file. But this sentence

if (words[i].word !=null) {

There was a null pointer exception. I debugged it many times, but I couldn't find the source of the problem.

Solution

When searching for problems, I suddenly saw this article, which gave me inspiration
https://www.sohu.com/a/208264500_611601
In the article

This sentence made me suddenly realize whether it was because my words[i] was a null object, so I modified the code to

if (words[i]!=null&&words[i].word !=null) {

That is to say, first judge whether the object of words[i] is empty, and then judge the properties of words[i]. After running, it is solved successfully.

View source code

In line with the idea of tracing the origin, I want to look at the corresponding source code and see what else may cause null pointer. The source code of NullPointerException is as follows:

/* * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package java.lang; /** * Thrown when an application attempts to use {@code null} in a * case where an object is required. These include: * <ul> * <li>Calling the instance method of a {@code null} object. * <li>Accessing or modifying the field of a {@code null} object. * <li>Taking the length of {@code null} as if it were an array. * <li>Accessing or modifying the slots of {@code null} as if it * were an array. * <li>Throwing {@code null} as if it were a {@code Throwable} * value. * </ul> * <p> * Applications should throw instances of this class to indicate * other illegal uses of the {@code null} object. * * {@code NullPointerException} objects may be constructed by the * virtual machine as if {@linkplain Throwable#Throwable(String, * Throwable, boolean, boolean) suppression were disabled and/or the * stack trace was not writable}. * * @author unascribed * @since JDK1.0 */ public class NullPointerException extends RuntimeException { private static final long serialVersionUID = 5162710183389028792L; /** * Constructs a {@code NullPointerException} with no detail message. */ public NullPointerException() { super(); } /** * Constructs a {@code NullPointerException} with the specified * detail message. * * @param s the detail message. */ public NullPointerException(String s) { super(s); } }

among

1. <ul> 2. <li>Calling the instance method of a {@code null} object. 3. <li>Accessing or modifying the field of a {@code null} object. 4. <li>Taking the length of {@code null} as if it were an array. 5. <li>Accessing or modifying the slots of {@code null} as if it 6. were an array. 7. <li>Throwing {@code null} as if it were a {@code Throwable} 8. value. 9. </ul>

It is not hard to see that when the above five conditions are met, a null pointer exception will be reported.
It can be roughly translated into:

1. Call an instance of an empty object
2. Access or modify empty objects
3. Test the length of an empty object array
4. Access or modify the slot of an empty array? Limited level, don't understand slots)
5. Take a null value as a thrown parameter

In other words, to avoid null pointer exceptions, you need to avoid the above five situations. Null pointer exceptions can be avoided in many code specifications. For example, in the coding specification of alibaba, when calling string1.equals(string2), if string2 is a specific string and string1 is an object property, then you should reverse euqal to string2.euqals(string1)
For example, when you install the encoding specification in idea, you write it in the form of a null pointer. Idea will remind you

At this time, it should be changed to

"true".equals(resultItems.get("type")

This avoids problems that can cause null pointers.

summary

  1. When encountering problems, don't panic, check more and think more. Most of the problems that can be encountered should have been encountered by predecessors, so we should check more, find mistakes and learn from experience.

  2. Take a look at the source code. There are instructions in many abnormal source codes.

  3. java source code link: Source code

14 June 2020, 05:25 | Views: 2922

Add new comment

For adding a comment, please log in
or create account

0 comments