首页 > 解决方案 > 为什么在评估调试对象时新的 FileInputStream 返回 null

问题描述

我有这个代码:

InputStream is = new FileInputStream(new FileURL(fileName));
properties.load(is);

我有这个异常: java.lang.NullPointerException: inStream parameter is null

在调试中一步一步走,我看到我的“is”变量为空。但是当我评估(想法中的ctrl + u)时

new FileInputStream(new FileURL(fileName));

我得到了分配给引用属性文件的普通 FileInputStream 对象。请帮忙。

标签: javainputstream

解决方案


你可以这样做:

      try 
      {
         File file = new File( fileName ) ;
          if (!file.exists())
           return ;

        FileInputStream in = new FileInputStream( file ) ;

        try 
        {
          props.load( in ) ;
        } 
        finally 
        {
          in.close() ;
        }
     }  
     catch (Exception exc) 
     {
        if (debug)
        System.out.println( "ORB properties file " + fileName +
            " not found: " + exc) ;
     }

推荐阅读