简体   繁体   English

在Spring中使用多线程时出现NullPointer异常

[英]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. 我试图使用Crud存储库在sql数据库中保存对象列表。 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); 我正在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. 提前致谢。

as a result of here: 由于这里的结果:

private MedicineService medServ = new MedicineService();

the instance 'medServ' was spring IOC management, if you 'new' it , maybe don't get a instance of 'PrescriptionRepository' because it was '@Autowire' , it will be throw NPE when you call presRepo.save(pres) . 实例“ medServ”是春季IOC管理,如果您“新建”它,可能因为它是“ @Autowire”而没有得到“ PrescriptionRepository”的实例,则在调用presRepo.save(pres)时将抛出NPE 。 but if you don't use Thread means that not a instance is new by youself, so it can be work fine 但是,如果您不使用Thread,则意味着没有实例是您自己的新实例,因此可以正常工作

I modified my thread class as below and i was able to get the bean. 我修改了我的线程类,如下所示,我能够得到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();
}
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM