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);
}
}
提的编程语言将是很好的返回值。 – JJJ
对不起兄弟我的错 –
什么是堆栈跟踪? –