Java - 变量范围误解 - 导致NullPointerException错误

时间:2013-12-16 17:53:35

标签: java exception scope smb jcifs

我正在尝试从SmbFileInputStreamSmbFile创建NtlmPasswordAuthentication。我在try-catch块之外实例化SmbFileInputStream,因此范围不会局限于try-catch块。我为SmbFile做了同样的事情,名为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);
}

我收到的错误消息为

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

我目前的理解是,通过在第一个try-catch块之外实例化SmbFileInputStream,它的范围将扩展到第二个块。以“我们正在尝试运行的命令”开头的错误消息的输出是

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)

我提前为任何格式或连续性错误道歉。仍在学习Stack Overflow礼仪。

0 个答案:

没有答案