基本的に、ユーザーが選択した年を送信すると、サーブレットはそれに応じた年次報告書を返します。その後、ユーザーはそれに応じて情報を変更できます。次に、サーブレットは変更された情報を保存します。
ユーザーが探しているスタッドが常に em に存在すると仮定します。問題は、ユーザーが以前に年次報告書情報を保存しておらず、ユーザーが 1 年、つまり 2009 年に提出した場合、すべてが正常に実行されることです。ユーザーが情報を変更すると、GPAInfoServlet は変更された情報を保存します。
ただし、上記のアクションを終了した後、つまり、ユーザーが以前に 1 つの年次レポート情報を保存したことを意味し、ユーザー (つまり私) は、選択した年を送信するプロセス中にERROR 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
公開クラス AnnualReport{
@Basic
private AttendSchool attendSchool;
private String attendSchoolNote;
......
}
** if (stud.getAnnualReport() == null) ** でNullPointerExceptionを取得するにはどうすればよいですか
if (stud != null) {
Map<Integer, AnnualReport> annualreport;
if (stud.getAnnualReport() == null)
annualreport = new HashMap<Integer, AnnualReport>();
else
annualreport = stud.getAnnualReport();
....
} else {
....
}
NullPointerException ?
助けが必要です....私は死にかけています...