Search code examples
javaspringmultithreadingthreadpoolspring-annotations

NullPointer exception while using Multithread in Spring


I am new to multi threading. I was trying to save a List of Object in sql database using crud repository. but i am getting null pointer exception in the save operation. Below is my code and exception. Thread Class

public class PrescriptionThread implements Runnable{
  private MedicineService medServ = new MedicineService();
  private  Presciption pres ;

  public PrescriptionThread(Presciption pres) {
    this.pres = pres;
  }

  @Override
  public void run() {
    medServ.savePrescription(pres);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
}

Service Class

@Service
public class MedicineService {

@Autowired
private PrescriptionRepository presRepo;

public void storePrescription(List<Presciption> payload) {

    ExecutorService executorService = Executors.newFixedThreadPool(5);
    for (Presciption presciption : payload) {
        Runnable runnable = new PrescriptionThread(presciption);
        executorService.execute(runnable);
    }
    executorService.shutdown();

    while(!executorService.isTerminated()) {}
}


public synchronized void savePrescription(Presciption pres) {
        presRepo.save(pres);
}
}

Repository

@Repository
public interface PrescriptionRepository extends CrudRepository<Presciption, Integer> {

}

I am getting exeption in presRepo.save(pres);

Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-2" java.lang.NullPointerException
at com.cerner.api.service.MedicineService.savePrescription(MedicineService.java:86)
at com.cerner.api.util.PrescriptionThread.run(PrescriptionThread.java:16)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)

If i don't use thread and just say

for (Presciption presciption : payload) {
        presRepo.save(pres);
    }

It is working fine and i am able to save it to db. But i want to implement thread. Thanks in advance.


Solution

  • I modified my thread class as below and i was able to get the bean.

    public class PrescriptionThread implements Runnable{
    private MedicineService medServ;// = new MedicineService();
    private  Presciption pres ;
    
    public PrescriptionThread(Presciption pres, MedicineService medicineService) {
    this.pres = pres;
    this.medServ = medicineService;
    }
    
    @Override
    public void run() {
    medServ.savePrescription(pres);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    }
    }