2016-10-01 39 views
0

我正在为我的大学的一个项目,我在 Assignment2.ColumnGen $ SubProblem.createModel(ColumnGen总是得到相同的错误消息数值误差图implementantion

显示java.lang.NullPointerException。 Java的:283)

的问题是在这些线路

double M = 0; 
for (int i=0; i<all_customers.size(); i++) { 
    for (int j=0; j<all_customers.size(); j++) { 
     double val = all_customers.get(i).time_to_node(all_customers.get(j)) + all_customers.get(i).time_at_node(); 
     if (M<val) M=val; 
    } 

} 

当我删除这些行的一切禾rks完美,但显然我没有得到最好的结果,只要我的算法,因为我想念这个参数。

我知道什么是空指针异常,但我尝试了一切,但仍然想念一些东西。

我的所有其他声明为您在代码中看到的东西都是

public Map<Integer, Customer> all_customers = new HashMap<Integer, Customer>(); 

    public double a() { 
     return ready_time; 
    } 

    public double b() { 
     return due_date; 
    } 

    public Node(int external_id, double x, double y, double t) { 
     this.id = all_nodes.size(); 
     this.id_external = external_id; 
     this.xcoord = x; 
     this.ycoord = y; 
     this.t_at_node = t; 
     all_nodes.put(this.id, this); 
    } 

    public double time_to_node(Node node_to) { 
     return Math.round(Math.sqrt(Math.pow(this.xcoord - node_to.xcoord, 2) + Math.pow(this.ycoord - node_to.ycoord, 2))); 
    } 

    public double time_at_node() { 
     return t_at_node; 
    } 

我该怎么办了?

+0

你在哪里添加数据到变量all_customers?你只是实例化一个HashMap并分配给变量,但没有数据。所以当你尝试在循环中运行它时会产生异常。 – Dez

回答

0

我觉得你的例外来自all_customers.get(I),调试代码,并确保所有的u请求键在地图上,或者你可以添加一个条件来检查地图是否包含了你的关键

0

您的问题是您的Map.get()操作之一返回null。很明显,你的地图中缺少一个键。您没有向我们展示如何填充地图,因此问题不在于您向我们展示的代码中。

替换的意大利面条下面一行代码:

double val = all_customers.get(i).time_to_node(all_customers.get(j)) + all_customers.get(i).time_at_node(); 

与下面的代码块:

Customer ci = all_customers.get(i); 
assert ci != null : "Not found:" + i; 
Customer cj = all_customers.get(j); 
assert cj != null : "Not found:" + j; 
double val = ci.time_to_node(cj) + ci.time_at_node(); 

并运行程序传递-enableassertions参数的VM。 (简称-ea)。这会给你一个很好的提示,指出哪里出了问题。