GAE应用程序开发:返回hashmap时出现NullPointerException

时间:2012-10-19 20:07:20

标签: java google-app-engine map

基本上用户提交所选年份,然后servlet返回相应的年度报告。 然后用户可以修改相应的信息。然后servlet保存修改后的信息。

假设用户寻找的螺柱始终存在于em中。问题是: 如果用户之前没有存储年度报告信息,并且用户提交一年即2009年,则一切运行正常。用户修改信息,GPAInfoServlet将存储修改的信息。

但是,在完成上述操作后,这意味着用户之前存储了一份年度报告信息,用户(这意味着我)在提交所选年份的过程中会收到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");

}

}

Stud课程:

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

......

}

如何在**中获取 NullPointerException if(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

需要帮助.... 我快要死了......

1 个答案:

答案 0 :(得分:0)

您确定行annualreport = stud.getAnnualReport();是您获得NPE的地方吗?

因为如果stud.getAnnualReport()null,您的代码肯定会生成NPE,因此会减少几行代码:

AnnualReport thatyear = stud.getAnnualReport()
                .get(yearselected);