我的Spring Boot Web应用程序有问题。
我有MainContorller类和CheckerClass。
它说到最后是java.lang.NullPointerexception,但是我不确定该如何处理。

MainController类:

package danilocongradac.webApp;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {
    private static CheckerClass checker = new CheckerClass();

    @GetMapping(value = {"/", "/home"})
    public static String mainPage() {
        return "index";
    }
    @GetMapping(value = {"/error"})
    public static String errorPage() {
        return "error";
    }

    @GetMapping(value = {"/signIn"})
    public static String inputPage() {
        return "signIn";
    }
    @GetMapping(value = {"output"})
    public static String outputPage(){
        return "output";
    }

    @RequestMapping(value = {"/inputCheck"})
    public static String inputPageCheck(HttpServletRequest req,Model m){
        String email = req.getParameter("email");
        String password = req.getParameter("password");

        System.out.println("email: " + email);
        System.out.println("password: " + password);

        // checker
        if(checker.check(email, password))
            return "output";
        return "input";
    }
}


和CheckerClass:

package danilocongradac.webApp;

import java.util.ArrayList;

public class CheckerClass {
    private static ArrayList<User> users;

    public CheckerClass(){
        users.add(new User("danilo", "congradac", "[email protected]", "danilo2002"));
    }
    public void addUser(String name, String surname, String email, String password){
        users.add(new User(name, surname, email, password));
    }
    public boolean check(String email, String password) {
        users.add(new User("danilo", "congradac", "[email protected]", "danilo2002"));
        for(User u : users) {
            if(email.equals(u.getEmail()) && password.equals(u.getPassword()))
                return true;
        }
        return false;
    }
}



我收到这样的错误消息...
错误消息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController' defined in file [/home/danilocongradac/Documents/java/webApp/target/classes/danilocongradac/webApp/MainController.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at danilocongradac.webApp.App.main(App.java:12) [classes/:na]
Caused by: java.lang.ExceptionInInitializerError: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_242]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_242]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:200) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
... 17 common frames omitted
Caused by: java.lang.NullPointerException: null at danilocongradac.webApp.CheckerClass.<init>(CheckerClass.java:9) [classes/:na] at danilocongradac.webApp.MainController.<clinit>(MainController.java:12) ~[classes/:na]
... 24 common frames omitted

最佳答案

CheckerClass类中,users变量未初始化。在构造函数中,请在使用前对其进行初始化。

public CheckerClass(){
    users= new ArrayList<User>();
    users.add(new User("danilo", "congradac", "[email protected]", "danilo2002"));
}

09-30 18:50