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.beans.factory.support.DefaultListableBeanFactory$FactoryAwareOrderSourceProvider.getOrderSource(DefaultListableBeanFactory.java:1742)
	at org.springframework.core.OrderComparator.getOrder(OrderComparator.java:102)
	at org.springframework.core.OrderComparator.doCompare(OrderComparator.java:87)
	at org.springframework.core.OrderComparator.access$000(OrderComparator.java:48)
	at org.springframework.core.OrderComparator$1.compare(OrderComparator.java:66)
	at java.util.TimSort.countRunAndMakeAscending(TimSort.java:360)
	at java.util.TimSort.sort(TimSort.java:234)
	at java.util.Arrays.sort(Arrays.java:1438)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1171)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1096)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
	at com.newpearl.da.web.DaWebApplication.main(DaWebApplication.java:27)
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(AbstractQueuedSynchronizer.java:2014)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2048)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at com.xxl.job.core.thread.TriggerCallbackThread$1.run(TriggerCallbackThread.java:65)

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

AddCouponCodeTask

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

The other is

cloudSolrClient

Found addCouponCodeTask bean is null

@Deprecated
@Component
public class AddCouponCodeTask extends Thread {

Other colleagues wrote... ,

Reference https://blog.csdn.net/think12/article/details/80078563

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.

 

 

summary

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