0

I am creating an encryption/decryption program that uses multiple classes. I have one class that is the UI, and uses a JFrame form with a file selector, and another class that encrypts/decrypts the selected file. I am encountering a problem when I try to use the java.io.File variable declared in the UI class in the encryption class.

File selector code:

public static void actionEncrypt() {
    encrypt = true;
    int retVal = selectFile.showOpenDialog(null);
    if (retVal == selectFile.APPROVE_OPTION) {
        java.io.File file = selectFile.getSelectedFile();
        System.out.println(file);
        Crypt.encrypt();
    }
}

Variable declaration code:

public static boolean encrypt;
public static java.io.File file;

File reading code:

public static void encrypt() {
    System.out.println(MainUI.file);
    try {
        Scanner filescan = new Scanner(MainUI.file);
        int count = 0;
        while (filescan.hasNextLine()) {
            count++;
            filescan.nextLine();
        }
    } catch (FileNotFoundException e) {
        System.out.println("File not found!");
    }
}

When I run this code, I get a NullPointerException because the value of the File variable while the file reading code is running is null. This is because it was declared as static in the variable declaration code, which overwrites the value that was declared in the method actionEncrypt. If I don't make the variable static, I get a Cannot find symbol when I try to access it from the other class. However, I cannot declare the variable as static within the method actionEncrypt, because it gives me an illegal start of expression. Does anyone know how to declare a variable in a method as static without hiding a field, or any other way to use the File variable in another class?

Thanks in advance,

Santiago

10
  • 3
    "The variable must be static in order to be accessed from other classes." -- this is wrong, so very wrong. Your variable should most definitely not be static. I'm not saying that static variables should never be used, and in fact there are many situations where they are quite helpful, but I am saying that this is not one of those situations. Instead if it must be accessible from other classes, make it an instance variable, and consider giving this class a getter method. Commented Jun 1, 2014 at 23:23
  • Show us a little bit more code, and we'll show you how to re-arrange it so you either do not need to use static variables, or ensure that initialization happens first. Commented Jun 1, 2014 at 23:28
  • @Hovercraft Full Of Eels - Sorry, I didn't word it right. What I meant was that in these particular circumstances, the variable has to be static because or else I get a Cannot find symbol when trying to access it from the encryption class. I edited the question with more precise wording. Commented Jun 1, 2014 at 23:29
  • It sounds like you are using the class MainUI without instantiating it. In that case, you should have some code in the MainUI that initializes the variable file, before the encryption code calls it. Commented Jun 1, 2014 at 23:30
  • How do I initialize the variable file? Isn't it already initialized when it is set to the file selected? Commented Jun 1, 2014 at 23:32

1 Answer 1

1

I think I see the error in your code.

Change the following line so that you initialize MainUI.file instead. Currently you are creating a local variable called file and initializing that one instead of MainUI.file, which is what you probably intended to initialize.

    java.io.File file = selectFile.getSelectedFile();

Replace it with.

    MainUI.file = selectFile.getSelectedFile();
Sign up to request clarification or add additional context in comments.

3 Comments

So I am assuming that I add the replacement code into the encryption class?
@Santiago, Delete the first line, and put the second line exactly where the first line used to be.
Ok. I tried putting it in either class, and it works in both. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.