Topic: 求教高手啊(ArrayIndexOutOfBoundsException)

  Print this page

1.求教高手啊(ArrayIndexOutOfBoundsException) Copy to clipboard
Posted by: jfs771
Posted on: 2005-04-19 14:59

请尽量用准确的文字描述作为标题

public class Attributes {
private byte type;
private byte length;
private byte[] value=null; //不定长

public Attributes(int typ,String value){
this.type=(byte)typ;

this.value=value.getBytes();
}


int getlength(){
return value.length+2;
}

byte[] tobyte(){

byte[] newbyte=new byte[this.getlength()];
newbyte[0]=this.type;
newbyte[1]=this.length;
for(int i=0;i<this.getlength()-1;i++){
newbyte[i+2]=this.value[i];}
return newbyte;
}
}

public class DAFrame {
private byte code;
private byte identifier;
private byte length[]=new byte[2];
private byte Authenticator[]=new byte[16];
private Attributes attributes;

public DAFrame(int code,int i,byte[] authen,Attributes attri) {
this.code=(byte)code;
this.identifier=(byte)i;
this.setlength();
this.Authenticator=authen;
this.attributes=attri;
}

void setlength(){
this.length[0]=(byte)this.getlength();
int length1=this.getlength();
length1=length1/256
this.length[1]=(byte)length1;
}

int getlength(){
return 20+this.attributes.getlength();
}

byte[] tobyte(){
byte[] newbyte=new byte[this.getlength()];
newbyte[0]=this.code;
newbyte[1]=this.identifier;
newbyte[2]=this.length[0];
newbyte[3]=this.length[1];
for(int i=0;i<17;i++){
newbyte[i+4]=this.Authenticator[i]; } //错误处
byte[] attribyte=this.attributes.tobyte();
for(int i=0;i<attribyte.length;i++){
newbyte[i+20]=attribyte[i]; }
return newbyte;
}

}

tobyte的本意是将类中的各个数据域连接起来组成一个新的byte数组

在程序中调用DAFrame的tobyte方法时出现错误
java.lang.ArrayIndexOutOfBoundsException: 16  at rasclient.DAFrame.tobyte(DAFrame.java:82)
剩下的15处都是由这个引起的。。

本人的JAVA基本功实在是差的,由于毕设,才接触了两三个星期,
使用的Jbuilder9。
高手的给讲讲啊,或者给改改。小弟在此不胜感激!!!!

2.Re:求教高手啊。。。。。 [Re: jfs771] Copy to clipboard
Posted by: bluepure
Posted on: 2005-04-19 17:30



byte[] tobyte(){

byte[] newbyte=new byte[this.getlength()+2];

newbyte[0]=this.type;
newbyte[1]=this.length;
for(int i=0;i<this.getlength();i++){

newbyte[i+2]=this.value[i];}
return newbyte;
}


3.Re:求教高手啊。。。。。 [Re: jfs771] Copy to clipboard
Posted by: jfs771
Posted on: 2005-04-19 19:14

还是不行哦,现在的问题是调用还没到Attributes的tobyte时就出错了

DAFrame.tobyte( ) {
.
. // (错误出在之前)
Attributes.tobyte( );
.
.
}

原文程序的倒数第7行

恩,还是谢谢bluepure哦,不过想问下为什么要加2呢?在getlength中
我已经有 +2 了啊,

4.Re:求教高手啊(ArrayIndexOutOfBoundsException) [Re: jfs771] Copy to clipboard
Posted by: xiaoshitou
Posted on: 2005-05-11 15:56

我的java程序也出现了这样的问题
Internal compiler error: java.lang.ArrayIndexOutOfBoundsException: 0.
请指点

5.Re:求教高手啊(ArrayIndexOutOfBoundsException) [Re: jfs771] Copy to clipboard
Posted by: undefined
Posted on: 2005-05-12 19:59

public class Attributes {
  private byte type;
  private byte length;
  private byte[] value = null; //不定长

  public Attributes(int typ, String value) {
    this.type = (byte) typ;

    this.value = value.getBytes();
  }

