开发者

NullPointerException [duplicate]

This question already has answers here: What is a NullPointerException, and how do I fix it? (12 answers) 开发者_运维问答 Closed 6 years ago.

I am facing some problem in this function where I'm running into null values and getting a null pointer exception. sqlDate and sqlTime are null.

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


Learn to Debug.

You have many tools at your disposal. Try them. Figure this out. If you can't break apart your code and find problems, you won't be much use as a programmer on anything other than tiny applications. This site is not a replacement for a debugger. Here are some things you should work on.

  • Figure out the debugger yourself or use an IDE like Eclipse that has those functions built in. Set breakpoints at the start of the function and step through it. Make sure all variables contain what you want at each step of the way.

  • Add System.out.println() lines to the code or use a logging framework like log4j to output the contents of variables along the way. Review the console after running this part of the code and make sure that all variables contain what you want each step of the way.

  • Refactor the code into smaller methods. Unit test each one individually. This helps isolate the part of the code that is breaking from the parts that aren't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