Получил ArrayIndexOutOfBoundsException - PullRequest
       0

Получил ArrayIndexOutOfBoundsException

0 голосов
/ 04 октября 2011

Я получаю время, значение, веса из базы данных и соответствующие методы доступны в 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;
    }

Ответы [ 2 ]

3 голосов
/ 04 октября 2011

Вы выделяете массив newWeights этой длины this.experimentTimeValuePairs[0].length,

, но в arrayCopy вы используете this.experimentWeigths.length.

Вы, вероятно, должны выделить newWeights с помощьюthis.experimentWeigths.length, потому что это то, что вы копируете.

1 голос
/ 04 октября 2011

Глядя на 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. 

Я хотел бы начать с отладки вашей программы и посмотреть, какая из этих ситуаций возникает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...