【问题标题】:How to fix java.lang.NullPointerException when call JpaRepository from normal class?从普通类调用 JpaRepository 时如何修复 java.lang.NullPointerException?
【发布时间】:2019-04-12 03:41:27
【问题描述】:

我是一名新的 Java 开发人员。我从普通类(不是主类,不在控制器中,..)调用 JpaRepository 并得到错误:

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());
            }
    }
}

我添加了许多符号,但它不起作用。请帮我。 感谢阅读

【问题讨论】:

    标签: java spring-boot jpa


    【解决方案1】:

    您的类 TableSpaceWarningCRC 似乎不是 spring bean。自动布线仅适用于 spring 管理的类。

    尝试在 TableSpaceWarningCRC 类的顶部添加 @Component 注释,然后从应用程序上下文中实例化该类,而不是调用构造函数。

    How to get bean using application context in spring boot

    此外,默认情况下,spring bean 是单例的。因此,如果您的类是有状态的或需要多个实例,请在 bean 定义中使用原型范围

    https://www.baeldung.com/spring-inject-prototype-bean-into-singleton

    【讨论】:

    • 嘿 DroidX86,我修复了我的代码如下:@Autowired TableSpaceWarningCRC tableSpaceWarning;并调用方法://但我在 tableSpaceWarning.setTableSpaceWarningCRC(historyUpload); 下面的行中遇到了同样的错误tableSpaceWarning.doTransData();并且该类包含 Jpa,我添加了组件表示法并修复: public TableSpaceWarningCRC() { } public void setTableSpaceWarningCRC(HistoryUpload historyUpload) { this.historyUpload = historyUpload;请帮帮我。
    • 你能用你的修复来编辑这个问题吗?在 cmets 中很难看到代码
    • 嗨 DroidX86,我更新了我的帖子。非常感谢。
    • 在同一行?
    • 嗨,对不起。我已经注意到错误行。调用 TableSpaceWarningCRC 方法时为普通类。
    猜你喜欢
    • 2018-08-13
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    相关资源
    最近更新 更多