从普通类调用JpaRepository时如何修复java.lang.NullPointerException?
我是一名新的Java开发人员。我从普通类(不是main,不是controller,…)调用一个JpaRepository,并得到错误:从普通类调用JpaRepository时如何修复java.lang.NullPointerException?,java,spring-boot,jpa,Java,Spring Boot,Jpa,我是一名新的Java开发人员。我从普通类(不是main,不是controller,…)调用一个JpaRepository,并得到错误: java.lang.NullPointerException at com.wecommit.criticalService.controller.TableSpaceWarningCRC.doTransData(TableSpaceWarningCRC.java:34) 这是我的代码: 普通班: package com.wecommit.cr
java.lang.NullPointerException
at com.wecommit.criticalService.controller.TableSpaceWarningCRC.doTransData(TableSpaceWarningCRC.java:34)
这是我的代码:
普通班:
package com.wecommit.criticalService.critical;
import org.springframework.beans.factory.annotation.Autowired;
import com.wecommit.criticalService.entity.HistoryUpload;
public class CreateDataPerTable {
HistoryUpload historyUpload;
int l_historyUpload;
@Autowired
TableSpaceWarningCRC tableSpaceWarning;
//Constructor
public CreateDataPerTable(TableSpaceWarningCRC tableSpaceWarning) {
}
public void setHistoryUpload(HistoryUpload historyUpload) {
//Get time_upload in historyUpload by historyUploadID
this.historyUpload = historyUpload;
l_historyUpload = historyUpload.getId();
}
public void creatDataTables() {
//the error row bellow
tableSpaceWarning.setTableSpaceWarningCRC(historyUpload);
tableSpaceWarning.doTransData();
}
}
类调用JpaRepository方法:
package com.wecommit.criticalService.critical;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.wecommit.criticalService.entity.TableSpace;
import com.wecommit.criticalService.repository.TableSpaceRepository;
import com.wecommit.criticalService.entity.HistoryUpload;
@Component
public class TableSpaceWarningCRC {
HistoryUpload historyUpload;
@Autowired
TableSpaceRepository tableSpaceRepository;
public TableSpaceWarningCRC() {
}
public void setTableSpaceWarningCRC(HistoryUpload historyUpload) {
this.historyUpload = historyUpload;
}
public void doTransData() {
//TableSpace Data
List<TableSpace> listTableSpace;
listTableSpace = tableSpaceRepository.findByLogFileId(historyUpload.getLogFileId());
for (int i = 0; i < listTableSpace.size(); i++) {
TableSpaceWarningCreateTableData tableSpaceWarningCreateTableData = new TableSpaceWarningCreateTableData(listTableSpace.get(i),historyUpload.getLogFileId());
}
}
}
包com.wecommit.criticalService.critical;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
导入org.springframework.stereotype.Service;
导入com.wecommit.criticalService.entity.TableSpace;
导入com.wecommit.criticalService.repository.TableSpaceRepository;
导入com.wecommit.criticalService.entity.HistoryUpload;
@组成部分
公共类表空间警告CRC{
历史上传历史上传;
@自动连线
表空间存储库表空间存储库;
公共表空间警告CRC(){
}
public void setTableSpaceWarningCRC(历史上载历史上载){
this.historyUpload=historyUpload;
}
public void doTransData(){
//表空间数据
列表表空间;
listTableSpace=tableSpaceRepository.findByLogFileId(historyUpload.getLogFileId());
for(int i=0;i
我添加了许多符号,但不起作用。请帮帮我。
感谢您阅读您的类TableSpaceWarningCRC似乎不是Springbean。自动布线仅在spring管理的类中工作 尝试在TableSpaceWarningCRC类的顶部添加@Component注释,然后从应用程序上下文实例化该类,而不是调用构造函数 另外,默认情况下,Springbean是单例的。因此,如果您的类是有状态的或需要多个实例,请在bean定义中使用原型范围
嘿,DroidX86,我修复了下面的代码:@Autowired tableSpaceWarning crc tableSpaceWarning;和调用methods://但我在tableSpaceWarning.setTableSpaceWarningCRC(historyUpload)下面的行中得到了相同的错误;tableSpaceWarning.doTransData();这个类包含Jpa,我添加了组件符号和修复:public TableSpaceWarningCRC(){}public void setTableSpaceWarningCRC(HistoryUpload HistoryUpload){this.HistoryUpload=HistoryUpload;}请帮助我。你能用你的修复编辑这个问题吗。在评论中很难看到代码,我已经更新了我的帖子。非常感谢。在同一行吗?嗨,对不起。我注意到了错误行。调用TableSpaceWarningCRC方法时,它处于正常类。