Project start spring is inexplicably null, null pointer is abnormal, troubleshooting

Before the project, it ran well. As a result, after other related modules updated the code, the project startup reported an error

2019-12-06 08:55:13 [main] ERROR o.s.boot.SpringApplication - Application startup failed
java.lang.NullPointerException: null
	at org.springframework.core.OrderComparator.getOrder(
	at org.springframework.core.OrderComparator.doCompare(
	at org.springframework.core.OrderComparator.access$000(
	at org.springframework.core.OrderComparator$
	at java.util.TimSort.countRunAndMakeAscending(
	at java.util.TimSort.sort(
	at java.util.Arrays.sort(
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(
	at org.springframework.boot.SpringApplication.refresh(
	at org.springframework.boot.SpringApplication.refreshContext(
	at com.newpearl.da.web.DaWebApplication.main(
2019-12-06 08:55:13 [Thread-8] ERROR c.x.j.c.thread.TriggerCallbackThread - null
java.lang.InterruptedException: null
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(
	at java.util.concurrent.LinkedBlockingQueue.take(
	at com.xxl.job.core.thread.TriggerCallbackThread$

It doesn't say where the code exploded. It's just spring's bean acquisition and then order's comparison.

I don't understand. And there are many updated module codes. Do you have any problem finding one by one? What's the matter?

The final way is to debug the code for business trip, but it is not easy to debug the code for spring to allow errors.


How to debug to the place where spring execution fails?

I use a rather stupid method.

First, count the time from the start of spring boot main method to the execution of error reporting, and know about the start of the project

How long does it take to report an error.  

After that, when initializing a bean, create a new thread to run,

See when the thread will execute before an error is reported.  

Then debug and set the breakpoint. And then it breaks the code that spring executes

For example, when the program starts to start, if the error is reported after 30S, the subthread will sleep for 30S, and then it can enter the breakpoint to debug

Then debug the spring code

Debugging here will take a lot of time and effort, because only when o1 is empty will an error be reported.

So you need to press F9 to debug until o1 is found to be empty

There's no way around here. I know how many times I press F9

Finally, when o1 is empty, the execution stack on the left side of idea debug is used here.

Found initializing bean


The next step is to find two objects to compare. One is bean addCouponCodeTask,

The other is


Found addCouponCodeTask bean is null

public class AddCouponCodeTask extends Thread {

Other colleagues wrote... ,


In Spring projects, it is sometimes necessary to start a new Thread to complete some complex tasks, while some services may need to be injected into the Thread. It is a reasonable way to manage and use services through Spring injection. However, it is invalid to inject Bean directly through annotation in the Thread subclass.

Because Spring's default bean is built in the singleton mode and is non Thread safe, the injection behavior in the Thread subclass is prohibited. Therefore, the bean directly injected into the Thread is null and a null pointer error will occur.

Well, I know I can't write like that.

And cloudSolrClient is empty, I know. In order to prevent error reporting when I write, I set it to null.

Because cloudSolrClient is injected into other module codes, but solr is not used in the whole project, so it is empty

   @Bean(name = "cloudSolrClient")
    public CloudSolrClient setSolrClientFactory() {

            return null;

Comment out all the code of "addCouponCodeTask". The startup is successful.

Or return a new object with the cloudSolrClient code by default. It can also be started successfully.

In other words, when spring initializes the bean, it will sort and compare other beans that the bean depends on,

This problem occurs when the orderComparator performs the comparison and both are null.




1. The subclass of thread cannot be injected into spring, otherwise it is null

2. When writing code, try not to initialize and assign a null value to the bean, otherwise there may be an unexplained hole of null pointer

Keywords: Programming Java Spring solr

Added by andymike07 on Sat, 07 Dec 2019 00:40:16 +0200