Saving Array in Multidimensional Array

Discussion in 'Plugin Development' started by PreFiXAUT, Feb 2, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PreFiXAUT

    I was trying to save an Array into an Multidimensional Array, but I only get Errors :/
    Show Spoiler

    Code:java
    1. public static String[][] getData()
    2. {
    3. checkFile();
    4. try {
    5. FileReader r = new FileReader(file);
    6. String read;
    7. String[][] temp = null;
    8. for (int i=0;(read = br.readLine()) != null; i++)
    9. {
    10. String[] tempArray = read.split(",");
    11. for (int o=0; tempArray[o] != null ; o++)
    12. {
    13. temp[i][o] = tempArray[o];
    14. }
    15. // Option 2:
    16. // temp[i] = tempArray;
    17. }
    18. br.close();
    19. r.close();
    20. return temp;
    21. } catch (Exception e) {
    22. System.out.println("DEBUG: getdata returns null");
    23. e.printStackTrace();
    24. return null;
    25. }
    26. }[/i][/i]


    Can someone tell me what's wrong on this one? I'm breaking my head since 2h on this >_>
    Thanks in advance.
     
  2. Offline

    Panjab

    You code isn't well readable, please format it again.
    And, what do you exactly want to do?
     
  3. Panjab What, the snipped is completely well formatted (one or two indentations are wrong, yes, but that's it).

    The reason you get errors, because you never create the array 'temp', just saying it is null.
     
  4. Offline

    PreFiXAUT

    @kumpalblase2 What do you mean with "not creating it"?
    Never worked with multidimensional Arrays yet, so I think they work like normal Arrays. Is that wrong? :O
     
  5. With that I mean creating an instance of it with a given size, which you don't do here.
     
  6. Offline

    Panjab

    kumpelblase2

    It wasn't. I think you didn't see the old code, which contained lot of BBCode. ;)
     
  7. Offline

    Rocoty

    Panjab Listen to kumpelblase2 . You are setting the temp[][] variable to null and later trying to set something to the
    Code:
    [i][o]
    index, but there is no
    Code:
    [i][o]
    index. In fact there isn't even an
    Code:
    [i]
    index. Because the variable is null.


    EDIT: PreFiXAUT Tahged the wrong person.
     
  8. Offline

    _Filip

    Array indexes have to be integers or variables referencing integers.
    Wha these guys are saying about temp bring null, just make the original value of temp {}.
     
  9. swampshark19 If things would be so easy. Then you would have an array with size 0 which will not give you a NPE but an IndexOutOfBunds exception and thus wouldn't solve the problem either.
     
  10. Offline

    _Filip

  11. swampshark19 I would suggest you trying that yourself. {} will result in <array-type>[0] and thus an empty array. Since arrays have a fixed length, you will always get an indexoutof bounds.
     
Thread Status:
Not open for further replies.

Share This Page