为什么我一直收到错误 java.lang.ArrayIndexOutOfBoundsException

问题描述 投票:0回答:0

这段代码是用Java编程语言编写的,使用Firebase Firestore数据库来检索数据。代码获取多个用户的动态数据,每个用户有多个测试。数据包括停留时间、飞行时间 2 和飞行时间 3,以及测试名称。

代码循环遍历每个用户并检索每个用户的动态数据。然后循环遍历该用户的每个测试并检索停留时间、飞行时间 2、飞行时间 3 和测试名称。检索到的数据存储在各种列表中。在为所有用户循环所有测试后,代码创建属性和类并将数据存储在 SharedPreferences 对象中。

但似乎我一直收到此错误:

 java.lang.ArrayIndexOutOfBoundsException: length=29; index=29
        at weka.core.DenseInstance.setValue(DenseInstance.java:242)
        at com.example.projetm.data.Getdata$1$1$2.<init>(Getdata.java:188)
        at com.example.projetm.data.Getdata$1$1.onComplete(Getdata.java:185)

这是我的代码:

` public String Class(Context 上下文) 抛出异常 {

    //hna nkharej ga3 les utilisateurs
    db.collection("users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                int numusers = task.getResult().size(); //hna nedi nombre des utilisateurs
                Log.d("nombre d'utilisateurs", String.valueOf(numusers));
                for (QueryDocumentSnapshot doc : task.getResult()) { //apres nboucle les utilisateurs

                    String id = doc.getId(); // nedi id ta3ah
                    //apres nedi dynamique  de chaque utilisateurs
                    db.collection("users").document(id).collection("dynamic").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
                                y++; // w koul ma nedi test nzid f y

                                nombre_instance += task.getResult().size(); //w nmed l nombre instance le nombre de test
                                Log.d("getdata ch7al mel instance", String.valueOf(nombre_instance));

                                for (QueryDocumentSnapshot document : task.getResult()) { //apres nboucle les tests
                                    // Log.d("data type", document.get("dwell time").getClass().getSimpleName());
                                    List<Long> dwellTimes = (List<Long>) document.get("dwell time");
                                    //ArrayList<Long> dwellTimes = (ArrayList<Long>) document.get("dwell time");
                                    dwell.addAll(dwellTimes);
                                    List<Long> flight2 = (List<Long>) document.get("dwell time");
                                    //ArrayList<Long> flightTimes2 = (ArrayList<Long>) document.get("flight time 2");
                                    flightm2.addAll(flight2);
                                    List<Long> flight3 = (List<Long>) document.get("dwell time");
                                    //ArrayList<Long> flightTimes3 = (ArrayList<Long>) document.get("flight time 3");
                                    flightm3.addAll(flight3);

                                    name.add(document.getString("nom"));

                                    Log.d("getdata1", "y = " + y);
                                }

                                    if (y == numusers) { //apres ki y yweli 9ad le nombre des utilisateurs sema da ga3  les tests ydir la suite
                                        Log.d("getdata", "wsel");
                                        //hna apres ncreeo  data
                                        preferences = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
                                        //nsamo les atributs
                                        final Attribute Attribute1 = new Attribute("dwelltime1");
                                        final Attribute Attribute2 = new Attribute("dwelltime2");
                                        final Attribute Attribute3 = new Attribute("dwelltime3");
                                        final Attribute Attribute4 = new Attribute("dwelltime4");
                                        final Attribute Attribute5 = new Attribute("dwelltime5");
                                        final Attribute Attribute6 = new Attribute("dwelltime6");
                                        final Attribute Attribute7 = new Attribute("dwelltime7");
                                        final Attribute Attribute8 = new Attribute("dwelltime8");
                                        final Attribute Attribute9 = new Attribute("dwelltime9");
                                        final Attribute Attribute10 = new Attribute("dwelltime10");
                                        final Attribute Attribute11 = new Attribute("flighttime2_1");
                                        final Attribute Attribute12 = new Attribute("flighttime2_2");
                                        final Attribute Attribute13 = new Attribute("flighttime2_3");
                                        final Attribute Attribute14 = new Attribute("flighttime2_4");
                                        final Attribute Attribute15 = new Attribute("flighttime2_5");
                                        final Attribute Attribute16 = new Attribute("flighttime2_6");
                                        final Attribute Attribute17 = new Attribute("flighttime2_7");
                                        final Attribute Attribute18 = new Attribute("flighttime2_8");
                                        final Attribute Attribute19 = new Attribute("flighttime2_9");
                                        final Attribute Attribute20 = new Attribute("flighttime3_1");
                                        final Attribute Attribute21 = new Attribute("flighttime3_2");
                                        final Attribute Attribute22 = new Attribute("flighttime3_3");
                                        final Attribute Attribute23 = new Attribute("flighttime3_4");
                                        final Attribute Attribute24 = new Attribute("flighttime3_5");
                                        final Attribute Attribute25 = new Attribute("flighttime3_6");
                                        final Attribute Attribute26 = new Attribute("flighttime3_7");
                                        final Attribute Attribute27 = new Attribute("flighttime3_8");
                                        final Attribute Attribute28 = new Attribute("flighttime3_9");
                                        List<String> classes = new ArrayList<String>() {
                                            {
                                                add("user1");
                                                add("user2");
                                                add("user3");
                                              
                                            }
                                        };  //w ndir les classe

                                        //apres n ajoutou les attributs f fastvecteur
                                        Attribute AttributeClass = new Attribute("class", classes);
                                        FastVector fastVector = new FastVector(nombre_attribute);
                                        fastVector.addElement(Attribute1);
                                        fastVector.addElement(Attribute2);
                                        fastVector.addElement(Attribute3);
                                        fastVector.addElement(Attribute4);
                                        fastVector.addElement(Attribute5);
                                        fastVector.addElement(Attribute6);
                                        fastVector.addElement(Attribute7);
                                        fastVector.addElement(Attribute8);
                                        fastVector.addElement(Attribute9);
                                        fastVector.addElement(Attribute10);
                                        fastVector.addElement(Attribute11);
                                        fastVector.addElement(Attribute12);
                                        fastVector.addElement(Attribute13);
                                        fastVector.addElement(Attribute14);
                                        fastVector.addElement(Attribute15);
                                        fastVector.addElement(Attribute16);
                                        fastVector.addElement(Attribute17);
                                        fastVector.addElement(Attribute18);
                                        fastVector.addElement(Attribute19);
                                        fastVector.addElement(Attribute20);
                                        fastVector.addElement(Attribute21);
                                        fastVector.addElement(Attribute22);
                                        fastVector.addElement(Attribute23);
                                        fastVector.addElement(Attribute24);
                                        fastVector.addElement(Attribute25);
                                        fastVector.addElement(Attribute26);
                                        fastVector.addElement(Attribute27);
                                        fastVector.addElement(Attribute28);
                                        fastVector.addElement(AttributeClass);
                                        //apres ncrreo instance avec nom dynamic w nmedo le nombre d'instance
                                        Instances instances = new Instances("dynamics", fastVector, nombre_instance);
                                        instances.setClassIndex(instances.numAttributes() - 1);


                                        String nom = String.valueOf(name);
                                        // Log.d("taille", String.valueOf(finalDwell.size()));

                                        for (int i = 0; i < nombre_instance; i++) {
                                            List<Long> finaldwell = dwell;
                                            List<Long> finalflight2 = flightm2;
                                            List<Long> finalflight3 = flightm3;

                                            DenseInstance denseInstance = new DenseInstance(instances.numAttributes()) {
                                                {
                                                    for (int j = 0; j < finaldwell.size(); j++) {
                                                        setValue(j, preferences.getLong("dwelltime" + (j+1), finaldwell.get(j)));
                                                    }
                                                    for (int j = 0; j < finalflight2.size(); j++) {
                                                        setValue(j + finaldwell.size(), preferences.getLong("flighttime2_" + (j+1), finalflight2.get(j)));
                                                    }
                                                    for (int j = 0; j < finalflight3.size(); j++) {
                                                        setValue(j + finaldwell.size() + finalflight2.size(), preferences.getLong("flighttime3_" + (j+1), finalflight3.get(j)));
                                                    }
                                                }
                                            }; Log.d("zzz", "i" + i);
                                            instances.add(denseInstance);
                                        }

                                        Log.d("getdata", instances.toString());    // w n affichi les donnees f log
                                    }`

我尝试了一切关于如何解决这个问题的任何建议或想法?我将非常感谢我的最后一年项目

java arrays firebase weka
© www.soinside.com 2019 - 2024. All rights reserved.