I'm a new Java developer. I call a JpaRepository from normal class ( not main, not in controller,..) and get the error:
java.lang.NullPointerException
at com.wecommit.criticalService.controller.TableSpaceWarningCRC.doTransData(TableSpaceWarningCRC.java:34)
Here my code: normal class:
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();
}
}
And the class call JpaRepository method:
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());
}
}
}
I added many notation but it does not work. Please help me. Thank you for reading