Search code examples
javajcifs

Cannot Find Symbol: SmbFileInputStream instantiated in try-catch


This is my first post, so I apologize in advance for any formatting/content faux pax I'm surely about to commit.

I am trying to create an SmbFileInputStream to create a POI-HSSF workbook from. I am pulling a file from a network drive to take some input. The SmbFileInputStream is instantiated in a try-catch.

package com.tem;

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 loadWorkbook(String fileName){
    // create a new file input stream with the input file specified by fileName
    NtlmPasswordAuthentication auth = new      NtlmPasswordAuthentication("10.6.5.7","tyler","123");

    try{SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);}

    catch(FileNotFoundException f){
        SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);
        HSSFWorkbook wb = new HSSFWorkbook();
        System.err.println("Workbook not found at ["+fileName+"], so a blank workbook has been created");
        return wb;
    }


  // create a new org.apache.poi.poifs.filesystem.Filesystem
    Path path = Paths.get(fileName);

    POIFSFileSystem poifs = null;
    HSSFWorkbook wb = new HSSFWorkbook();

    try{poifs = new POIFSFileSystem(fin);}
        catch(java.io.IOException i){throw new IllegalArgumentException("loadWorkbook could not create poifs from filename ["+fileName+"]");}

    try{wb = new HSSFWorkbook(poifs);}
        catch(java.io.IOException i){throw new IllegalArgumentException("loadWorkbook could not read workbook from filename ["+fileName+"]");}

    try{fin.close();}
        catch(java.io.IOException i){throw new IllegalArgumentException("loadWorkbook could not close workbook from filename ["+fileName+"]");}

    return wb;
}

The exact error message I am receiving reads

com\tem\POIStuff.java:737: error: cannot find symbol
    try{poifs = new POIFSFileSystem(fin);}
                                    ^
symbol:   variable fin
location: class POIStuff
com\tem\POIStuff.java:741: error: cannot find symbol
    try{fin.close();}
        ^
symbol:   variable fin
location: class POIStuff
2 errors

I am working jointly with other programmers on this code, so some of it is slightly above my understanding. But I do not understand why the instantiation of SmbFileInputStream is failing.

Thank you!


Solution

  • The problem is that fin is out of scope when it gets to that line. Here are the scopes in your code.

    public static HSSFWorkbook loadWorkbook(String fileName){ //start method scope
        try{//start try scope
        } //end try scope
        catch(...) { //start catch scope
        }//end catch scope
    }//end method scope
    

    A variable is only usable within it's respective scope. Variables instantiated inside of the try scope don't exist outside of that try scope. I would combine your try statements.

    If you can't put everything into the same scope you can bring that variable out of the try block. i.e.:

    public static HSSFWorkbook loadWorkbook(String fileName){
        SmbFileInputStream fin = null;
        try{
            fin = new SmbFileInputStream(...);
        } catch(...) {
        }
        //do stuff with fin
    }
    

    Personally, I would rather put everything in the same scope and implement my own exception class:

    public static HSSFWorkbook loadWorkbook(String fileName)throws CustomException{
        try{
            SmbFileInputStream fin = new SmbFileInputStream(...);
            //do stuff with fin
            return wb;
        } catch(FileNotFoundException f) {
            //handle file not found
            throw new CustomException("File Not found ...", f);
        } catch(IOException io) {
            //handle io exception
            throw new CustomException("loadWorkBook failed", io);
        }
    }
    

    Also you shouldn't do this:

    try{
        SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);
    } catch(FileNotFoundException e){
        SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);
        //unreachable code
        ...
    }
    

    because if instantiating SmbFileInputStream throws a file not found exception in the try block, then it will definitely throw one in the catch block. Everything after the first line in the catch will never execute.