Spring 引导存储库 java.lang.NullPointerException 错误

Spring boot repository java.lang.NullPointerException error

我的 spring 启动应用程序也能正常工作。

然后一旦更改为获取 Inputsteam 而不是 MultipartFile,它就不起作用了。

上一个代码在这里:

@Service
public class FileService {
@Autowired
Interface_freight_index interface_freight_index;
@Autowired
Interface_freight_chargecodes interface_freight_chargecodes;
@Autowired
Interface_billing_invoice_items interface_billing_invoice_items;
@Autowired
Interface_billing_invoice_header interface_billing_invoice_header;

public void save(MultipartFile file, boolean isCsv, boolean isXlsx) {

try {
  List<Object> FreightIndex = null;
  FreightIndex = ParseHelper.FileParser(file.getInputStream(), isCsv, isXlsx);

  for (Object row : FreightIndex) {
    if (row instanceof freight_index) interface_freight_index.saveAndFlush((freight_index) row);
    else if (row instanceof freight_chargecodes) interface_freight_chargecodes.saveAndFlush((freight_chargecodes) row);
    else if (row instanceof billing_invoice_items) interface_billing_invoice_items.saveAndFlush((billing_invoice_items) row);
    else if (row instanceof billing_invoice_header) interface_billing_invoice_header.saveAndFlush((billing_invoice_header) row);
  }
} catch (IOException e) {
  throw new RuntimeException("fail to store csv data: " + e.getMessage());
}
}
}

此处当前代码:

@Service
@Configurable
public class FileService {
@Autowired
private static Interface_freight_index interface_freight_index;
@Autowired
private static Interface_freight_chargecodes interface_freight_chargecodes;
@Autowired
private static Interface_billing_invoice_items interface_billing_invoice_items;
@Autowired
private static Interface_billing_invoice_header interface_billing_invoice_header;

public static void Save(InputStream file, boolean isCsv, boolean isXlsx) {

List<Object> FreightIndex = null;
FreightIndex = ParseHelper.FileParser(file, isCsv, isXlsx);

try {
  for (Object row : FreightIndex) {
    if (row instanceof freight_index) interface_freight_index.save((freight_index) row);
    else if (row instanceof freight_chargecodes) interface_freight_chargecodes.save((freight_chargecodes) row);
    else if (row instanceof billing_invoice_items) interface_billing_invoice_items.save((billing_invoice_items) row);
    else if (row instanceof billing_invoice_header) interface_billing_invoice_header.save((billing_invoice_header) row);
  }
} catch (Exception e) {
  e.printStackTrace();
}
}
}

截图如下: here

FileController 代码在这里:

进口com.pixeltrice.springbootimportcsvfileapp.services.FileService;

@CrossOrigin("http://localhost:8080")
@Controller
public class FileController {

public static void uploadFile(InputStream file, boolean isCsv, boolean isXlsx) {

try {
  FileService.Save(file, isCsv, isXlsx);
} catch (Exception e) {
  e.printStackTrace();
  }
 }
}

行有 freight_index 模型格式数据。 但它看起来是空的。

帮帮我。

消除所有静电。在你的控制器 class 中注入 FileService - 我们在这里保留字段注入,所以在控制器中定义一个字段 class

@Autowired 
FileService fileService 

然后在您的非静态控制器方法中调用 fileService.Save