空指针异常

时间:2010-11-12 13:42:09

标签: java

我在这个函数中面临一些问题,我遇到了空值并获得了空指针异常。 sqlDatesqlTime为空。

 private void setCalendarComboDetails(Map tenantConnInfo, HttpSession session, HttpServletRequest request) {
    logger.info("Enter setCalendarComboDetails");
    Appointment appointmentInfo = new Appointment();
    Date sqlDate;
    Time sqlTime;

    try {
        CompanyDbImpl companyImpl = new CompanyDbImpl(tenantConnInfo);
        PatientDbImpl patientImpl = new PatientDbImpl(tenantConnInfo);
        DoctorDbImpl doctorImpl = new DoctorDbImpl(tenantConnInfo);

        CalendarDbImpl calendarImpl = new CalendarDbImpl(tenantConnInfo);
        ConfigurationDbImpl configurationImpl = new ConfigurationDbImpl(tenantConnInfo);
        session.setAttribute("isvalid",true);
        session.removeAttribute("appointmentInfo");
       // session.removeAttribute("index");
        List<CompanyBranchDetails> branchDetailsList = companyImpl.getCompanyNBranchList();
        session.setAttribute("companyBranchList", branchDetailsList);

        List<PatientInfo> patientList = patientImpl.getPatientInfoList();
        session.setAttribute("patientInfoList", patientList);

        String whereClause = " dbd.branch_id=" + branchDetailsList.get(0).getBranchId();

        List<DoctorBranchDetails> doctorBranchDetailsList = doctorImpl.getDoctorBranchDetailsListByWhereClause(whereClause);
        System.out.println("doctor branch list size " + doctorBranchDetailsList.size());
        session.setAttribute("doctorBranchList", doctorBranchDetailsList);

        Map configMap = configurationImpl.getConfigurationMapByConfigType(ApplicationConstants.CONFIGURATION_TYPE_CALENDAR);

        int timeFormatId = Integer.parseInt(configMap.get("time_format_id").toString());
        whereClause = " time_format_id=" + timeFormatId;
        String dbTimeFormat = calendarImpl.getTimeFormatListByWhereClause(whereClause).get(0).getTimeFormatType();

        int dateFormatId = Integer.parseInt(configMap.get("date_format_id").toString());
        whereClause = " date_format_id=" + dateFormatId;
        String dbDateFormat = calendarImpl.getDateFormatListByWhereClause(whereClause).get(0).getDateFormatType();
        session.setAttribute("calendarDateFormat", dbDateFormat);

        String jsStart = request.getParameter("start1");
        String jsEnd = request.getParameter("end1");
        String jsDateNTimeFormat = ApplicationConstants.JS_DATENTIME_PATTERN;
        System.out.println("start1 " + request.getParameter("start1") + " " + new java.util.Date());

        sqlDate = appointmentInfo.getStartDate().getDatepickerDate();
            appointmentInfo.setStrStartDate(ApplicationUtils.formatSqlDate(dbDateFormat, sqlDate));
        System.out.println("sqlDate1"+sqlDate);
          sqlTime = appointmentInfo.getStartTime().getTimepickerTime();
            appointmentInfo.setStrStartTime(ApplicationUtils.formatSqlTime(dbTimeFormat, sqlTime));
            java.util.Date date1 = ApplicationUtils.getDateFromSqlDateNTime(sqlDate, sqlTime);

            sqlDate = appointmentInfo.getEndDate().getDatepickerDate();
            appointmentInfo.setStrEndDate(ApplicationUtils.formatSqlDate(dbDateFormat, sqlDate));

            sqlTime = appointmentInfo.getEndTime().getTimepickerTime();
            appointmentInfo.setStrEndTime(ApplicationUtils.formatSqlTime(dbTimeFormat, sqlTime));

            java.util.Date date2 = ApplicationUtils.getDateFromSqlDateNTime(sqlDate, sqlTime);

            String diff = ApplicationUtils.getDateDifference(date1, date2);
            appointmentInfo.setDuration(diff);
            System.out.println("difference"+diff);
        if(session.getAttribute("index") != null) {
             doctorBranchDetailsList = (ArrayList<DoctorBranchDetails>)session.getAttribute("doctorBranchDetailsList");
            int selectedIndex = Integer.parseInt(session.getAttribute("index").toString());
        DoctorBranchDetails doctorBranchInfo = doctorBranchDetailsList.get(selectedIndex);           
        appointmentInfo.getDoctorBranchDetails().setDoctorBranchId(doctorBranchInfo.getDoctorBranchId());
        appointmentInfo.getDoctorBranchDetails().getCompanyBranchDetails().setBranchId(doctorBranchInfo.getCompanyBranchDetails().getBranchId());
        appointmentInfo.getDoctorBranchDetails().setDoctorName(doctorBranchInfo.getDoctorInfo().getFullName());
         appointmentInfo.getDoctorBranchDetails().setBranchNType(doctorBranchInfo.getBranchNType());           
        }

             appointmentInfo.setStrStartDate(ApplicationUtils.converDateFormats(jsStart, jsDateNTimeFormat, dbDateFormat));
        appointmentInfo.setStrStartTime(ApplicationUtils.converDateFormats(jsStart, jsDateNTimeFormat, dbTimeFormat));
        appointmentInfo.setStrEndDate(ApplicationUtils.converDateFormats(jsEnd, jsDateNTimeFormat, dbDateFormat));
        appointmentInfo.setStrEndTime(ApplicationUtils.converDateFormats(jsEnd, jsDateNTimeFormat, dbTimeFormat));

        session.setAttribute("appointmentInfo", appointmentInfo);
    } catch (Exception e) {
        logger.error(e, e);
    }
    logger.info("Exit setCalendarComboDetails");
}

1 个答案:

答案 0 :(得分:9)

学会调试。

您可以使用许多工具。试试吧。把这个想出来(解决;计算出;弄明白。如果您无法分解代码并发现问题,那么除了小应用程序之外,您将无法充当程序员。此站点不是调试器的替代品。以下是你应该做的一些事情。

  • 自己弄清楚调试器或使用像Eclipse这样内置了这些函数的IDE。在函数开头设置断点并逐步执行。确保所有变量都包含您在每一步中所需的内容。

  • 在代码中添加System.out.println()行或使用log4j之类的日志框架来输出变量的内容。运行这部分代码后检查控制台,确保所有变量都包含您想要的每一步。

  • 将代码重构为更小的方法。单独测试每一个。这有助于隔离违反部分的代码部分。