Java 为什么我会得到NullPointerException?(基本GPS逻辑)

Java 为什么我会得到NullPointerException?(基本GPS逻辑),java,nullpointerexception,gps,Java,Nullpointerexception,Gps,大家好! 我的任务是开发一个基本的GPS逻辑。这些是我到目前为止创建的主要类和附加类之一。我遇到的问题是,经过一天的调试,我无法找到原因,出现了NullPointerException。出现例外情况: 在主类的第29行调用我的方法 在49 current_path.addv处的子类中; 我的向导告诉我,它应该正常工作,我需要找到其中的逻辑错误,但我直到现在才这样做 我希望这些信息足够了,如果没有,请询问您需要什么来理解上面的代码。您从未初始化当前路径,因此在调用addv时它仍然为空。这就是抛出N

大家好! 我的任务是开发一个基本的GPS逻辑。这些是我到目前为止创建的主要类和附加类之一。我遇到的问题是,经过一天的调试,我无法找到原因,出现了NullPointerException。出现例外情况:

在主类的第29行调用我的方法 在49 current_path.addv处的子类中; 我的向导告诉我,它应该正常工作,我需要找到其中的逻辑错误,但我直到现在才这样做

我希望这些信息足够了,如果没有,请询问您需要什么来理解上面的代码。

您从未初始化当前路径,因此在调用addv时它仍然为空。这就是抛出NullPointerException的原因。您必须创建一个新的ArrayList并将其分配给当前路径,然后才能使用它。您可以在构造函数中创建它

import java.math.BigDecimal;
import java.util.*;


public class DirectionGraphe {

    public ArrayList<GraphVector> shortest;
    public ArrayList<GraphVector> current_path;
    private ArrayList <GraphVector> fastest;
    private Integer weight_sum;
    private Integer current_gain;

    public DirectionGraphe(){
    }

    private ArrayList <GraphVector> graph = new ArrayList<GraphVector>();

    public ArrayList <GraphVector> getRoutes(GraphVector toncho){
        ArrayList<GraphVector> list = new ArrayList <GraphVector> ();

        for(GraphVector eachVector : graph){
            if(toncho.isRoute(eachVector)){
                list.add(eachVector);
            }
        }

        return list;
    }

    public void addVector(GraphVector vector) throws Exception{
        if(isConnected(vector) || graph.isEmpty()){
            graph.add(vector);  
        }
        else throw(new Exception (" Vector is not connected ! "));
    }

    public boolean isConnected(GraphVector vector){     ///////////////////////////////////////////////////////////////////////////////
        for(GraphVector v : graph){
            if(vector.isConnected(v)){
                return true;
            }
        }
        return false;
    }

    public ArrayList<GraphVector> findShortestRoute(DecCoord start, DecCoord end){
        for(GraphVector v : graph){
            if(v.beginning.equals(start)){
                current_path.add(v);
                if(v.end.equals(end)){          //If we are to be in that scope it means this we have found a vector from A to B and within that scope here we are checking
                    if( shortest == null || shortest.isEmpty() || current_path.size() < shortest.size() ){ // to see if this VECTOR is shorter compared to the last vector
                        shortest = current_path;        
                        ArrayList <GraphVector> current_path2 = new ArrayList <GraphVector> ();///////////////////
                        current_path2.addAll(shortest);       /////////////                             // hack 
                        current_path.clear();     
                        shortest = current_path2; //////////
                    }
                } else {
                    for(GraphVector p : this.getRoutes(v)){         // 


                        System.out.println("shortest else " + shortest);
                        System.out.println("current path else " + current_path);


                        findShortestRoute(p.beginning,end);
                    }
                }
                current_path.remove(current_path.lastIndexOf(current_path));
            }
        }
        return shortest;
    }

    public ArrayList<GraphVector> findFastestRoute (DecCoord start, DecCoord end){
        for(GraphVector d : graph){
            if(d.beginning.equals(start) && current_path != null){
                current_path.add(d);
                if(d.end.equals(end)){
                    if(fastest == null || fastest.isEmpty() || gain(current_path) < gain(fastest)){
                        current_path = fastest;
                    }
                }
                else {
                    for(GraphVector z : this.getRoutes(d)){
                        findFastestRoute(z.beginning,end);
                    }
                }
                current_path.remove(current_path.lastIndexOf(current_path));
            }
        }


        return current_path;    
    }

    public Integer gain (ArrayList<GraphVector> current_path){ 
        for(GraphVector e : current_path){
            weight_sum =+ e.speed;
        }
        Integer size = current_path.size();
        current_gain = size/weight_sum; 
        System.out.println(current_gain);
        return current_gain;    
    }
}



import java.math.*;
import java.util.ArrayList;
public class GPS_Fail {




    public static void main(String[] args) throws Exception {

        int [][] coordinates = { 
                {45,60,90,43,80},
                {90,43,87,123,50},
                {87,123,111,133,120},
                {90,43,40,56,60},
                {40,56,111,133,90},
                {90,43,145,543,70},
                {145,543,344,54,20},
                {344,54,432,54,50},
                {432,54,111,133,70}};
        DirectionGraphe graphe = new DirectionGraphe();
        for(int i = 0; i< coordinates.length; i++ ){
            graphe.addVector(new GraphVector(new BigDecimal(coordinates[i][0]), new BigDecimal(coordinates[i][1]),new BigDecimal(coordinates[i][2]), new BigDecimal(coordinates[i][3]),(coordinates[i][4])));



        }
        DecCoord Lili = new DecCoord(new BigDecimal(45),new BigDecimal(60));
        DecCoord pich = new DecCoord(new BigDecimal(111),new BigDecimal(133)) ;
        graphe.findShortestRoute(Lili, pich);
        //graphe.findFastestRoute(Lili, pich);
    }
}

另外,我会检查其他实例变量,确保它们正确初始化。比如重量和重量。看起来您在给它赋值之前先给它添加了一个数字

您可以发布完整的堆栈跟踪吗?您的实例变量都为空。请在出现问题的行上添加一个清晰的标记,我不想计算行数。
public DirectionGraphe(){
    current_path = new ArrayList<GraphVector>();
}