Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Java, checking if variable is initialized with if x == null give me variable might not have been initialized error

I want to check if a variable is initialized before using it.

        java.io.File yDriveFolder;

        //move files to temp folder 
        for(int i=0; i<filesForDocument.length; i++){
          ...
          yDriveFolder = new java.io.File(yDrivePath + "/");
          ...
        }
        //delete source folder when it is done  
        if(filesForDocument.length> 1){
          if(yDriveFolder != null){
            yDriveFolder.delete();
          }
        }

error: variable yDriveFolder might not have been initialized

How should I check if a variable is initialized before using it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Write something like java.io.File yDriveFolder = null; Then the compiler knows you know it is not initialized.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading