I'm trying to create an object of a "Company" using the appropriate facade and I'm getting a null pointer exception that I can't explain. I tried initializing the attributes with a new arrayList and it didn't seem to work
the facade with the problematic method:
package com.lukman.vehicle.service;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.lukman.vehicle.beans.Company;
import com.lukman.vehicle.beans.Driver;
import com.lukman.vehicle.beans.SafetyOff;
import com.lukman.vehicle.beans.Supervisor;
import com.lukman.vehicle.exceptions.InvalidDetailsException;
@Service
@Scope("prototype")
public class SupervisorService extends ClientService {
private int supervisorId;
public SupervisorService() {
super();
}
public SupervisorService(int supervisorId) {
super();
this.supervisorId = supervisorId;
}
@Override
public boolean login(String userName, String password) throws InvalidDetailsException {
List<Supervisor> supervisors = supervisorRepo.findAll();
for (Supervisor supervisor : supervisors) {
if (supervisor.getUserName().equals(userName) && supervisor.getPassowrd().equals(password)) {
this.supervisorId = supervisor.getId();
return true;
}
}
return false;
}
public void addCompany(Company company) throws InvalidDetailsException {
List<Company> companies = companyRepo.findAll();
for (Company company2 : companies) {
if (company2.getName().equals(company.getName())) {
throw new InvalidDetailsException("a company with the same name already exists");
}
}
company.setSupervisorId(this.supervisorId);
***companyRepo.save(company);***
}
public void updateCompany(Company company) {
companyRepo.saveAndFlush(company);
}
public void deleteCompany(int companyId) {
companyRepo.delete(companyRepo.getOne(companyId));
}
public Company getOneCompany(int companyId) {
return companyRepo.getOne(companyId);
}
public List<Company> getAllCompanies() {
return companyRepo.findCompaniesBySupervisorId(this.supervisorId);
}
public void addDriver(Driver driver) throws InvalidDetailsException {
List<Driver> drivers = driverRepo.findAll();
for (Driver driver2 : drivers) {
if (driver.getTz() == driver2.getTz() || driver.getCompanyId() == 0) {
throw new InvalidDetailsException("tz must be unique, cannot add driver without specifying companyId");
}
}
driverRepo.save(driver);
}
public void deleteDriver(int driverId) {
driverRepo.delete(driverRepo.getOne(driverId));
}
public void addSafetyOff(SafetyOff safetyOff) throws InvalidDetailsException {
List<SafetyOff> safetyOffs = safetyOffRepo.findAll();
for (SafetyOff safetyOff2 : safetyOffs) {
if (safetyOff.getTz() == safetyOff2.getTz()) {
throw new InvalidDetailsException("tz must be unique");
}
}
safetyOff.setSupervisorId(supervisorId);
safetyOffRepo.save(safetyOff);
}
public void updateSafetyOff(SafetyOff safetyOff) {
safetyOffRepo.saveAndFlush(safetyOff);
}
public void deleteSafetyOff(int safteyOffId) {
safetyOffRepo.delete(safetyOffRepo.getOne(safteyOffId));
}
}
the Company bean:
@Data
@Entity
@Table(name = "companies")
@NoArgsConstructor
@AllArgsConstructor
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false)
private int id;
private int supervisorId;
private String name;
@ManyToMany(cascade = { CascadeType.ALL })
private List<SafetyOff> safetyOffs = new ArrayList<>();
@OneToMany(cascade = { CascadeType.REMOVE,
CascadeType.PERSIST }, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "companyId")
private List<Driver> drivers = new ArrayList<>();
public Company(String name) {
super();
this.name = name;
}
the console:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.3.jar:2.4.3]
at com.lukman.vehicle.VehicleApplication.main(VehicleApplication.java:10) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:566) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.4.3.jar:2.4.3]
Caused by: java.lang.NullPointerException: null
at com.lukman.vehicle.service.SupervisorService.addCompany(SupervisorService.java:48) ~[classes/:na]
at com.lukman.vehicle.clr.SupervisorTesting.run(SupervisorTesting.java:22) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-2.4.3.jar:2.4.3]
... 10 common frames omitted
the clr:
@Order(2)
@Component
@Lazy
public class SupervisorTesting implements CommandLineRunner {
SupervisorService supervisorService = new SupervisorService(1);
@Override
public void run(String... args) throws Exception {
Company c1 = new Company("coca-cola");
supervisorService.addCompany(c1);
c1 = supervisorService.getOneCompany(1);
c1.setName("cola-coca");
supervisorService.updateCompany(c1);
The parent class:
package com.lukman.vehicle.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lukman.vehicle.exceptions.InvalidDetailsException;
import com.lukman.vehicle.repository.CompanyRepo;
import com.lukman.vehicle.repository.DriverRepo;
import com.lukman.vehicle.repository.SafetyOffRepo;
import com.lukman.vehicle.repository.SubDriverRepo;
import com.lukman.vehicle.repository.SupervisorRepo;
@Service
public abstract class ClientService {
@Autowired
protected CompanyRepo companyRepo;
@Autowired
protected DriverRepo driverRepo;
@Autowired
protected SafetyOffRepo safetyOffRepo;
@Autowired
protected SubDriverRepo subDriverRepo;
@Autowired
protected SupervisorRepo supervisorRepo;
public abstract boolean login(String userName, String password) throws InvalidDetailsException;
}