我正在使用spring data jpa创建服务。我正在尝试独立检查Room Department Mapping
方法的结果时,在Room service.
中调用unclaimedRoomAssign()
类方法。但是,当我从客房服务类中调用此方法时,我在java.lang.NullPointerException
中得到了RoomDepartmentMappingController
rdmService
对象ref我得到空值,但是我不明白为什么该ref为空值?有人可以告诉我吗?
RoomDepartmentMappingController类
public class RoomDepartmentMappingController {
@Autowired
RoomDepartmentMappingService rdmService;
@PostMapping("/assignUnclaimedRooms")
public ResponseEntity<String> unclaimedRoomAssign(@NotNull @RequestParam(name="nDeptId", required= true) Integer nDeptId,
@NotNull @RequestParam(name="nRoomId" , required = true) Integer nRoomId,
@Nullable @RequestParam(name="nSubDeptId" , required = false) Integer nSubDeptId){
return ResponseEntity.ok(rdmService.unclaimedRoomAssign(nDeptId, nRoomId,nSubDeptId )); //line no 29 rdmService object is getting null
}
}
RoomDepartmentMappingService类
@Service
public class RoomDepartmentMappingService {
@Autowired
RoomDepartmentMappingRepository roomDepartmentmappingRepo;
//Assign Unclaimed room to specified department and sub department
public String unclaimedRoomAssign(Integer nDeptId, Integer nRoomId, Integer nSubDeptId) {
RoomDepartmentMapping roomDepartmentMappingObj = new RoomDepartmentMapping();
if(nDeptId != 0) {
roomDepartmentMappingObj.nDeptId = nDeptId;
roomDepartmentMappingObj.nRoomId = nRoomId;
roomDepartmentMappingObj.nSubdeptId= nSubDeptId;
roomDepartmentMappingObj.nPercentageAssigned = 100;
roomDepartmentMappingObj.sStatus = "R";
}
roomDepartmentmappingRepo.save(roomDepartmentMappingObj);
return "Room assign sucessfully";
}
}
RoomDepartmentMappingRepository类
@Repository
public interface RoomDepartmentMappingRepository extends JpaRepository<RoomDepartmentMapping, Integer>{
}
RoomService类
当我打电话给rdmObj.unclaimedRoomAssign()
时,我得到了空指针异常
@Service
public class RoomService {
@Autowired
RoomRepository roomRepository;
// Add new Room Details
public String addNewRoom(@RequestBody RoomInformation roomInfo) {
Room roomRecord = new Room();
if(roomInfo.nCampusId != 0)
roomRecord.nCampusId = roomInfo.nCampusId;
roomRecord.sRoomNumber=roomInfo.sRoomNumber;
roomRecord.sRoomDesc=roomInfo.sRoomDesc;
roomRecord.nArea=roomInfo.nArea;
roomRepository.save(roomRecord); // able to save data in room
if (roomInfo.nDeptId !=0 && roomRecord.nRoomId !=0) {
System.out.println("inside if");
RoomDepartmentMappingController rdmObj= new RoomDepartmentMappingController();
rdmObj.unclaimedRoomAssign( roomInfo.nDeptId, roomRecord.nRoomId, roomInfo.nSubDeptId);
}
return "New room added sucessfully";
}
控制台
inside if
2018-09-27 13:36:44.183 ERROR 11872 --- [nio-7075-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.spacestudy.controller.RoomDepartmentMappingController.unclaimedRoomAssign(RoomDepartmentMappingController.java:29) ~[classes/:na]
at com.spacestudy.services.RoomService.addNewRoom(RoomService.java:113) ~[classes/:na]
at com.spacestudy.controller.RoomController.addNewRoom(RoomController.java:41) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_144]