错误线程“main”中的异常java.lang.NumberFormatException:null

时间:2015-01-21 18:30:38

标签: java exception null numbers

当我尝试运行我的代码时出现此错误:

Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at edu.ndhu.bm.healthow.HealThowConstant.<init>(HealThowConstant.java:91)
at edu.ndhu.bm.healthow.HealThow.main(HealThow.java:511)

我搜索了类似的答案,但我仍然无法修复它。这必须与数据类型的转换有关,但我不知道如何解决问题。 我是Java编程的新手,所以任何帮助都将不胜感激。谢谢。这是代码:

package edu.ndhu.bm.healthow;

import java.util.Properties;

public class HealThowConstant
{
  public final int n;
  public final int m;
  public final int d;
  public final int t;

  public final double taux;
  public final double etax;
  public final double ax;
  public final double bx;
  public final double alphax;
  public final double betax;
  public final double qx;
  public final double add_taux;
  public final double tauUpperBoundx;
  public final double lux;
  public final double minlux;
  public final double gux;
  public final double mingux;

  public final double tauy;
  public final double etay;
  public final double ay;
  public final double by;
  public final double alphay;
  public final double betay;
  public final double qy;
  public final double add_tauy;
  public final double tauUpperBoundy;
  public final double luy;
  public final double minluy;
  public final double guy;
  public final double minguy;

  public final double tauw;
  public final double etaw;
  public final double aw;
  public final double bw;
  public final double alphaw;
  public final double betaw;
  public final double qw;
  public final double add_tauw;
  public final double tauUpperBoundw;
  public final double luw;
  public final double minluw;
  public final double guw;
  public final double minguw;

  public final String nfetay;

  public final int ant;
  public final int iteration;

  public final String parameter;

  public final int fl;
  public final int fu;
  public final int pl;
  public final int pu;
  public final int cl;
  public final int cu;
  public final int rl;
  public final int ru;
  public final int Cl;
  public final int Cu;
  public final int ul;
  public final int uu;
  public final int al;
  public final int au;

  public final String scenario_generate;

  public final int scenario;
  public final int average;
  public final int variance;

  public final int stage;

  public final double runtime;

  public HealThowConstant(Properties properties)

