2016-05-16 18 views
0

Ich versuche, alle Zeilen aus der Tabelle "Location" zu erhalten. Aber meine DAO-Methode gibt immer NullPointerException zurück. Ich weiß nicht, warum es passiert. Ich habe versucht, hql, Kriterien usw., aber das Ergebnis war das gleiche.Ruhezustand. Alle Zeilen werden zurückgegeben nullPointerException

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

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

}

Ort 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-Verarbeitung fehlgeschlagen; verschachtelte Ausnahme ist java.lang.NullPointerException. In dieser Zeile Liste locations = locationService.getAll() ;. Im Controller.

Wenn Sie mehr Dateien benötigen, sagen Sie es mir einfach.

Antwort

1

@Autowired gilt nur für das unmittelbare Klassenmitglied. Fügen Sie es zu locationService

@Autowired 
private LocationService locationService; 

das gleiche für futurePlanService tun, wenn es in Ihren zukünftigen Plänen gehört;)

+0

Thx Reimeus! Ich dachte, ich kann mehrere Dienste unter einer Anmerkung verwenden. Ihre Lösung löste mein Problem. –

Verwandte Themen