我为我的移动客户端制作了一个 REST 服务器。我正在使用休眠进行数据库访问。但我似乎对这段代码有一些问题
public PlacesAssociation getPlacesAssocitionById(String id) {
session = HibernateUtil.getSessionFactory().getCurrentSession();
PlacesAssociation placesAssociation=null;
try {
session.beginTransaction();
placesAssociation = (PlacesAssociation) session.get(
PlacesAssociation.class, id);
session.getTransaction().commit();
} catch (RuntimeException e) {
session.getTransaction().rollback();
e.printStackTrace();
}
return placesAssociation;
}
我已经通过触发查询在我的数据库中手动看到了我得到结果的位置,但是在这里我有时会得到 NullPointerException。由于这个问题是偶然的,因此追踪它变得非常困难
谁能帮我解决这个问题
这是调用代码
public Response getSpecificRide(@PathParam("id") String id) {
// TODO Hibernate for a specific ride identified by the ride id.
try {
System.out.println("Get Specific Ride " + id);
Ride ride = rideRepository.getRideById(Long.parseLong(id));
RideDetailWrapper rideDetailWrapper = new RideDetailWrapper();
PlacesAssociation placesAssociationFrom = placesAssociationRepository.getPlacesAssocitionById(ride.getFromId());
PlacesAssociation placesAssociationTo = placesAssociationRepository.getPlacesAssocitionById(ride.getToId());
rideDetailWrapper.setId(ride.getId());
rideDetailWrapper.setInterval(ride.getInterval());
rideDetailWrapper.setSeatsAvailable(ride.getSeatsAvailable());
rideDetailWrapper.setTime(ride.getTime());
rideDetailWrapper.setFromAddress(placesAssociationFrom.getName());
rideDetailWrapper.setFromVicinity(placesAssociationFrom.getVicinity());
rideDetailWrapper.setToAddress(placesAssociationTo.getName());
rideDetailWrapper.setToVicinity(placesAssociationTo.getVicinity());
rideDetailWrapper.setVehicleType(ride.getVehicleType());
for (User user : ride.getUsers()) {
UserDetail userDetail = userRepository.getUserDetailByRegistrationId(user.getRegistrationId());
UserDetailWrapper userDetailWrapper = new UserDetailWrapper();
userDetailWrapper.setRegistrationId(userDetail.getRegistrationId());
userDetailWrapper.setAge(userDetail.getAge());
userDetailWrapper.setFirstName(userDetail.getFirstName());
userDetailWrapper.setLastName(userDetail.getLastName());
userDetailWrapper.setSex(userDetail.getSex());
List<UserDetailWrapper> userDetailWrappers = rideDetailWrapper.getUserDetailWrappers();
userDetailWrappers.add(userDetailWrapper);
rideDetailWrapper.setUserDetailWrappers(userDetailWrappers);
}
return Response.ok(rideDetailWrapper).build();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Problem with Get Specific Ride");
return Response.serverError().build();
}
}
我在这条线上遇到错误RideDetailWrapper.setFromAddress(placesAssociationFrom.getName());