Java Spring Data JPA 存储库抛出空指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37443852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA repository throws null pointer
提问by James Millner
I've been working with Spring Data with Spring Boot. With the JavaConfig documentation I have set up a Spring JPA configuration, but when calling the save method in my repository a null pointer is thrown.
我一直在使用 Spring Boot 处理 Spring Data。使用 JavaConfig 文档,我设置了 Spring JPA 配置,但是在我的存储库中调用 save 方法时,会抛出空指针。
My repository:
我的存储库:
import org.springframework.data.jpa.repository.JpaRepository;
public interface HouseRepository extends JpaRepository<House, Long> {
}
My POJO's:
我的POJO:
AbstractHouse
抽象屋
@MappedSuperclass
@Table(name="houses")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class AbstractHouse implements Serializable {
@NotEmpty
private String houseName;
@NotEmpty
private String houseNumber;
House
房子
@Entity
public class SmallHouse extends AbstractHouse {
@Id
@GeneratedValue
private Long id;
private Date created;
private Date updated;
I've then got a service object that takes an Autowired repository and attempts to save using methods obtained through JPA:
然后我有一个服务对象,它接受一个 Autowired 存储库并尝试使用通过 JPA 获得的方法进行保存:
@Service
public class HouseService {
@Autowired
protected HouseRepository houseRepository;
public void save(House house) {
houseRepository.save(house);
}
My application class contains:
我的应用程序类包含:
@SpringBootApplication
@EnableScheduling
@EnableJpaRepositories
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
I am fairly new to Spring Data, so I do think its something within the configuration that I am missing, any advice would be greatly appreciated!
我对 Spring Data 相当陌生,所以我确实认为它在我缺少的配置中的某些东西,任何建议将不胜感激!
--Update--
- 更新 -
Stack Trace:
堆栈跟踪:
java.lang.NullPointerException
at com.jm.service.HouseService.save(HouseService.java:15)
at com.jm.service.HouseClass.gatherHouses(HouseClass.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
And I call the method as such:
我这样称呼该方法:
public class HouseMain {
@Autowired
public HouseService houseService;
public void gatherHouse() {
houseService = new HouseService();
House h = new House();
h.setHouseName("House1");
h.setHouseNumber("12");
try {
HouseService.save(vmComp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Then in my controller I am calling house.gatherHouse
然后在我的控制器中我调用 house.gatherHouse
采纳答案by Nikem
You currently instantiate HouseService
by hand, not through Spring. In the houseService
object that you created you have field HouseRepository houseRepository
which is null
, because you don't initialize it in any way. If you use Spring and its Autowired
functionality, then you should get the instance of HouseService
from Spring context. E.g. you can @Autowired
it into your controller class.
您目前HouseService
手动实例化,而不是通过 Spring。在houseService
您创建的对象中,您有一个字段HouseRepository houseRepository
is null
,因为您没有以任何方式对其进行初始化。如果您使用 Spring 及其Autowired
功能,那么您应该HouseService
从 Spring 上下文中获取 的实例。例如,您可以将@Autowired
其添加到您的控制器类中。
回答by shankarsh15
This is not correct:
这是不正确的:
houseService = new HouseService();
since you are using Spring
, you should allow Spring to create all the beans ,that's the reason of using @Autowired
, which in this case is not getting executed and houseRepository is initialised
as null
.
由于您正在使用Spring
,您应该允许 Spring 创建所有 bean,这就是 using 的原因@Autowired
,在这种情况下,它没有被执行并且 houseRepository 是initialised
as null
。