Java中的索引出界错误。 [英] Index Out of Bound Error In Java.

查看:139
本文介绍了Java中的索引出界错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解Java Code,它在编译索引超出范围时会出错。我试图弄清楚为什么它会给出这个错误但是失败了。任何人都可以帮我解释为什么这段代码会给出索引超出范围的错误?



 尝试 {
ORB orb = ORB.init(args, null );
POA rootpoa = POAHelper.narrow(orb
.resolve_initial_references( RootPOA ));
rootpoa.the_POAManager()。activate();

ProfilerServant profilerServant = new ProfilerServant(args [ 4 ],
args [ 5 ]。equals( true< /跨度>));
org.omg.CORBA。 Object ref = rootpoa
.servant_to_reference(profilerServant) );
Profiler pref = ProfilerHelper.narrow( ref );

org.omg.CORBA。 Object objRef = orb
.resolve_initial_references( NameService);
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

字符串 name = 探查;
NameComponent path [] = ncRef.to_name(name);
ncRef.rebind(path,pref);

orb.run();
}

catch (例外e){
System.err.println( 错误: + e.getMessage());
e.printStackTrace(System。 out );
}
}





这是ProfilerServant类

与构造函数< br $>


< pre lang =c#> public class ProfilerServant扩展ProfilerPOA {

boolean cacheEnabled;

ServerParser解析器;
HashMap< String,Integer> songCache;
HashMap< String,User> userCache;

ProfilerServant(String fileName,boolean cacheEnabled){
this.cacheEnabled = cacheEnabled;
parser = new ServerParser(fileName);
songCache = new HashMap< String,Integer>();
userCache = new HashMap< String,User>();
init();
}

解决方案

简单索引越界表示在列表或数组中,你试图访问一个不存在的元素。



示例:在一个5个整数的数组中,编号从0到4,你试着读取整数5,哪个不存在。



告诉错误信息是好的,但是告诉错误信息哪里更好,这有助于缩小研究范围。



如果没有看到阵列,那么进一步帮助你将会很复杂。

如果你收到错误消息编译时,你的数组可能是一个常量。

如果在运行时收到错误信息,请使用调试器并检查发生错误的变量。


< pre lang =java> ProfilerServant profilerServant = new ProfilerServant(args [ 4 ],args [ 5 ]。EQU als( true));



你确定 args 有6个元素吗?


I am trying to understand Java Code which gives an error on compiling that index out of bound. I am trying to figure out why it is giving this error but failed. Can anyone help me that why this code is giving index out of bound error?

try {
			ORB orb = ORB.init(args, null);
			POA rootpoa = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();

			ProfilerServant profilerServant = new ProfilerServant(args[4],
					args[5].equals("true"));
			org.omg.CORBA.Object ref = rootpoa
					.servant_to_reference(profilerServant);
			Profiler pref = ProfilerHelper.narrow(ref);

			org.omg.CORBA.Object objRef = orb
					.resolve_initial_references("NameService");
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			String name = "Profiler";
			NameComponent path[] = ncRef.to_name(name);
			ncRef.rebind(path, pref);

			orb.run();
		}

		catch (Exception e) {
			System.err.println("ERROR: " + e.getMessage());
			e.printStackTrace(System.out);
		}
	}



Here is the ProfilerServant Class
with the constructor

<pre lang="c#">public class ProfilerServant extends ProfilerPOA {

	boolean cacheEnabled;

	ServerParser parser;
	HashMap<String, Integer> songCache;
	HashMap<String, User> userCache;

	ProfilerServant(String fileName, boolean cacheEnabled) {
		this.cacheEnabled = cacheEnabled;
		parser = new ServerParser(fileName);
		songCache = new HashMap<String, Integer>();
		userCache = new HashMap<String, User>();
		init();
	}

解决方案

Simple index out of bound means that in a list or an array, you try to access an element that doesn't exist.

Example: in an array of 5 integers numbered from 0 to 4, you try to read integer number 5, which does not exist.

Telling what is the error message is nice, but telling where is the error message is better, it helps to narrow the research.

Without seeing the array involved, it will be complicated to help you further.
If you get the error message at compile time, you array is probably a constant.
If you get the error message at runtime, use the debugger and check the variables where error occurs.


ProfilerServant profilerServant = new ProfilerServant(args[4], args[5].equals("true"));


Are you certain that args has 6 elements?


这篇关于Java中的索引出界错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