-6

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");
}
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
yopirates
  • 153
  • 1
  • 3
  • 9
  • 5
    If you have to add details to the question, please edit it instead of making comments. – lbedogni Nov 12 '10 at 13:44
  • 6
    A stacktrace would be helpful. It's hard to figure out a blob of code without context. – speshak Nov 12 '10 at 13:46
  • if you have additional information for you question, please edit and add instead of writng comments. also, please mark the line where you get your exception, thats very much code for such a simple problem. – oezi Nov 12 '10 at 13:46
  • basically i want to get the duration of the time where i m taking the two dates and two time.start date , end date and start time and end time. while calcultating for the duration i m facing this problem – yopirates Nov 12 '10 at 13:54
  • @yopirates, please post the exception thrown, that's what everyone is trying to tell you. – Buhake Sindi Nov 12 '10 at 14:32

1 Answers1

9

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.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • 3
    + 2 for "This site is not a replacement for a debugger" but -1 for `System.out.println()`. Use a logging-framework (java.util.logging or log4j), it won't cost any time writing log.log(Level.DEBU,"message") compared to System.out.println("message");to use it, but it is helpful in production-use of you code. – Christian Kuetbach Nov 12 '10 at 15:36
  • I think this comes down to personal preference a lot. I added the log4j reference into the answer as an option. The point really is that the OP should be looking for ways to get information out of his code so he can see what's going on. – Erick Robertson Nov 12 '10 at 16:02
  • 3
    @ckuetbach: Don’t try to be holier than the pope. Sometimes, good old `printf` style debugging is absolutely fine and using a logger is overkill. Many rock star programmers use it – for example, (almost?) all in “Coders at Work” said that they do. – Konrad Rudolph Nov 12 '10 at 16:02
  • 2
    not use System.out.println() please! – hkadejo Nov 12 '10 at 17:18
  • 1
    use System.out.println() please! – Erick Robertson Nov 12 '10 at 18:25
  • Don't want to start a flame-war. I use `System.out` too, sometimes. But there are many advantaged of using a logging framework. So lets end this discussion. – Christian Kuetbach Nov 13 '10 at 18:51