我正在尝试使用 Spring Boot 制作登录页面。我有三类用户:学生、学院、公司。当我检查用户是否存在时,以下代码对学生工作正常,但对其他两个人不起作用。它为 servlet [dispatcherServlet] 在路径 [] 的上下文中提供了 NullPointerException 和消息 Servlet.service() 抛出异常 [请求处理失败;嵌套异常是 java.lang.NullPointerException: 无法调用“com.springboot.controller.repository.CollegeRepo.findByEmail(String)”,因为“this.clgrepo”为 null] 其根本原因 我在数据库中为三个类别有三个不同的表。如果我做错了什么,请告诉我。 以下是我的代码:
主课
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EntityScan(basePackages="com.springboot.controller.model")
@EnableJpaRepositories(basePackages="com.springboot.controller.repository")
@SpringBootApplication
public class CunsultustodayWebServicesApplication {
public static void main(String[] args) {
SpringApplication.run(CunsultustodayWebServicesApplication.class, args);
}
}
LoginController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.springboot.controller.model.CollegeReg;
import com.springboot.controller.model.CompanyReg;
import com.springboot.controller.model.User;
import com.springboot.controller.repository.CollegeRepo;
import com.springboot.controller.repository.CompanyRepo;
import com.springboot.controller.repository.UserRepo;
import com.springboot.controller.services.StudentService;
@RestController
public class LoginController {
@Autowired(required=true)
private StudentRepo stdrepo;
private CollegeRepo clgrepo;
private CompanyRepo cmprepo;
@RequestMapping("/")
public ModelAndView checkMVC()
{
ModelAndView mav= new ModelAndView("Login");
return mav;
}
@RequestMapping("/login")
public ModelAndView loginHome(@RequestParam(value="email" ,required=true) String email, @RequestParam(value="password", required=true) String password, Model model)
{
StudentReg u= null;
u= stdrepo.findByEmail(email);
if(u!=null) {
model.addAttribute("email", email);
ModelAndView mav=new ModelAndView("HomePage");
return mav;
}
else {
CollegeReg c=null;
c=clgrepo.findByEmail(email);
if(c!=null) {
model.addAttribute("email", email);
ModelAndView mav=new ModelAndView("HomePage");
return mav;
}
else {
CompanyReg co=null;
co=cmprepo.findByEmail(email);
if(co!=null) {
model.addAttribute("email", email);
ModelAndView mav=new ModelAndView("HomePage");
return mav;
}
}
}
//model.addAttribute("error", "User not found");
ModelAndView mav=new ModelAndView("Login");
return mav;
}}
StudentRepo.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import com.springboot.controller.model.StudentReg;
@Service("StudentRepo")
public interface StudentRepo extends JpaRepository<StudentReg,Integer> {
StudentReg findByEmail(String email);
}
CollegeRepo.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import com.springboot.controller.model.CollegeReg;
@Service("CollegeRepo")
public interface CollegeRepo extends JpaRepository<CollegeReg,Integer>{
CollegeReg findByEmail(String email);
}
CompanyRepo.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import com.springboot.controller.model.CompanyReg;
@Service("CompanyRepo")
public interface CompanyRepo extends JpaRepository<CompanyReg,Integer> {
CompanyReg findByEmail(String email);
}
StudentReg.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import javax.persistence.Id;
@Entity
@Table(name="tbl_student_reg")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="std_id")
private int stdId;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
public User() {}
public int getStdId() {
return stdId;
}
public void setStdId(int stdId) {
this.stdId = stdId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
CollegeReg.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="tbl_colleges_reg")
public class CollegeReg {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="clg_id")
private int clgId;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
public CollegeReg() {
super();
}
public int getClgId() {
return clgId;
}
public void setClgId(int clgId) {
this.clgId = clgId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
CompanyReg.java
package com.springboot.controller.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="tbl_companies_reg")
public class CompanyReg {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="comp_id")
private int compId;
@Column(name="name")
private String name;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
public CompanyReg() {
super();
}
public int getCompId() {
return compId;
}
public void setCompId(int compId) {
this.compId = compId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
错误日志
java.lang.NullPointerException: Cannot invoke "com.springboot.controller.repository.CollegeRepo.findByEmail(String)" because "this.clgrepo" is null
at com.springboot.controller.resource.LoginController.loginHome(LoginController.java:52) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.4.jar:5.3.4]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.4.jar:5.3.4]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.43.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.4.jar:5.3.4]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.43.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.43.jar:9.0.43]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.4.jar:5.3.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.4.jar:5.3.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.4.jar:5.3.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.4.jar:5.3.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.4.jar:5.3.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.4.jar:5.3.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:346) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:887) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1684) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
请帮忙 谢谢!
存储库未注入。 在
LoginController
中,可以使用存储库创建构造函数(首选方式)或将字段标记为 @Autowired
。
检查您的
LoginController
课程。
您仅在
@Autowired
中有 StudentRepo
,但其他 2 个存储库没有此类注释。它们不会被自动装配,并且默认设置为空。
也将
@Autowired
注释也放置在那里,然后再次检查。