-4

I am just wondering if it is possible to have a public static array that can be accessed and edited by all classes.

To see if it works I have tried to access it but I just get an error.

public class FileManager {
    public static String[][] cassetteArray = new String[10][6];
}

The command I use to check if it works is;

FileManager.cassetteArray[0][1].length();

Instead I get the following error;

Exception in thread "main" java.lang.NullPointerException
at AssignmentStage1.UserInterface.userMenu(UserInterface.java:35)
Sir. Hedgehog
  • 1,260
  • 3
  • 17
  • 40
  • 3
    It does work. But you probably don't fill the array with values, so the array elements are all ``null``. And calling ``length()`` on a ``null`` reference... – f1sh Oct 04 '18 at 11:10
  • You haven't put any `String`s into the elements of `FileManager.cassetteArray` in any of the code you've shown, nor, apparently in any other code you haven't shown either. – Kevin Anderson Oct 04 '18 at 11:11
  • Ohh Ok! Thanks! I will try and give it some values. My question is answered though. – overduealteration Oct 04 '18 at 11:12

1 Answers1

1

This object has a null value FileManager.cassetteArray[0][1] so that's the reason of exception. However you can use singleton for that purpose.

NikNik
  • 2,191
  • 2
  • 15
  • 34