java - 收到错误 java.lang.ArrayIndexOutOfBoundsException : 1

标签 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

最佳答案

您是否确信:

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

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

编辑开始:

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

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

编辑结束:

您需要更加“防御性”地编程,并期待意外情况:

            // 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]);

关于java - 收到错误 java.lang.ArrayIndexOutOfBoundsException : 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19988033/

相关文章:

Java 正则表达式构建

java - hibernate 加载时间很长(我认为 hibernate 加载所有项目)

java - 绘图后随机出现 GL_OUT_OF_MEMORY 错误

Java 抽象方法参数类型

java - Spring MVC MongoDB 配置

java - FilterChainProxy 不会 Autowiring

java - 返回一个包含 CompletableFuture 列表的 CompletableFuture

java - 在 Spring security 中使用 @PreAuthorize 以及角色和权限

java - Validator 的目标无效 - Spring MVC

java - 使用 Saxon9he 进行 XSLT 转换抛出 java.net.URISyntaxException : Illegal character in path at index