[英]Getting NULL pointer exception while uploading object to database using JPA
在我的代码中,我已经阅读了一个Excel工作表,并将read参数从Excel工作表传递给了方法
@RestController
@RequestMapping("/api/v1")
public class ExcelController3 {
private MultipartFile uploadfile;
@Autowired
private EmployeeRepository employeeRepository;
@RequestMapping(value = "/upload3", method = RequestMethod.POST, consumes = javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA)
public void uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) throws IOException {
this.uploadfile=file;
System.out.println("*****************************");
System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
System.out.println("file.getContentType()" + file.getContentType());
System.out.println("file.getInputStream() " + file.getInputStream());
System.out.println("file.toString() " + file.toString());
System.out.println("file.getSize() " + file.getSize());
System.out.println("name " + name);
System.out.println("file.getBytes() " + file.getBytes());
System.out.println("file.hashCode() " + file.hashCode());
System.out.println("file.getClass() " + file.getClass());
System.out.println("file.isEmpty() " + file.isEmpty());
try {
ExcelController ex=new ExcelController();
File f1=ex.convert(file);
// FileInputStream file = new FileInputStream(new File("E://Imp/Details.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(f1);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//This will change all Cell Types to String
cell.setCellType(Cell.CELL_TYPE_STRING);
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
break;
case Cell.CELL_TYPE_STRING:
List list=new ArrayList();
list.add(cell.getStringCellValue());
break;
}
}
name=row.getCell(0).getStringCellValue();
String empid = row.getCell(1).getStringCellValue();
String add=row.getCell(2).getStringCellValue();
String mobile=row.getCell(3).getStringCellValue();
System.out.println(name+empid+add+mobile);
ExcelController3 ex1=new ExcelController3();
// ex1.InsertRowInDB(name,empid,add,mobile);
System.out.println("");
Employee em=new Employee();
em.setName(name);
em.setEmpid(empid);
em.setAddress(add);
em.setMobile(mobile);
System.out.println(em);
System.out.println(em.getAddress());
System.out.println(em.getEmpid());
System.out.println(em.getMobile());
System.out.println(em.getName());
ex1.InsertRowInDB(name,empid,add,mobile,em);
}
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
public void InsertRowInDB(String name,String empid,String add,String mobile,Employee em) {
// System.out.println("name "+name);
// System.out.println("empid "+empid);
// System.out.println("add "+add);
// System.out.println("mobile "+mobile);
// Employee em=new Employee();
//
// em.setName(name);
// em.setEmpid(empid);
// em.setAddress(add);
// em.setMobile(mobile);
// System.out.println(em);
// System.out.println(em.getAddress());
// System.out.println(em.getEmpid());
// System.out.println(em.getMobile());
// System.out.println(em.getName());
employeeRepository.save(em);
//employeeRepository.save(em);
// Statement stmt=db.con.createStatement();
// PreparedStatement ps=null;
// String sql="Insert into Employee(Name,EmployeeId,Address,ContactInfo) values(?,?,?,?)";
// ps=db.con.prepareStatement(sql);
// ps.setString(1, name);
// ps.setString(2, empid);
// ps.setString(3, add);
// ps.setString(4, mobile);
// ps.executeUpdate();
System.out.println("Values Inserted Successfully");
}
public File convert(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}
@GetMapping(value="/upload")
public ResponseEntity<Collection<Employee>> getallemployees(){
Collection<Employee> el =employeeRepository.findAll();
return new ResponseEntity<Collection<Employee>>(el, HttpStatus.OK);
}
}
但这给了我employeeRepository.save(em);的空指针异常; 。 无法找出原因,我也自动连接了employeeRepository并为jpa创建了正确的pojo类。
因此,再次阅读代码后,问题是因为您在控制器上使用了一些“新”字符。 这: ExcelController3 ex1=new ExcelController3();
创建一个不受管理的ExcelController3
全新实例(即,没有@Autowired
或任何其他spring magic ),因此ex1
的存储库为null。
您可能要替换:
ExcelController3 ex1=new ExcelController3();
ex1.InsertRowInDB(name,empid,add,mobile,em);
通过
this.InsertRowInDB(name,empid,add,mobile,em);
还要注意的是,你不应该储存一些上传的文件( private MultipartFile uploadfile;
控制器上(因为每个用户都使用相同的控制器实例)
一些阅读:
我不了解JPA,但在您使用“ employeeRepository.save(em);”之前, 在类中使用该函数之前,不需要创建类“ EmployeeRepository”的新实例吗? 我可以看到您在顶部将类声明为private EmployeeRepository employeeRepository;
但是看不到在哪里创建它的实例,如果不这样做,则在尝试使用函数save时会出现nullpointer异常错误。
例如:
EmployeeRepository employeeRepository = new EmployeeRepository ();
employeeRepository.save(em);
如果这不能解决您的问题,请发布功能“保存”的更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.