这个问题在这里已有答案:
我已尽力而为但我不断收到NullPointerException错误 .
我有两个使用ManyToMany映射映射在一起的Spring引导实体 . 这些实体都很好,因为所有表都是创建的 .
但是,当我尝试插入数据时,我得到一个NullPointerException .
有人可以指点我做错了吗?
下面是我用来插入数据的代码:
import com.ait.aiadmin.model.Cluster;
import com.ait.aiadmin.model.Subscriber;
import com.ait.aiadmin.repository.ClusterRepository;
import com.ait.aiadmin.repository.SubscriberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
@SpringBootApplication
public class AiAdminApplication {
public static void main(String[] args) {
SpringApplication.run(AiAdminApplication.class, args);
/*ApplicationContext ctx = SpringApplication.run(AiAdminApplication.class, args);
String [] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String name: beanNames){
System.out.println(name);
}*/
}
@Component
public class DatabaseLoader implements CommandLineRunner {
private final SubscriberRepository subscriberRepository;
private final ClusterRepository clusterRepository;
@Autowired
public DatabaseLoader(SubscriberRepository subscriberRepository, ClusterRepository clusterRepository) {
this.subscriberRepository = subscriberRepository;
this.clusterRepository = clusterRepository;
}
@Override
@Transactional
public void run (String...strings) throws Exception {
Subscriber subscriber1 = new Subscriber("Olalekan Samuel", "Ogunleye", "olalekan@gmail.com");
Cluster cluster1 = new Cluster("Aws-eu-west-1", "Ireland", "123.98.45", "Olalekan Samuel");
subscriber1.clusters.add(cluster1);
this.subscriberRepository.save(subscriber1);
this.clusterRepository.save(cluster1);
}
}
以下是我得到的错误
java.lang.IllegalStateException:无法在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]的org执行CommandLineRunner . springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] org.springframework.boot.SpringApplication.run(SpringApplication.java:304)[spring-boot-1.5.8.RELEASE.jar:1.5 .8.RELEASE] org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] org.springframework.boot.SpringApplication . 运行(SpringApplication.java:1107)[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at com.ait.aiadmin.AiAdminApplication.main(AiAdminApplication.java:19)[classes /:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_ 152] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_152] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_152] at java .lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_152] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)[spring-boot-devtools -1.5.8.RELEASE.jar:1.5.8.RELEASE]引起:java.lang.NullPointerException:null at com.ait.aiadmin.AiAdminApplication $ DatabaseLoader.run(AiAdminApplication.java:73)〜[classes /:na ] com.ait.aiadmin.AiAdminApplication $ DatabaseLoader $$ FastClassBySpringCGLIB $$ d20028d3.invoke()〜[classes /:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)~ [spring -core-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)~ [spring-aop-4.3.12.RELEASE . jar:4.3.12.RELEASE]在org.springframework.aop.framewor k.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)〜[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java: 99)〜[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE] org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)~ [spring-tx-4.3.12 .RELEASE.jar:4.3.12.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)〜[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE] org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)〜[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at com.ait.aiadmin.AiAdminApplication $ DatabaseLoader $$ E nhancerBySpringCGLIB $$ 6e72d57b.run()〜[classes /:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE ] ...省略了11个常见帧
1 回答
我不知道
Subscriber
的代码,但我猜是NPE会发生,因为subscriber1.clusters
没有初始化 . 确保正确创建实体中的所有集合 .在我的应用程序中,我经常使用getter作为集合来初始化它,如果它不存在,例如: