2016-08-18 205 views
-1
public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Scanner sc = new Scanner(System.in); 
     int source= sc.nextInt(); 
     int dest = sc.nextInt(); 
     int noOfVertices= sc.nextInt(); 
     int noofEdges= sc.nextInt(); 
     int GreeLights[] = new int[noOfVertices+1]; 
     HashMap<edge, Integer> edgeInfo= new HashMap<>(); 
     Graph g= new Graph(); 
     for(int i=1;i<=noOfVertices;i++){ 
      GreeLights[i]=sc.nextInt(); 
     } 

     for(int i=1;i<=noofEdges;i++){ 
      int x= sc.nextInt(); 
      int y= sc.nextInt(); 
      int weight= sc.nextInt(); 
      edge e= new edge(x,y); 
      edgeInfo.put(e, weight); 
      g.adj.put(x, new LinkedList<Integer>()); 
      g.addNeighbour(x, y); 
     } 

     boolean visited[]= new boolean[noOfVertices+1]; 
     int distance[]= new int[noOfVertices+1]; 

     for(int i=1;i<=noOfVertices;i++){ 
      visited[i]=false; 
      distance[i]=1000; 
     } 

     distance[source]=0; 

     for(int i=1;i<=noOfVertices;i++){ 
      int min=1000; 
      int minIndex=-1; 
      for(int j=1;j<=noOfVertices;j++){ 
       if(distance[j]<min){ 
        min=distance[j]; 
        minIndex=j; 
       } 
      }    
      visited[minIndex]=true; 
      LinkedList<Integer> LL= g.getNeighbors(minIndex); 
      for(int x:LL){ 
       if(visited[x]!=true){ 
        edge e = new edge(minIndex,x); 
        int weightofEdge=edgeInfo.get(e); //Null pointer exception occuring here 
        int distancetoNeighbour =  
       distance[minIndex]+weightofEdge; 
        if(distance[x]>distancetoNeighbour){ 
         int greenTime=GreeLights[x]; 
         int wait=0; 
         for(int j=0;j<distancetoNeighbour;){ 
          j=j+greenTime; 
          wait=j; 
         } 
         wait-=distancetoNeighbour; 
         if(wait==0){ 
          distance[x]=distancetoNeighbour; 
         } 
         else{ 
          distance[x]=distancetoNeighbour+wait; 
         } 
        } 
       } 
      } 
     } 


     System.out.println(distance[dest]); 

    } 

我创建了一个散列表,将边缘存储为键,并将该边的权值存储为值。现在应用Dijkstras算法来解决我需要得到与边相对应的权值的问题。所以我创建了一个新的边,其顶点与hashmap中存在的所需边相同,但是当我调用edgeInfo.get()函数来获取该边的权值时,它会显示NullPointerException ...任何人都可以帮助我...空指针异常

我的边缘种类是: -

class edge{ 
    int x; 
int y; 

public edge(int x,int y){ 
    this.x=x; 
    this.y=y; 

} 

public boolean equals(edge e){ 
    return (this.x==e.x && this.y==e.y); 
} 

} 
+0

提的编程语言将是很好的返回值。 – JJJ

+0

对不起兄弟我的错 –

+0

什么是堆栈跟踪? –

回答

0
edge e = new edge(minIndex,x); 
int weightofEdge=edgeInfo.get(e); 

edgeInfo不可能包含E,因为E具有刚刚创建,并从未投入散列图。所以对get()的调用返回一个空整数,代码试图取消分配给int weightofEdge,并因此返回NullPointerException。

+0

,那么应该如何获取值该边缘,因为最初我已经在edgeInfo中插入了这些边缘和相应的值.... –

+0

您没有将该边缘放入hashmap中,因为您刚刚在上面的t行上创建了它他打电话。您可能以前已经将相同的x和y值放在一条边上,但该边与您刚创建的边不同。如果你的边界类(NB:遵循约定,并且以大写字母开始类名!)将覆盖hashcode()为@azurefrog建议的值,那么只要两个边界实例产生相同哈希值。 – FredK

+0

而且您不应该先使用HashMap.get()调用的返回值,而是先检查返回值是否为空。 – FredK

0

你必须实现哈希函数,因此根据x和y 的方式,你正在做它,你只能得到

class edge{ 
      int x; 
      int y; 

      public edge(int x,int y){ 
        this.x=x; 
        this.y=y; 

      } 

      public boolean equals(edge e){ 
        return (this.x==e.x && this.y==e.y); 
      } 

      // overWride hash function 
      @Override 
      public int hashCode() { 
        String s = new String(Integer.toString(x)+Integer.toString(y));// you can change this if you like 
      return s.hashCode(); 
      } 

}

+0

因此,对于边缘hashCode()将返回一个唯一的数字......但如果你能告诉我它将如何影响我的程序,那么这将是真正有用的...谢谢你提前。 –