【问题标题】:Why new FileInputStream return null while in debug object is evaluated为什么在评估调试对象时新的 FileInputStream 返回 null
【发布时间】:2020-08-04 08:21:31
【问题描述】:

我有这个代码:

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

我有这个例外: java.lang.NullPointerException: inStream 参数为空

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

new FileInputStream(new FileURL(fileName));

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

【问题讨论】:

  • 你的FileURL类从何而来?
  • 自定义项目类
  • 它扩展了 java.io.File
  • 如果你的代码真的像上面那样,这意味着对properties.load(is)的调用是直接在之前创建FileInputStream之后进行的,那么此时is不可能为空。对象的构造可能会因异常而失败,也可能会成功,但永远不会产生 null。
  • @codeflush.dev 的分析是对的。我认为您应该提供更多代码。 NullPointerException 的堆栈跟踪也会有所帮助。

标签: java inputstream


【解决方案1】:

你可以这样做:

      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) ;
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2019-01-19
    • 2020-09-13
    • 1970-01-01
    • 2015-10-17
    相关资源
    最近更新 更多