2016-10-01 26 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)。這會給你一個很好的提示,指出哪裏出了問題。