Java - Variable scope misunderstanding - Causing NullPointerException error

217 views Asked by At

I am trying to create an SmbFileInputStream from an SmbFile and NtlmPasswordAuthentication. I instantiate the SmbFileInputStream outside of my try-catch block so the scope will not be limited to the try-catch block. I do the same thing for my SmbFile, called sf.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import jcifs.smb.*;

public static HSSFWorkbook loadWorkbookOrFail(String fileName){
    // create a new file input stream with the input file specified by fileName
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("MY_DOMAIN","MY_USERNAME","MY_PASSWORD");
    SmbFile sf = null;
    SmbFileInputStream fin = null;


try {sf = new SmbFile("smb:\\\\LRIFS2\\Network\\Builders\\Everymoment\\reports\\finished\\Hearthside Ph. 3.xls", auth);
    } catch (Exception e){
        System.err.println("************************************************* The try block defining sf was caught. sf is null at this time");
        throw new IllegalArgumentException("Caught exception in loadWorkbookOrFail(): " + e.getClass().getName());
    }

try{SmbFileInputStream fin = new SmbFileInputStream(sf);}
    catch(Exception e){
            System.err.println("************************************************ SmbFile sf contains " + sf);
//              System.err.println("************************************************ SmbFileInputStream fin contains " + fin);
//        System.err.println("The command we are trying to run (Drawing a NullPointerException) reads: try{" + fin + " = new SmbFileInputStream(" + sf + ")");
        throw new IllegalArgumentException("Exception caught: " + e.getClass().getName());
    }
    return loadWorkbook(fileName);
}

The error message I am receiving reads

java.lang.IllegalArgumentException: Exception caught: java.lang.NullPointerException 
at com.tem.POIStuff.loadWorkbookOrFail(POIStuff.java:723) 

My current understanding is that by instantiating the SmbFileInputStream outside of the first try-catch block, that the scope of it would extend to the second block. The output of the error message that begins "The command we are trying to run" is

The command we are trying to run (Drawing a NullPointerException) reads: 
try{null = new SmbFileInputStream(smb:\\LRIFS2\Network\Builders\Everymoment\reports\finished\Hearthside Ph. 3.xls)

I apologize in advance for any formatting or continuity errors. Still learning Stack Overflow etiquette.

0

There are 0 answers