简体   繁体   中英

Why does my App crash after i set an array?

I used an array the whole time for my app. But i only set the values after the app was created, with:

      public String[][] stunde = new String [6][13];
      public String[][] lehrer = new String [6][13];


    stunde[1][1]= "SZ";
    stunde[2][1]= "Bi";
    stunde[3][1]= "";
    stunde[4][1]= "DG2";
    stunde[5][1]= "";


    lehrer[1][1]= "Gt";
    lehrer[2][1]= "Pön";
    lehrer[3][1]= "";
    lehrer[4][1]= "Lc";
    lehrer[5][1]= "";

but now i wanted to set these values before, so that i could use them in another method. Like this:

  public  String[][] stunde = {
            {"SZ", "SZ", "Ku", "Ku", "M", "M", "GeL1", "EL2"},
           {"Bi", "Bi", "EL2", "EL2", "Pl", "Pl","DG2","If"},
            {"", "", "EL2","EL2", "","","M","Bi"},
            {"DG2", "DG2", "","", "GeL1","GeL1","Pl","Ku"},
            {"", "", "GeL1","GeL1", "If","If","","SZ","","","Sp","Sp"}
    };

But after i tried it like in the second code my app started to crash after i opened it.

Any ideas why?

Your old code used indexes on the top-level array starting at one, not zero. It looks like the rest of your app relies on that numbering as well.

Add a "fake" row and column to fix the problem:

public  String[][] stunde = {
/*0*/  {"", "", "", "", "", "", "", "", ""},
/*1*/  {"", "SZ", "SZ", "Ku", "Ku", "M", "M", "GeL1", "EL2"},
/*2*/  {"", "Bi", "Bi", "EL2", "EL2", "Pl", "Pl","DG2","If"},
/*3*/  {"", "", "", "EL2","EL2", "","","M","Bi"},
/*4*/  {"", "DG2", "DG2", "","", "GeL1","GeL1","Pl","Ku"},
/*5*/  {"", "", "", "GeL1","GeL1", "If","If","","SZ","","","Sp","Sp"}
};

Now your row 0 remains unused, and the rest of your app that wants rows 1 through 5 would find the data where it used to be before. Same goes for column numbering.

Note: Once you get this under control and the app no longer crashes, a long-term approach to this would be changing the code that uses the arrays to index 0..4 instead of 1..5 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM