序列化数据格式为NullPointerException

时间:2013-12-18 06:36:28

标签: java serialization

我已经编写了一些代码来以文件

中的某种格式显示数据
public List<TimeSheetVO> getEmployeedetails(Connection con2, java.sql.Date dbdate) {

    timesheetlist=new ArrayList<TimeSheetVO>();
    try {
        String query = SQLutilConstants.GET_TIMESHEET;
        psmt=con2.prepareStatement(query);
        psmt.setDate(1,dbdate );
        rs=psmt.executeQuery();

        while(rs.next()) {
            TimeSheetVO  timeSheetVO=new TimeSheetVO();
            timeSheetVO.setEMP_ID(rs.getString("EMP_ID"));  
            //timeSheetVO.setATTENDANCE_DATE(rs.getString("ATTENDANCE_DATE").replaceAll("[-+.^:,]", ""));
            String attendance= rs.getString("ATTENDANCE_DATE");
            String[] splitattendance=attendance.split("-");
            timeSheetVO.setATTENDANCE_DATE(splitattendance[2]+splitattendance[1]+splitattendance[0]); 

            String intimetime= rs.getString("IN_TIME");
            String[] splitintime=intimetime.split(":");
            timeSheetVO.setIN_TIME(splitintime[0]+splitintime[1]);

            String outtime=rs.getString("OUT_TIME");
            String[] splitouttime=outtime.split(":");
            timeSheetVO.setOUT_TIME(splitouttime[0]+splitouttime[1]);

            System.out.println("timesheetVo"+timeSheetVO.getATTENDANCE_DATE());
            timesheetlist.add(timeSheetVO);
        }
    } catch(Exception e) {
            e.printStackTrace();
    }
    return  timesheetlist;
}

我们再使用一个方法并将 timesheetlist 作为 timesheetlist2 传递: 但是,此处仅显示一行剩余行不会显示相应的出勤日期获取 NullPointerException

public  String getTimesheet(List<TimeSheetVO> timesheetlist2, java.sql.Date dbdate) {           
    try {
        String fieloc=MessageConstants.timesheetpath; 
        File file=new File(fieloc+dbdate+".txt");
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);

        for(TimeSheetVO time: timesheetlist2) {
            bw.write(time+" ");
            bw.newLine();
            bw.flush();
            path=file.getAbsolutePath();
        }
        bw.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
    return  path;
}

1 个答案:

答案 0 :(得分:-1)

首先检查从查询执行返回的记录数。 然后在循环后检查timesheetlist的大小。 当然你会得到一些线索。