【问题标题】:Got ArrayIndexOutOfBoundsException得到 ArrayIndexOutOfBoundsException
【发布时间】:2011-11-30 16:49:38
【问题描述】:

我正在从数据库中获取时间、价值、权重,ExperimenalImpl 中提供了相关方法。这些值显示在表格中。当我们选择特定的行时,相关的 getXxx() 方法将被调用。但是有些行正在正确获取数据。只有一行出现此异常。

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;
    }

【问题讨论】:

  • -1 4个问题后,你应该知道如何正确格式化你的问题。
  • ArrayIndexOutOfBounds 是一个非常明显的例外。您在某处使用超出数组范围的索引。如果您有任何作为程序员的野心,您必须学会自己调试这些简单的异常和错误。
  • newWeights[i] = (0.0D / 0.0D);该死的,那是邪恶的:P
  • 除了所有的cmets,请至少发布一个SSCCE。如果我们可以在不浪费太多时间的情况下运行您的代码,那么我们将有可能快速给出答案。
  • 我同意@PetarMinchev,这是一个非常清晰、直接的错误。另外,(0.0D/0.0D),请不要那样做,哈哈 :-)

标签: java swing


【解决方案1】:

你用这个长度this.experimentTimeValuePairs[0].length分配newWeights数组,

但在arrayCopy 中,您使用的是this.experimentWeigths.length

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

【讨论】:

    【解决方案2】:

    看着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. 
    

    我会首先调试你的程序,看看会发生哪些情况。

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 2017-09-05
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多