  int getlengths() {
    return value.length + 2;
  }

  byte[] tobyte() {

    byte[] newbyte = new byte[this.getlengths()];
    newbyte[0] = this.type;
    newbyte[1] = this.length;
    for (int i = 0; i < this.getlengths() - 2; i++) {
      //System.out.println(this.value[i]);
      newbyte[i + 2] = this.value[i];
    }
    return newbyte;
  }
  public static void main(String [] args)
  {
    Attributes att= new Attributes(5,"abcd");
    System.out.println(att.tobyte());
  }
}

public class DAFrame {
  private byte code;
  private byte identifier;
  private byte length[] = new byte[2];
  private byte Authenticator[] = new byte[16];
  private Attributes attributes;

  public DAFrame(int code, int i, byte[] authen, Attributes attri) {
    this.code = (byte) code;
    this.identifier = (byte) i;
    this.Authenticator = authen;
    this.attributes = attri;
    this.setlength();
  }

  void setlength() {
    //System.out.println("$$$$$$$$$$$$$$");
    //System.out.println((byte) this.getlength());
    this.length[0] = (byte) this.getlength();
    
    int length1 = this.getlength();
    length1 = length1 / 256 ;
    this.length[1] = (byte) length1;
  }

  int getlength() {
    //System.out.println(new Attributes(5,"abc").getlengths());
    return 20 + this.attributes.getlengths();
  }

  byte[] tobyte() {
    byte[] newbyte = new byte[this.getlength()];
    newbyte[0] = this.code;
    newbyte[1] = this.identifier;
    newbyte[2] = this.length[0];
    newbyte[3] = this.length[1];
    for (int i = 0; i < this.Authenticator.length; i++) {
      newbyte[i + 4] = this.Authenticator[i];
    } //错误处
    byte[] attribyte = this.attributes.tobyte();
    for (int i = 0; i < attribyte.length; i++) {
      newbyte[i + 20] = attribyte[i];
    }
    return newbyte;
  }
  public static void main(String [] args)
  {
    byte [] b= new byte[3];
    b[0]='a';
    b[1]='b';
    b[2]='c';
    DAFrame daf = new DAFrame(5,12,b,new Attributes(22,"abc"));
    //daf.getlength();
    byte[] by=daf.tobyte();
    for(int i=0;i<by.length;i++)
    {
      System.out.print(by[i]+" * ");
      
    }
    //Attributes att= new Attributes(5,"abc");
    //System.out.println(att.getlengths());
  }

}

我给调好了,输出结果:

5 * 12 * 25 * 0 * 97 * 98 * 99 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 22 * 0 * 97 * 98 * 99 *

6.Re:求教高手啊(ArrayIndexOutOfBoundsException) [Re: jfs771] Copy to clipboard
Posted by: undefined
Posted on: 2005-05-12 20:10

要说一句的是,你的程序毛病太多了~~我不好总结具体错在哪里了~

首先最前面的有一个错误: DAFrame类的构造器里,你是如下所写的
this.setlength();
this.Authenticator=authen;
这样是肯定不行的, Attributes 类的对象还没被初始化,你就用this来调用setlength()方法,然后在setlength()方法里又会用到int length1=this.getlength();调用getlength()方法,而在getlength()方法里面return 20 + this.attributes.getlengths();这段代码的attributes属性根本还没有被初始化呢,所以就会在这里就会出先错误的.
然后你在tobyte()方法里的for循环中的循环次数控制在17次,而数组
private byte Authenticator[] = new byte[16];却被定义成16这样肯定会出现
java.lang.ArrayIndexOutOfBoundsException这个错误.
再往后还有错误
再往后就进到Attributes类里了错误依然在for循环里,也是循环次数和数组的索引指针的问题,你自己比对我改过的代码,好好看看自己的错在哪里~

7.Re:求教高手啊(ArrayIndexOutOfBoundsException) [Re: jfs771] Copy to clipboard
Posted by: jfs771
Posted on: 2005-05-18 10:37

恩,现在调试的时候真的发现很多错误都是这些问题
谢谢大家了,


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923