-1

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

Le Minh
  • 23
  • 5

1 Answers1

0

Your class TableSpaceWarningCRC doesn't seem to be a spring bean. Auto wiring only works inside spring managed classes.

Try adding an @Component annotation on top of the TableSpaceWarningCRC class and then instantiate that class from the application context instead of calling the constructor.

How to get bean using application context in spring boot

Also, by default spring beans are singletons. So if your class is stateful or requires multiple instances use the prototype scope in the bean definition

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

rdas
  • 20,604
  • 6
  • 33
  • 46
  • Hey DroidX86, i fix my code folow: @Autowired TableSpaceWarningCRC tableSpaceWarning; And call methods: //but i got the same error in row below tableSpaceWarning.setTableSpaceWarningCRC(historyUpload); tableSpaceWarning.doTransData(); And the class contain Jpa, i add Component notation and fix: public TableSpaceWarningCRC() { } public void setTableSpaceWarningCRC(HistoryUpload historyUpload) { this.historyUpload = historyUpload; } Please help me. – Le Minh Apr 12 '19 at 04:02
  • Can you edit the question with your fixes. It's hard to see code in the comments – rdas Apr 12 '19 at 04:04
  • Hi DroidX86, i have updated my post. Thank you very much. – Le Minh Apr 12 '19 at 04:20
  • In the same line? – rdas Apr 12 '19 at 04:28
  • hi, i'm sorry. I have noted the error row. It is at normal class when call TableSpaceWarningCRC method. – Le Minh Apr 12 '19 at 04:32
  • That's because of the same reason but for the CreateDataPerTable class. That's not a spring-managed bean either. Make that a bean and then the other class should be autowired into it – rdas Apr 12 '19 at 05:21
  • hi, i try to create bean for class by many ways. But service do not start. Maybe, i must write project by another way. Although, thank you very much DroidX86. Have a nice day! – Le Minh Apr 12 '19 at 08:41