在SpringBoot应用程序中使用ManyToMany插入数据时出现NullPointerException

时间:2018-01-26 07:06:34

标签: java spring-boot many-to-many

我已尽力而为但我不断收到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:无法执行CommandLineRunner     在   org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)   [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at   org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)   [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at   org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)   [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:304)   [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)   [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] at   org.springframework.boot.SpringApplication.run(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   方法)〜[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] at   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] at   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)   〜[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at   org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)   〜[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE] at   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] at   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)   〜[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at   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 $$ EnhancerBySpringCGLIB $$ 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 个答案:

答案 0 :(得分:0)

我不知道Subscriber的代码,但我猜想会发生NPE,因为subscriber1.clusters未初始化。确保正确创建实体中的所有集合。

在我的应用程序中,我经常使用getter作为集合来初始化它,如果它不存在,例如:

public List<Something> getSomethings() {
    if (somethings == null) {
        somethings = new ArrayList<>();
    }
    return somethings;
}