2011-10-04 103 views
-7

我正在从ExperimenalImpl获取数据库和相关方法的时间,值,权重。这些值显示在一个表格中。当我们选择特定的行时,相关的getXxx()方法将会调用。但有些行正在正确获取数据。只有一行给出这个例外。Got ArrayIndexOutOfBoundsException

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException 
      at java.lang.System.arraycopy(Native Method) 
      at com.integrativebioinformatics.processdb.pdbnt.IBExperimentalDataImpl.getWeights(IBExperimentalDataImpl.java:103) 
      at com.insilicalabs.processdb.ui.controllers.ExperimentalDataController.showExperiment(ExperimentalDataController.java:197) 
      at com.insilicalabs.processdb.ui.controllers.ExperimentalDataController.showExperimentAtIndex(ExperimentalDataController.java:184) 

代码:

public void setWeights(double[] experimentWeigths) { 
      if (experimentWeigths == null) throw new IllegalArgumentException("cannot take a null argument"); 
      this.experimentWeigths = new double[experimentWeigths.length]; 
      System.arraycopy(experimentWeigths, 0, this.experimentWeigths, 0, experimentWeigths.length); 
      for(int i=0;i<this.experimentWeigths.length;i++){ 
       System.out.println("Weights : "+ experimentWeigths[i]); 
      } 
     } 


public double[] getWeights() { 
     double[] newWeights = new double[this.experimentTimeValuePairs[0].length]; 
     if(this.experimentTimeValuePairs[0].length != 0){ 
      System.arraycopy(this.experimentWeigths, 0, newWeights, 0, this.experimentWeigths.length);//Here, the ArrayIndexOutOfBoundsException occured. 

      for (int i = this.experimentWeigths.length; i < this.experimentTimeValuePairs[0].length; ++i) { 
       newWeights[i] = (0.0D/0.0D); 
      } 
     } 

     return newWeights; 
    } 


    public double[][] getTimeValuePairs() { 
      double[][] newPairs = new double[2][this.experimentTimeValuePairs[0].length]; 
      System.arraycopy(this.experimentTimeValuePairs[0], 0, newPairs[0], 0, this.experimentTimeValuePairs[0].length); 
      System.arraycopy(this.experimentTimeValuePairs[1], 0, newPairs[1], 0, this.experimentTimeValuePairs[1].length); 

      return newPairs; 
     } 


public double[][] getTimeValuePairs() { 
     double[][] newPairs = new double[2][this.experimentTimeValuePairs[0].length]; 
     System.arraycopy(this.experimentTimeValuePairs[0], 0, newPairs[0], 0, this.experimentTimeValuePairs[0].length); 
     System.arraycopy(this.experimentTimeValuePairs[1], 0, newPairs[1], 0, this.experimentTimeValuePairs[1].length); 
     System.out.println("Called getTimeValuePairs() from IBExperimentalDataImpl : "+this.experimentTimeValuePairs); 
     return newPairs; 
    } 
+5

-1 4个问题后,您应该知道如何正确设置问题的格式。 – mre

+12

'ArrayIndexOutOfBounds'是一个非常明显的例外。某处你正在使用一个超出数组边界的索引。如果你有任何程序员的野心,你必须学会​​自己调试这种简单的例外和错误。 –

+5

'newWeights [i] =(0.0D/0.0D);'该死的,这是邪恶的:P –

回答

1

望着System.ArrayCopy API,它说:

Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified: 

The srcPos argument is negative. 
The destPos argument is negative. 
The length argument is negative. 
srcPos+length is greater than src.length, the length of the source array. 
destPos+length is greater than dest.length, the length of the destination array. 

我会被调试程序时,看到其中的这些情况发生的开始。

3

您分配newWeights阵列与这个长度this.experimentTimeValuePairs[0].length

arrayCopy您使用this.experimentWeigths.length

您可能应该将newWeights分配给this.experimentWeigths.length,因为这是您正在复制的内容。

相关问题