0

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.

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
Tyler Wood
  • 35
  • 8
  • which line is line 723? – Haozhun Dec 16 '13 at 17:57
  • You're shooting yourself in the foot by catching exceptions and ignoring their stack trace. Use `throw new IllegalArgumentException(e);`, and don't catch Exception, so that the stack trace of NullPointerException is printed. – JB Nizet Dec 16 '13 at 18:00
  • Simply remove all try-catch blocks. If there are any checked exceptions involved, declare them. – Marko Topolnik Dec 16 '13 at 18:22
  • Hi @JBNizet. I apologize, but would you be able to expand a tiny bit? The only place I currently know how to throw an IllegalArgumentException would be in the catch block of a try-catch construct. Wouldn't any code after throwing the exception be unreachable, including the final return statement? – Tyler Wood Dec 16 '13 at 21:52
  • You should only catch declared, checked exceptions, that you can handle correctly. Catching the class Exception catches everything, included unexpected exceptions that should not be caught, but bubble up the stack until they're printed with a meaningful stack trace. If you catch and rethrow an exception, then wrap the original exception as shown in my above comment so that its message and stack trace aren't lost. If you had the stack trace of the NPE, you could tell precisely where it comes from, and you could fix the bug so that the NPE isn't thrown anymore. – JB Nizet Dec 17 '13 at 07:04

0 Answers0