Java 如何修复以下示例的路径操作和空遵从性问题

Java 如何修复以下示例的路径操作和空遵从性问题,java,Java,在使用fortify扫描代码后,我遇到以下问题 1> 路径操作问题: private MimeMessage prepareMessage(EmailMessage req) throws EmailProviderException { long start=System.currentTimeMillis(),finish=0; try { MimeMessage message = emailSender.createMimeMessage();

在使用fortify扫描代码后,我遇到以下问题

1> 路径操作问题:

private MimeMessage prepareMessage(EmailMessage req) throws EmailProviderException {
        long start=System.currentTimeMillis(),finish=0;
    try {
        MimeMessage message = emailSender.createMimeMessage();

        // Create a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        // Set email addresses
        helper.setFrom(convertAddress(req.getFromAddress()));
        helper.setTo(convertAddress(req.getToAddress()));
        helper.setCc(convertAddress(req.getCcAddress()));
        helper.setBcc(convertAddress(req.getBccAddress()));

        // Set subject and body
        helper.setSubject(req.getEmailSubject());
        String emailBody = req.getEmailBody();
        String emailMime = req.getEmailMimeType();

        MimeBodyPart messagePart = new MimeBodyPart();
        DataSource bodyDataSource = new ByteArrayDataSource(emailBody, emailMime);
        messagePart.setDataHandler(new DataHandler(bodyDataSource));
        helper.getMimeMultipart().addBodyPart(messagePart);

        // Add attachments
        List<EmailAttachment> lAttach = req.getEmailAttachment();
        if (lAttach != null) {
            for (EmailAttachment attachMnt: lAttach) {
                DataSource dSource = new ByteArrayDataSource(attachMnt
                        .getContent(), attachMnt
                        .getMimeType());
                helper.addAttachment(attachMnt.getFileName(), dSource);
            }
        }

        finish=System.currentTimeMillis();
        statsLogger.info(new FedExLogEntry("prepareMessage took {0}ms",new Object[]{finish-start}));    

        return message;

    } catch (Exception e) {

        // Covers MessagingException, IllegalStateException, IOException, MailException
        String emsg = new StringBuilder("Unable to prepare smtp message.")
            .append("\n").append(req.toString()).toString();
        logger.warn(emsg, e);
        throw new EmailProviderException(emsg, e);
    }   
}
空解引用问题2

private void addLastUpdatedHours(
    List<LocationHoursForADate> masterHours, List<LocationHoursWithSearchedDate> hoursToAdd, Map<String,String> scheduleTypeIncludesMap){
    String prevScheduleTypeCode = null;
    String prevHourTypeCode = null;
    Date searchedDate = null;

    // Build map of locationHours to searchDates
    List<LocationHours> locationHours = null;
    Map<Date, List<LocationHours>> locationHoursSearchDatesMap = new HashMap<Date, List<LocationHours>>();
    for(LocationHoursWithSearchedDate locationHoursWithSearchedDate : hoursToAdd) {
        if(scheduleTypeIncludesMap.containsKey(locationHoursWithSearchedDate.getLocationHoursPK().getScheduleTypeCd())) {
            searchedDate = locationHoursWithSearchedDate.getLocationHoursPK().getSearchedDate();
            locationHours = locationHoursSearchDatesMap.get(searchedDate);
            if(locationHours==null) {
                locationHours = new ArrayList<LocationHours>();
                locationHoursSearchDatesMap.put(searchedDate,locationHours);
            }
            locationHours.add(locationHoursWithSearchedDate.createLocationHours());
        }
    }        

    for(Map.Entry<Date,List<LocationHours>> entry : locationHoursSearchDatesMap.entrySet()) {
        prevHourTypeCode = null;
        prevScheduleTypeCode = null;
        searchedDate = entry.getKey();
        for(LocationHours hour: entry.getValue()){
            // new ST & new 01, add it
            if((prevScheduleTypeCode == null) && (prevHourTypeCode == null)){
                masterHours.add(new LocationHoursForADate(searchedDate, hour));
                prevScheduleTypeCode = hour.getLocationHoursPK().getScheduleTypeCd();
                prevHourTypeCode = hour.getLocationHoursPK().getHoursTypeCd();
            }
            else{
                //same ST
                if(prevScheduleTypeCode.equals(hour.getLocationHoursPK().getScheduleTypeCd())){
                    // same 01, skip this schedule
                    if(prevHourTypeCode.equals(hour.getHoursType().getHoursTypeCd())){
                        continue;
                    }
                    else { //new 01, add it
                        masterHours.add(new LocationHoursForADate(searchedDate, hour));
                        prevScheduleTypeCode = hour.getLocationHoursPK().getScheduleTypeCd();
                        prevHourTypeCode = hour.getLocationHoursPK().getHoursTypeCd();                      
                    }
                }
                else{ //new ST, add it
                    masterHours.add(new LocationHoursForADate(searchedDate, hour));
                    prevScheduleTypeCode = hour.getLocationHoursPK().getScheduleTypeCd();
                    prevHourTypeCode = hour.getLocationHoursPK().getHoursTypeCd();                                          
                }
            }
        }
    }
}
private void addLastUpdatedHours(
列出主小时、要添加的列表小时、地图计划类型(包括地图){
字符串prevScheduleTypeCode=null;
字符串prevHourTypeCode=null;
日期searchedDate=null;
//构建locationHours到searchDates的地图
列表位置小时数=空;
Map locationHoursSearchDatesMap=new HashMap();
用于(位置小时数带搜索日期位置小时数带搜索日期:小时数添加){
if(ScheduleTypeIncludeMap.containsKey(locationHoursWithSearchedDate.getLocationHoursPK().getScheduleTypeCd()){
searchedDate=locationHoursWithSearchedDate.getLocationHoursPK().getSearchedDate();
locationHours=locationHoursSearchDatesMap.get(searchedDate);
if(locationHours==null){
locationHours=新建ArrayList();
locationHoursSearchDatesMap.put(搜索日期,locationHours);
}
添加(locationHoursWithSearchedDate.createLocationHours());
}
}        
对于(Map.Entry:locationHoursSearchDatesMap.entrySet()){
prevHourTypeCode=null;
prevScheduleTypeCode=null;
searchedDate=entry.getKey();
对于(LocationHours小时:entry.getValue()){
//新建ST和新建01,添加它
if((prevScheduleTypeCode==null)&&(prevHourTypeCode==null)){
masterHours.add(新位置hoursforadate(searchedDate,hour));
prevScheduleTypeCode=hour.getLocationHoursPK().getScheduleTypeCd();
prevHourTypeCode=hour.getLocationHoursPK().getHoursTypeCd();
}
否则{
//同一街
if(prevScheduleTypeCode.equals(hour.getLocationHoursPK().getScheduleTypeCd())){
//相同01,跳过此计划
if(prevHourTypeCode.equals(hour.getHoursType().getHoursTypeCd())){
继续;
}
否则{//new 01,添加它
masterHours.add(新位置hoursforadate(searchedDate,hour));
prevScheduleTypeCode=hour.getLocationHoursPK().getScheduleTypeCd();
prevHourTypeCode=hour.getLocationHoursPK().getHoursTypeCd();
}
}
否则{//new ST,添加它
masterHours.add(新位置hoursforadate(searchedDate,hour));
prevScheduleTypeCode=hour.getLocationHoursPK().getScheduleTypeCd();
prevHourTypeCode=hour.getLocationHoursPK().getHoursTypeCd();
}
}
}
}
}
}第二种情况(空差异1)很容易发现

问题是,如果try-catch块中存在异常,则对象
消息
将为null,因此最终指令返回
消息.getBytes()
将引发
NullPointerException

避免这种情况的一种方法是如下更改return语句:

return message != null ? message.getBytes() : null;

但是,您可能更愿意提出异常…

没有人会阅读完整的代码,保持清晰并突出问题所在。至少,在每种情况下都要指出准确的行。
return message != null ? message.getBytes() : null;