Tag Archives: java.lang.NullPointerException

[Solved] Springboot test class injection bean is null Error: java.lang.NullPointerException…

When the springboot test class injects a NullPointerException object:

Normally, the test class add @springboottest can be tested normally

However, the value of classes in @ springboottest (classes = bootapplication. Class) can inject properties normally only after the class is started. That is, bootapplication is the springboot startup class

@RunWith(SpringRunner.class)

@SpringBootTest(classes = GrouptwoApplication.class)

How to Solve JAVA Test Class Error: java.lang.nullpointerexception

 

1. Error information:

IDEA:

Java test class java.lang.nullpointerexception

2. Error reporting reason:

Without annotation, the unit test cannot call other classes in the project.

3. Error reporting solution:

These two annotations should be used above the test class:

@RunWith(SpringJUnit4ClassRunner.class)

  @SpringBootTest

Android room and java.lang.nullpointerexception and observeasstate

val itemList: List<Message>?by messageViewModel.messageList.observeAsState()

MessageWindow(itemList = itemList!!)

The error of null pointer is caused by the problem of observeasstate function,

There are two versions of the obserasstate function. One is to give an initial value, and the other is to give no initial value, that is, the version I reported an error.

Because there is no initial value, it will return null!

 

  Because my itemlist starts the background thread to query in the ViewModel, and the main thread runs faster than the background thread, when the main thread runs to the observer line, the data is not ready, the result returns null, and the result conflicts with the following line.

 

The solution is to use another version of the obser function to give an initial value so that a null error will not be reported.

val itemList: List<Message>?by messageViewModel.messageList.observeAsState(listOf())

 

Several cases of error reporting in java.lang.nullpointerexception

Several cases of java.lang.nullpointerexception error reporting:

1. String variable is not initialized

2. Interface type objects are not initialized with specific classes, such as:

  List stuList ; In this case, a null pointer exception will be reported

  List stuList = new ArrayList(); After initialization, no error will be reported

3. When the value of an object is null and you do not judge it as null, a null pointer exception will also be reported

Therefore, add a judgment before the code, such as:

    if(ObjId != null);

For string type objects, you can make the following judgment:

    if(objectStr !== null && amp; !“”. equals(objectStr))

Of course, you can also judge whether it is not an empty string:

    if(objectStr !== null && amp; !“”. equals(objectStr.trim()))

 

Example: when I edit a page, it will pop up a custom error.jsp page code block (reporting system error)

Check the log: (an error is reported on line 54 — the picture is badly cut)

Because doc.getid does not make judgment, null pointer exception is reported when ID does not exist… (for case 3)