基本上用戶提交所選年份,然後servlet返回相應的年度報表。 然後用戶可以修改相應的信息。那麼servlet保存修改的信息。GAE應用程序開發:返回hashmap時的NullPointerException
假設用戶所尋找的螺柱始終存在於em中。問題是: 如果用戶之前未存儲年度報告信息,並且用戶提交了一年(即2009年),則所有內容都運行正常。用戶修改信息並且GPAInfoServlet將存儲修改的信息。
然而,在完成上述動作,這意味着前一個年度報告的信息存儲在用戶,用戶(指我)得到錯誤NullPointerException異常過程中提交每年評選
at tcndata.Stud.jdoGetannualReport(Stud.java)
at tcndata.Stud.getAnnualReport(Stud.java:33)
at servlet.YearSelectedServlet.doPost(YearSelectedServlet.java:35)
選擇後年份:
public class YearSelectedServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
int yearselected = Integer.parseInt(req
.getParameterValues("yearselected")[0]);
long studid = Long.parseLong(req.getSession().getAttribute("studID")
.toString());
EntityManager em;
em = EMF.get().createEntityManager();
Stud stud = em.find(Stud.class, studid);
if (stud != null) {
Map<Integer, AnnualReport> annualreport;
if (stud.getAnnualReport() == null)
annualreport = new HashMap<Integer, AnnualReport>();
else
annualreport = stud.getAnnualReport();
if (annualreport.containsKey(yearselected)) {
AnnualReport thatyear = stud.getAnnualReport()
.get(yearselected);
req.getSession().setAttribute("thatyear", thatyear);
req.getSession().setAttribute("annualReport", thatyear);
}
} else {
req.getSession().setAttribute("Error", "alumni not found");
resp.sendRedirect("Error.jsp");
}
req.getSession().setAttribute("yearselected",
req.getParameterValues("yearselected")[0]);
req.getSession().setAttribute("SearchTag", "GPAInfo");
resp.sendRedirect("Search.jsp");
}
}
保存信息修改:
public class GPAInfoServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
AnnualReport thatyear = new AnnualReport();
thatyear.setAttendSchool(AttendSchool.valueOf(req
.getParameterValues("AttendSchool")[0]));
.....
int yearselected = Integer.parseInt(req.getSession()
.getAttribute("yearselected").toString());
long studid = Long.parseLong(req.getSession().getAttribute("studID")
.toString());
EntityManager em;
em = EMF.get().createEntityManager();
Stud stud = em.find(Stud.class, studid);
if (stud != null) {
Map<Integer, AnnualReport> annualreport;
if (stud.getAnnualReport() == null)
annualreport = new HashMap<Integer, AnnualReport>();
else
annualreport = stud.getAnnualReport();
annualreport.put(yearselected, thatyear);
try {
stud.setAnnualReport(annualreport);
em.merge(stud);
} finally {
em.close();
}
} else {
req.getSession().setAttribute("Error", "alumni not found");
resp.sendRedirect("Error.jsp");
}
req.getSession().setAttribute("SearchTag", "GPAInfo");
resp.sendRedirect("/Search.jsp");
}
}
梭哈類:
@Entity(name = "Stud")
public class Stud{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long studID;
private Map<Integer,AnnualReport>annualReport = new HashMap<Integer,AnnualReport>();
public Map<Integer, AnnualReport> getAnnualReport() {
return annualReport;
}
public void setAnnualReport(Map<Integer, AnnualReport> annualReport) {
this.annualReport = annualReport;
}
**我沒有任何想法會發生什麼。
我已經在年報@Embeddable:
@Embeddable
公共類年報{
@Basic
private AttendSchool attendSchool;
private String attendSchoolNote;
......
}
我怎樣才能得到NullPointerException異常在**如果(stud.getAnnualReport( )== null)**
if (stud != null) {
Map<Integer, AnnualReport> annualreport;
if (stud.getAnnualReport() == null)
annualreport = new HashMap<Integer, AnnualReport>();
else
annualreport = stud.getAnnualReport();
....
} else {
....
}
NullPointerException?
需要幫助.... 我要死了......
是的,我敢肯定。爲什麼thatyear = stud.getAnnualReport()。get(yearselected);生產NPE? if(stud.getAnnualReport()== null) then annualreport = new HashMap(); then annualreport.containsKey(yearselected)is false, then thatyear = stud.getAnnualReport() .get(yearselected);不能執行,對吧? –
user1654099