java.lang.ArrayIndexOutOfBoundsException: 2

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/you23hai45/article/details/49826437

1、错误描述

java.lang.ArrayIndexOutOfBoundsException: 2
	at report.service.impl.ExportServiceImpl$7.setSheetValue(ExportServiceImpl.java:2649)
	at utils.ExcelUtil.getWorkbook(ExcelUtil.java:45)
	at report.service.impl.ExportServiceImpl.exportStation(ExportServiceImpl.java:2678)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
	at com.sun.proxy.$Proxy78.exportStation(Unknown Source)
	at report.controller.ExportController.exportStationStatis(ExportController.java:765)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at filter.TranscationFilter.doFilter(TranscationFilter.java:32)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at filter.ContentFilter.doFilter(ContentFilter.java:64)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

2、错误原因

String[] ch = {"A","B","C","D","E"};
int[] userId = {0,1};
for(int a=0;a<ch.length;a++)
{
     user.setId(userId(a));
     for(int b=0;b<ch.length;b++)
     {

     }
}

    ch字符串数组的长度是5,userId数组的长度是2,在第一层for循环中,给id赋值时用的是userId(a),超出userId数组长度


3、解决办法

     正确做法:

String[] ch = {"A","B","C","D","E"};
int[] userId = {0,1,2};
for(int a=0;a<userId.length;a++)
{
     user.setId(userId(a));
     for(int b=0;b<ch.length;b++)
     {

     }
}

    将最外层循环a的最大值为userId.length-1

运行报错:java.lang.ArrayIndexOutOfBoundsException: 2

09-30

[code=java]package ie.dit.comp.lukejia.fyp.swn;rnrn import java.io.BufferedReader;rn import java.io.File;rn import java.io.FileReader;rn import java.util.HashMap;rn import java.util.Iterator;rn import java.util.Set;rn import java.util.Vector;rnrn public class SWN3 rn private String pathToSWN = "D:\\study\\Quarter1\\IRDM\\Project\\resource\\SWN.txt";rn private HashMap _dict;rnrn public SWN3()rnrn _dict = new HashMap();rn HashMap> _temp = new HashMap>();rn tryrn BufferedReader csv = new BufferedReader(new FileReader(pathToSWN));rn String line = ""; rn while((line = csv.readLine()) != null)rn rn String[] data = line.split("\t");rn Double score = Double.parseDouble(data[2])-Double.parseDouble(data[3]);rn String[] words = data[4].split(" ");rn for(String w:words)rn rn String[] w_n = w.split("#");rn w_n[0] += "#"+data[0];rn int index = Integer.parseInt(w_n[1])-1;rn if(_temp.containsKey(w_n[0]))rn rn Vector v = _temp.get(w_n[0]);rn if(index>v.size())rn for(int i = v.size();i v = new Vector();rn for(int i = 0;i temp = _temp.keySet();rn for (Iterator iterator = temp.iterator(); iterator.hasNext();) rn String word = (String) iterator.next();rn Vector v = _temp.get(word);rn double score = 0.0;rn double sum = 0.0;rn for(int i = 0; i < v.size(); i++)rn score += ((double)1/(double)(i+1))*v.get(i);rn for(int i = 1; i<=v.size(); i++)rn sum += (double)1/(double)i;rn score /= sum;rn String sent = ""; rn if(score>=0.75)rn sent = "strong_positive";rn elsern if(score > 0.25 && score<=0.5)rn sent = "positive";rn elsern if(score > 0 && score>=0.25)rn sent = "weak_positive";rn elsern if(score < 0 && score>=-0.25)rn sent = "weak_negative";rn elsern if(score < -0.25 && score>=-0.5)rn sent = "negative";rn elsern if(score<=-0.75)rn sent = "strong_negative";rn _dict.put(word, score);rn rn rn catch(Exception e)e.printStackTrace(); rn rnrnpublic Double extract(String word)rnrn Double total = new Double(0);rn if(_dict.get(word+"#n") != null)rn total = _dict.get(word+"#n") + total;rn if(_dict.get(word+"#a") != null)rn total = _dict.get(word+"#a") + total;rn if(_dict.get(word+"#r") != null)rn total = _dict.get(word+"#r") + total;rn if(_dict.get(word+"#v") != null)rn total = _dict.get(word+"#v") + total;rn return total;rnrnrnpublic static void main(String[] args) rn SWN3 test = new SWN3();rn String sentence="Hello have a Super awesome great day";rn String[] words = sentence.split("\\s+"); rn double totalScore = 0;rn for(String word : words) rn word = word.replaceAll("([^a-zA-Z\\s])", "");rn if (test.extract(word) == null)rn continue;rn totalScore += test.extract(word);rn rn System.out.println(totalScore);rnrnrnrn[/code]rnrn报错rnjava.lang.ArrayIndexOutOfBoundsException: 2rn at ie.dit.comp.lukejia.fyp.swn.SWN3.(SWN3.java:25)rn at ie.dit.comp.lukejia.fyp.swn.SWN3.main(SWN3.java:101)rn0.0

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试