【问题标题】:NumberFormatException while parsing numeric value from property file?解析属性文件中的数值时出现 NumberFormatException?
【发布时间】:2015-10-18 00:49:29
【问题描述】:

我正在尝试从属性文件中获取 Java 程序中的值,该文件是使用服务器计算机上的 python 脚本编写的。

程序在本地运行良好,但在服务器上,当我尝试将属性文件中的整数值转换为程序的变量时,我得到 空指针异常例如

db_port=34

我愿意:

int a =  Integer.parseInt(p.getProperty("db_port").trim());

正如我之前提到的,这只会在服务器上引起问题,但在 localhost 上它可以正常工作。 这也发生在我的客户端和中间件机器上。 两者都有相同的错误,在从文件中检索属性值时停止。 但是在本地主机上我手动编写了一个属性文件,在服务器上它是使用这个脚本生成的。

我编写属性文件的方式是在具有值的配置文件中,我的脚本从那里获取值并将它们写入属性文件以放在不同的机器上(IN PYTHON):

prop_file_on_local_machine_S = '%s/middleware.properties'%directory
with open(prop_file_on_local_machine_S,'w') as f:

        for keys in configFile.options("middleware_props"):
          f.write("%s=%s\n"%(keys,configFile.get("middleware_props",keys)))

我已经在这个问题上停留了 10 个小时,感谢任何帮助。

编辑:我想提一下,字符串读得很好,整数不是。

编辑2:

String sp = "/local/r/properties/middleware.properties";
        Properties p = new Properties();

            p.load(new FileInputStream(sp));
            this.serverhost = p.getProperty("Serverhost");
            this.serverport = Integer.parseInt(p.getProperty("Serverport").trim());
            this.buffersize = Integer.parseInt(p.getProperty("Clientmessagesize").trim());

这就是我加载属性文件的方式。

编辑:当我自己在本地制作属性文件时,它可以工作,但是当我使用 python 生成的文件时,这里也会出错。 看起来有点像这样:

serverport =5555
serverhost =dryad04.ethz.ch
clientmessagesize =200

我猜这就是问题所在,我的 python 代码生成的属性文件。

错误:

java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at ch.ethz.rama.asl.client.ClientInstance.<init>(ClientInstance.java:26)
    at ch.ethz.rama.asl.tests.ClientThreadInstance.run(ClientThreadInstance.java:58)
    at java.lang.Thread.run(Thread.java:745)
Exception in thread "Thread-3" java.lang.NullPointerException
    at ch.ethz.rama.asl.tests.ClientThreadInstance.run(ClientThreadInstance.java:82)
    at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at ch.ethz.rama.asl.client.ClientInstance.<init>(ClientInstance.java:26)
    at ch.ethz.rama.asl.tests.ClientThreadInstance.run(ClientThreadInstance.java:58)
    at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at ch.ethz.rama.asl.client.ClientInstance.<init>(ClientInstance.java:26)
    at ch.ethz.rama.asl.tests.ClientThreadInstance.run(ClientThreadInstance.java:58)
    at java.lang.Thread.run(Thread.java:745)
java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:454)
    at java.lang.Integer.parseInt(Integer.java:527)
    at ch.ethz.rama.asl.client.ClientInstance.<init>(ClientInstance.java:26)
    at ch.ethz.rama.asl.tests.ClientThreadInstance.run(ClientThreadInstance.java:58)

clientinstance 的第 26 行是:

this.buffersize = Integer.parseInt(p.getProperty("Clientmessagesize"));

其他类的第 58 行是:

client = new ClientInstance(client_id);

所以是属性文件的问题,不知怎么弄乱了属性文件的生成。

【问题讨论】:

  • 调试它,检查p是否不为null,然后检查p.getPropery("db_port")是否不为null,如果p.getProperty("db_port")为则代码将抛出NPE空
  • 你是如何加载属性的?在你的 python 脚本创建属性文件之后,你是否对属性文件进行了 chmod,以便你的 java 程序可以正确读取它?
  • @BOND 我没有 chmod 它,为什么我必须这样做以及如何做?
  • @LoveMeow 因为您需要确保 java 程序具有访问属性文件的适当权限。只需使用 linux 命令:chmod 755 filename.properties。 755 是生产系统中常用的文件访问模式。
  • 尝试使用 p.get("property") 而不是 p.getProperty("property") 看看是否会有所不同...

标签: java properties-file numberformatexception


【解决方案1】:

显然您要查找的属性根本不在文件中,因此Properties.getProperty() 返回 null,因此String.trim() 获得 NPE。

查看堆栈跟踪:它应该包含在您的问题中。并检查您的属性名称。

编辑既然您终于透露实际问题是NumberFormatException,这只能意味着属性值不是数字。

但我已经受够了。

【讨论】:

  • 我认为是因为单词后面的空格?我会尽快发布错误文件。
  • @LoveMeow 什么空间?在什么词之后?您还应该在问题中发布属性文件本身。
  • 我已经发布了属性文件。
  • @LoveMeow 我同意:摆脱空间。请注意,您在原始问题中发布的内容根本不正确。你根本没有db_port=34
  • 这是我发布的一个例子,我有 2-3 个属性文件有同样的问题。
【解决方案2】:

问题是上面发布的python脚本从配置文件中获取了变量并将它们全部小写,正如您在上面看到的名称'Servername'变成servername,用这段代码打破了我的头脑导致我没有看到明显的,现在我不知道为什么我写的脚本会这样做,但我不是python专家,我现在只是把我所有的变量都做了小案例!

感谢所有试图提供帮助的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多