I have tried all I could but I keep getting NullPointerException Error.
I have two Spring boot entities that have been mapped together using ManyToMany mapping. These Entities are fine as all the tables are created.
However, when I try to insert data, I get a NullPointerException.
Can someone please point me to what I am doing wrong?
Below is the code that I'm using to insert the data:
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);
}
}
And below is the error that I am getting
java.lang.IllegalStateException: Failed to execute CommandLineRunner at 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 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] Caused by: 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 common frames omitted