  {
  n = Integer.parseInt(properties.getProperty("n"));
  m = Integer.parseInt(properties.getProperty("m"));
  d = Integer.parseInt(properties.getProperty("d"));
  t = Integer.parseInt(properties.getProperty("t"));

  taux = Double.parseDouble(properties.getProperty("taux"));
  etax = Double.parseDouble(properties.getProperty("etax"));
  ax = Double.parseDouble(properties.getProperty("ax"));
  bx = Double.parseDouble(properties.getProperty("bx"));
  alphax = Double.parseDouble(properties.getProperty("alphax"));
  betax = Double.parseDouble(properties.getProperty("betax"));
  qx = Double.parseDouble(properties.getProperty("qx"));
  add_taux = Double.parseDouble(properties.getProperty("add_taux"));
  tauUpperBoundx = Double.parseDouble(properties.getProperty("tauUpperBoundx"));
  lux = Double.parseDouble(properties.getProperty("lux"));
  minlux = Double.parseDouble(properties.getProperty("minlux"));
  gux = Double.parseDouble(properties.getProperty("gux"));
  mingux = Double.parseDouble(properties.getProperty("mingux"));

  tauy = Double.parseDouble(properties.getProperty("tauy"));
  etay = Double.parseDouble(properties.getProperty("etay"));
  ay = Double.parseDouble(properties.getProperty("ay"));
  by = Double.parseDouble(properties.getProperty("by"));
  alphay = Double.parseDouble(properties.getProperty("alphay"));
  betay = Double.parseDouble(properties.getProperty("betay"));
  qy = Double.parseDouble(properties.getProperty("qy"));
  add_tauy = Double.parseDouble(properties.getProperty("add_tauy"));
  tauUpperBoundy = Double.parseDouble(properties.getProperty("tauUpperBoundy"));
  luy = Double.parseDouble(properties.getProperty("luy"));
  minluy = Double.parseDouble(properties.getProperty("minluy"));
  guy = Double.parseDouble(properties.getProperty("guy"));
  minguy = Double.parseDouble(properties.getProperty("minguy"));

  tauw = Double.parseDouble(properties.getProperty("tauw"));
  etaw = Double.parseDouble(properties.getProperty("etaw"));
  aw = Double.parseDouble(properties.getProperty("aw"));
  bw = Double.parseDouble(properties.getProperty("bw"));
  alphaw = Double.parseDouble(properties.getProperty("alphaw"));
  betaw = Double.parseDouble(properties.getProperty("betaw"));
  qw = Double.parseDouble(properties.getProperty("qw"));
  add_tauw = Double.parseDouble(properties.getProperty("add_tauw"));
  tauUpperBoundw = Double.parseDouble(properties.getProperty("tauUpperBoundw"));
  luw = Double.parseDouble(properties.getProperty("luw"));
  minluw = Double.parseDouble(properties.getProperty("minluw"));
  guw = Double.parseDouble(properties.getProperty("guw"));
  minguw = Double.parseDouble(properties.getProperty("minguw"));

  nfetay = properties.getProperty("nofactoryetay");

  ant = Integer.parseInt(properties.getProperty("ant"));
  iteration = Integer.parseInt(properties.getProperty("iteration"));

  parameter = properties.getProperty("parameter");

  fl = Integer.parseInt(properties.getProperty("fl"));
  fu = Integer.parseInt(properties.getProperty("fu"));
  pl = Integer.parseInt(properties.getProperty("pl"));
  pu = Integer.parseInt(properties.getProperty("pu"));
  rl = Integer.parseInt(properties.getProperty("rl"));
  cl = Integer.parseInt(properties.getProperty("cl"));
  cu = Integer.parseInt(properties.getProperty("cu"));
  ru = Integer.parseInt(properties.getProperty("ru"));
  Cl = Integer.parseInt(properties.getProperty("Cl"));
  Cu = Integer.parseInt(properties.getProperty("Cu"));
  ul = Integer.parseInt(properties.getProperty("ul"));
  uu = Integer.parseInt(properties.getProperty("uu"));
  al = Integer.parseInt(properties.getProperty("al"));
  au = Integer.parseInt(properties.getProperty("au"));

  scenario_generate = properties.getProperty("scenario_generate");

  scenario = Integer.parseInt(properties.getProperty("scenario"));
  average = Integer.parseInt(properties.getProperty("average"));
  variance = Integer.parseInt(properties.getProperty("variance"));

  stage = Integer.parseInt(properties.getProperty("stage"));

  runtime = Double.parseDouble(properties.getProperty("runtime"));
  }
}

2 个答案:

答案 0 :(得分:3)

此堆栈跟踪说明在第91行,您使用的是Integer.parseInt,但是您正在将null传入其中。这就是为什么会发生这种错误的原因。您必须传入表示整数的String。您无法传递null

您需要检查null并避免传入,或确保第91行始终传入非null。

答案 1 :(得分:0)

正如Daniel Kaplan所说,您将null值传入Integer.parseInt,导致异常。 parseInt的Javadoc说这会导致NumberFormatException被抛出:

  

投掷

     

NumberFormatException - 如果字符串不包含可解析的整数

正如您在评论中所述,这发生在t = Integer.parseInt(properties.getProperty("t"));问题是您的properties变量被调用且无法找到属性。查看java.util.Properties方法public String getProperty(String key)的Javadoc,强调我的:

  

在此属性中搜索具有指定键的属性   名单。如果在此属性列表中找不到该键,则为默认值   然后检查属性列表及其递归的默认值。的的   如果找不到该属性,则返回null

您似乎希望避免将null传递给Integer.parseInt方法。您可以通过不同的方式执行此操作,具体取决于您希望如何为您的班级建模,但以下是您可以做的一些事情:

  1. null
  2. 之前进行Integer.parseInt检查
  3. 使用public String getProperty(String key, String defaultValue),如果找不到属性,则提供默认值。即,properties.getProperty("t", "15")