像java.lang.ArrayIndexOutOfBoundsException获取错误:1

时间:2013-11-14 20:47:22

标签: java spring-mvc

我正在尝试运行以下代码,但它失败并出现以下错误。我无法找出出了什么问题。请帮忙。 错误:java.lang.ArrayIndexOutOfBoundsException:第37行第1行。

代码:

package com.ugam.filter.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.ugam.domain.filter.FilterTemplate;


@Configuration
@PropertySource("classpath:properties/filterTemplate.properties")
public class FilterTemplateRepository  {
    @Autowired
    private Environment env;

    @Bean
    public Map<String,List<FilterTemplate>> getBean()
    {

        Map<String,List<FilterTemplate>> filterTemplateMap=new HashMap<String,List<FilterTemplate>>();
        String moduleName=env.getProperty("module");
        for(String eachmoduleName : moduleName.split(",")){
            String templateName=env.getProperty(eachmoduleName + ".templateNames");
            List<FilterTemplate> filterTemplatelist=new ArrayList<FilterTemplate>();
            for (String eachtemplateNames : templateName.split(",")) {
                String templateNameFields[] = env.getProperty(eachmoduleName + "." + eachtemplateNames).split(",");
                FilterTemplate filterTemplate=new FilterTemplate();
                filterTemplate.setTemplateHtml(templateNameFields[0]);
                filterTemplate.setClassSelect(templateNameFields[1]);
                filterTemplatelist.add(filterTemplate);
            }
            filterTemplateMap.put(eachmoduleName, filterTemplatelist);
        }
        return filterTemplateMap;

    }


    public List<FilterTemplate> getModuleData(String moduleName)
    {

        return getBean().get(moduleName);
    }
}

属性文件: -     模块=仪表板

dashboard.templateNames=templateHtml1,templateHtml2,templateHtml3,templateHtml4
dashboard.templateHtml1=dashBoardLeftInsight.html,leftpanel
dashboard.templateHtml2=dcdcsFilterOptions.html.html,
dashboard.templateHtml3=advanceFilterOptions.html,advancedOptions
dashboard.templateHtml4=reportFilterOptions.html,reportdashboard

2 个答案:

答案 0 :(得分:2)

你确信:

 env.getProperty(eachmoduleName + "." + eachtemplateNames)

包含带逗号的值?如果没有,那么filterTemplate.setClassSelect(templateNameFields[1]);将为您提供例外。

编辑开始:

您的数据包含dashboard.templateHtml2=dcdcsFilterOptions.html.html,

确实包含','逗号,但常规String.split()将忽略逗号后面的空值。您可能希望使用String.split(",", -1)的“完整”版本,它将返回并清空表示输入字符串中空值的String值....

编辑结束:

你需要更多'防御性'编程,并期待意想不到的事情:

            // add a default value "" if the property is not set.
            String value = env.getProperty(eachmoduleName + "." + eachtemplateNames, "");
            String templateNameFields[] = value.split(",");
            if (templateNameFields.length != 2) {
                 // log an error or something
                 continue; // move on to valid data.
            }
            FilterTemplate filterTemplate=new FilterTemplate();
            filterTemplate.setTemplateHtml(templateNameFields[0]);
            filterTemplate.setClassSelect(templateNameFields[1]);

答案 1 :(得分:0)

在这一行

filterTemplate.setClassSelect(templateNameFields[1]);

它失败了,因为它没有“类选择”。

dashboard.templateHtml2=dcdcsFilterOptions.html.html,