-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]);

    }

I created a hashmap to store the edge as key and the weight of that edge as value. Now applied Dijkstras algorithm to solve my problem in which I need to get the weight value corresponding to an edge. So I make a new edge having vertices same as the required edge present in the hashmap but when I call edgeInfo.get() function to get the weight value of that edge it shows NullPointerException...can anyone help me..

My edge class is :-

  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);
}   

 }
Mike
  • 3,830
  • 2
  • 16
  • 23
Aditya kumar
  • 76
  • 1
  • 11

2 Answers2

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

edgeInfo cannot possibly contain e, since e has just been created and is never put into the hash map. So the call to get() returns a null Integer, which the code tries to unbox to assign to int weightofEdge, and therefore the NullPointerException.

FredK
  • 4,094
  • 1
  • 9
  • 11
  • so what should I do to get the value at that edge because initially I have inserted these edge and corresponding values in edgeInfo.... – Aditya kumar Aug 18 '16 at 18:52
  • You have NOT put that edge into the hashmap, because you just created it on the line above the call. You may previously have put an edge with the same x and y values, but that edge is a different item than the one you just created. If your edge class (NB: follow conventions, and start class names with an upper-case letter!) were to override hashcode() as @azurefrog suggested, then the two edge instances would be seen as equal as long as they generated the same hash. – FredK Aug 18 '16 at 19:00
  • And you shouldn't ever use the return value of a HashMap.get() call without first checking that the return value is not null. – FredK Aug 18 '16 at 19:02
0

you have to implement the hash function so it returns values according to x and y the way you are doing it you only get the

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(); 
            }

}

whyn0t
  • 301
  • 2
  • 14
  • So for an edge hashCode() will return a unique number ......but if you could tell me how it will effect my program then it will be really helpful...thankyou in advance. – Aditya kumar Aug 19 '16 at 14:38