簡體   English   中英

NullPointerException在Spring-boot中,如何解決?

[英]NullPointerException In Spring-boot and how do I fix it?

今天,我在spring-boot項目中遇到了一個錯誤。 在我的代碼中,我想獲取ApplicationContext ,但是它為null ,所以我不能使用getBean()

這是用於配置的Application.java

@SpringBootApplication
@EnableAspectJAutoProxy
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    @Bean
    public ServletRegistrationBean readHitchEventServletBean() {

        ServletRegistrationBean readHitchEventServletBean = 
                new ServletRegistrationBean();
        readHitchEventServletBean.setServlet(new ReadHitchEventServlet());
        readHitchEventServletBean.setLoadOnStartup(5);
        return readHitchEventServletBean;
    }

    public static void main(String[] args) throws Exception {

        SpringApplication.run(Application.class, args);
    }
}

然后是servlet:

@Component
public class ReadHitchEventServlet extends HttpServlet implements ApplicationContextAware {

    private static final long serialVersionUID = 1L;
    private static ApplicationContext applicationContext;

    @SuppressWarnings("static-access")
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {
    // TODO Auto-generated method stub
        this.applicationContext = applicationContext; 
    }

    public static ApplicationContext getApplicationContext() {  
        return applicationContext;  
    } 

    public static Object getBean(String name) throws BeansException {  
        return applicationContext.getBean(name);  
    }

    @Override
    public void init() {
        getBean("heheda");
    }
}

我收到以下錯誤:

java.lang.NullPointerException: null
at com.gdut.dongjun.ActiveSwitchThread.getBean(ReadHitchEventServlet.java:126) ~[classes/:na]
at com.gdut.dongjun.ActiveSwitchThread.<init>(ReadHitchEventServlet.java:66) ~[classes/:na]
at com.gdut.dongjun.ReadHitchEventServlet.init(ReadHitchEventServlet.java:56) ~[classes/:na]
at javax.servlet.GenericServlet.init(GenericServlet.java:158) ~[tomcat-embed-core-8.0.23.jar:8.0.23]
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231) [tomcat-embed-core-8.0.23.jar:8.0.23]
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1034) [tomcat-embed-core-8.0.23.jar:8.0.23]
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4914) [tomcat-embed-core-8.0.23.jar:8.0.23]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.deferredLoadOnStartup(TomcatEmbeddedContext.java:66) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.startConnector(TomcatEmbeddedServletContainer.java:209) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:152) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:483) [spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java) [spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java) [spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at com.gdut.dongjun.Application.main(Application.java:282) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_79]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_79]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79]
at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418) [spring-boot-maven-plugin-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at java.lang.Thread.run(Thread.java:745) [na:na]

我如何解決它?

最好有一個單獨的類來實現ApplicationContextAware,以向所需的類提供應用程序上下文。 這里的問題似乎是Spean bean生命周期和servlet容器生命周期執行不同的操作。

也就是說,可以在春季之前設置上下文之前調用init。

在線:

readHitchEventServletBean.setServlet(new ReadHitchEventServlet());

您正在創建不是Spring Bean的servlet對象的新實例,因此無法正確連接它。 如果您要這樣做,請考慮執行以下操作:

Application.java:

@Bean
public ServletRegistrationBean readHitchEventServletBean(ApplicationContext applicationContext) {
    ServletRegistrationBean readHitchEventServletBean = new ServletRegistrationBean();
    readHitchEventServletBean.setServlet(new ReadHitchEventServlet(applicationContext));
    readHitchEventServletBean.setLoadOnStartup(5);
    return readHitchEventServletBean;
}

ReadHitchEventServlet.java:

public class ReadHitchEventServlet extends HttpServlet {
    private final ApplicationContext applicationContext;

    ReadHitchEventServlet(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void init() {
        applicationContext.getBean("heheda");
    }
}

甚至更好的方法是完全不使用ApplicationContext並僅連接您最終想要的bean。

Application.java:

@Bean
public ServletRegistrationBean readHitchEventServletBean(Heheda heheda) {
    ServletRegistrationBean readHitchEventServletBean = new ServletRegistrationBean();
    readHitchEventServletBean.setServlet(new ReadHitchEventServlet(heheda));
    readHitchEventServletBean.setLoadOnStartup(5);
    return readHitchEventServletBean;
}

ReadHitchEventServlet.java:

public class ReadHitchEventServlet extends HttpServlet {
    private final Heheda heheda;

    ReadHitchEventServlet(Heheda heheda) {
        this.heheda = heheda;
    }

    @Override
    public void init() {
        // do something with heheda
    }
}
public List<ISurvey> getActiveSurveysWithRespondents(String userId) throws MCBusinessException {
        List<ISurvey> surveyList = null;
        List<ISurvey> activeSurveys = surveyRepo.getActiveSurveysList(userId, OTellerConstants.Pollster_Active_Survey);
        if (activeSurveys != null && !activeSurveys.isEmpty()) {
            Comparator<ISurvey> compareByDate = (ISurvey o1, ISurvey o2) -> (o1.getCreatedDate())
                    .compareTo(o2.getCreatedDate());
            activeSurveys.sort(compareByDate.reversed());
            Comparator<ISurvey> compareByTime = (ISurvey o1, ISurvey o2) -> (o1.getLastChgTs())
                    .compareTo(o2.getLastChgTs());
            activeSurveys.sort(compareByTime.reversed());
            surveyList = new ArrayList<>();
            ISurvey surveyD = null;
            if (!CollectionUtils.isEmpty(activeSurveys)) {
                for (ISurvey survey : activeSurveys) {
                    if (survey.getStatus() == OTellerConstants.Live_Survey_status) {
                        Integer taggedRespondents = surveyRepo.getRespondents(survey.getId());
                        Integer answeredRespondents = surveyRepo.getAnsweredRespondents(survey.getId());
                        surveyD = new SurveyD();
                        surveyD.setId(survey.getId());
                        surveyD.setSurveyName(survey.getSurveyName());
                        surveyD.setStatus(survey.getStatus());
                        BeanUtils.copyProperties(survey, surveyD);
                        surveyD.setRespondentCount(taggedRespondents);
                        surveyD.setAnsweredRespondent(answeredRespondents);
                        surveyList.add(surveyD);
                    } else {
                        surveyD = new SurveyD();
                        surveyD.setId(survey.getId());
                        surveyD.setSurveyName(survey.getSurveyName());
                        surveyD.setStatus(survey.getStatus());
                        BeanUtils.copyProperties(survey, surveyD);
                        surveyList.add(surveyD);
                    }
                }

我getnng null如何解決?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM