Reputation: 270
I'm trying to get all rows from "Location" table. But my DAO method always return NullPointerException. I don't know why does it happens. I have tried hql, criteria etc., but result was the same.
Controller
@Controller
public class HomeController {
@Autowired
private UserService userService;
private FuturePlanService futurePlanService;
private LocationService locationService;
@RequestMapping(value = "/user/{id}/addPlan", method = RequestMethod.GET)
public ModelAndView addFuturePlan(@PathVariable int id){
ModelAndView modelAndView = new ModelAndView("add_future");
User user = userService.findById(id);
FuturePlan futurePlan = new FuturePlan();
List<Location> locations = locationService.getAll();
modelAndView.addObject("ownerUser", user);
modelAndView.addObject("future", futurePlan);
modelAndView.addObject("locations", locations);
return modelAndView;
}
Location Dao
@Repository
public class LocationDaoImpl implements LocationDao {
@Autowired
SessionFactory sessionFactory;
@Override
public Location findLocation(int id) {
Session session = sessionFactory.openSession();
Location location = (Location) session.get(Location.class, id);
session.close();
return location;
}
@Override
public Location findLocation(String name) {
Session session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Location.class);
cr.add(Restrictions.eq("lName", name));
cr.setMaxResults(1);
Location location = (Location) cr.uniqueResult();
session.close();
return location;
}
@Override
public List<Location> getAll() {
Session session = sessionFactory.openSession();
List<Location> list = session.createQuery("from Location").list();
session.close();
return list;
}
}
Location Entity
@Entity
public class Location {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int lId;
private String lName;
public Location() {
super();
}
public Location(int lId, String lName) {
super();
this.lId = lId;
this.lName = lName;
}
public int getlId() {
return lId;
}
public void setlId(int lId) {
this.lId = lId;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException. In this line List locations = locationService.getAll();. In Controller.
If u need more files, just tell me.
Upvotes: 0
Views: 92
Reputation: 159844
@Autowired
only applys to the immediate class member. Add it to locationService
@Autowired
private LocationService locationService;
Do the same for futurePlanService
if it features in your future plans ;)
Upvotes: 2