2016-05-16 7 views
0

"場所"テーブルからすべての行を取得しようとしています。しかし、私のDAOメソッドは常にNullPointerExceptionを返します。私はなぜそれが起こるのか分からない。私はhql、基準などを試しましたが、結果は同じでした。Hibernate。すべての行を取得するnullPointerExceptionを返します

コントローラ

@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; 
} 

場所ダオ

@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; 
} 

}

ロケーションエンティティ

@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ステータス500 - 要求の処理に失敗しました。ネストされた例外はjava.lang.NullPointerExceptionです。 この行のリストの場所= locationService.getAll();.コントローラ内。

もっとファイルが必要な場合は、教えてください。

答えて

1

@Autowiredは、即時のクラスメンバーにのみ適用されます。 )

+0

ThxをReimeusを、locationService

@Autowired private LocationService locationService; 

それはあなたの将来の計画に備えている場合futurePlanServiceについて同じことを行うには、それを追加! 1つの注釈で複数のサービスを使用できると思った。 あなたのソリューションは私の問題を解決しました。 –

関連する問題