I am using spring data jpa for creating services. I am trying to call Room Department Mapping
class method in Room service.
When I am checking result of unclaimedRoomAssign()
method independently its working properly. But When I am calling this method from Room Service class I am getting java.lang.NullPointerException
in RoomDepartmentMappingController
rdmService
object ref I am getting null but I am not getting why that ref is null? Can any one please tell me?
RoomDepartmentMappingController class
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 class
@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 class
@Repository
public interface RoomDepartmentMappingRepository extends JpaRepository<RoomDepartmentMapping, Integer>{
}
RoomService class
Here when I am calling rdmObj.unclaimedRoomAssign()
I am getting null pointer exception
@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";
}
Console
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]