i have to create a graph for my project i found an example that was working and followed it first time with graph. i wrote my code got no errors but when i run the code this is what i am getting i don't understand it can some one help me please i am in crunch mode.

Exception in thread "main" java.lang.NullPointerException
    at Main.GetAllAdjcentNodes(Main.java:69)
    at Main.Dijkstras(Main.java:101)
    at Main.main(Main.java:184)

here is the code

import java.util.ArrayList;
import java.util.HashMap;
import java.util.ArrayDeque;

public class Main {

    public static ArrayList<node> Nodes = new ArrayList<node>();
    public static HashMap<String,Integer> NodesIndex = new HashMap<String,Integer>();
    public static ArrayList<edge> Edges = new ArrayList<edge>();
    public static int SizeNodes;
    public static int SizeEdges;
    public static String StartNode;

    public static class node {
        public String name;
        public String Pre;
        public int Dist;
        public boolean vis;
        public node(String NodeName) {
            name = NodeName;
            Dist = -1;
            vis = false;
            Nodes.add(this);
            NodesIndex.put(name, Nodes.size()-1);
        }
    }
    public static class edge {
        String N1;
        String N2;
        int Dist;
        public edge(String Node1, String Node2, int EdgeVal) {
            N1 = Node1;
            N2 = Node2;
            Dist = EdgeVal;
            Edges.add(this);
        }
    }
    public static void SetStartNode(String name) {
        Nodes.get(NodesIndex.get(name)).Dist=0;
        SizeNodes = Nodes.size();
        SizeEdges = Edges.size();
        StartNode = name;
    }
    public static int VisitClosestNode() {
        int index=0;
        int Dist=0;
        for(int i=0;i<SizeNodes;++i) {
            if(!Nodes.get(i).vis && Nodes.get(i).Dist >=0) {
                Dist = Nodes.get(i).Dist;
                index=i;
                break;
            }
        }
        for(int i=0;i<SizeNodes;++i) {
            if(Nodes.get(i).Dist < Dist && !Nodes.get(i).vis && Nodes.get(i).Dist >=0){
                Dist = Nodes.get(i).Dist;
                index=i;
            }
        }
        Nodes.get(index).vis = true;
        return index;
    }
    public static void GetAllAdjcentNodes(String N, ArrayList<String> AdjNodes) {
        //nodes can be conetcted both ways
        //can take out the else if if you want them to only conect one way
        for(int i=0;i<SizeEdges;++i) {
           if(Edges.get(i).N1.equals(N) && !Nodes.get(NodesIndex.get(Edges.get(i).N2)).vis) {
               AdjNodes.add(Edges.get(i).N2);
           } else if(Edges.get(i).N2.equals(N) && !Nodes.get(NodesIndex.get(Edges.get(i).N1)).vis){
               AdjNodes.add(Edges.get(i).N1);
           }
        }
    }
    public static boolean EdgeConnectsNodes(edge E, String N1, String N2) {
        //again this statment determins weather or not edges go both ways or not
        //take out the second half of the condtional if you only want them to
        //conect one way
        return (E.N1.equals(N1)&&E.N2.equals(N2)||E.N1.equals(N2)&&E.N2.equals(N1)) ? true:false;
    }
    public static int GetDistance(String N1, String N2) {
        for(int i=0;i<SizeEdges;++i) {
            if(EdgeConnectsNodes(Edges.get(i),N1,N2)) {
                return Edges.get(i).Dist;
            }
        }
        return -1; //they dont conect so we will say that distance is endless
    }
    public static int GetNumOfUnVisNodes() {
        int NOUVN=0;
        for(int i=0;i<SizeNodes;++i) {
            if(!Nodes.get(i).vis) {
                ++NOUVN;
            }
        }
        return NOUVN;
    }
    public static void Dijkstras() {
        while(GetNumOfUnVisNodes()>0) {
            node ClosetsNode = Nodes.get(VisitClosestNode());
            ArrayList<String> AdjNodes = new ArrayList<String>();
            GetAllAdjcentNodes(ClosetsNode.name,AdjNodes);
            int SizeAdj = AdjNodes.size();
            for(int i=0;i<SizeAdj;++i) {
                node AdjNode = Nodes.get(NodesIndex.get(AdjNodes.get(i)));
                int Distance = ClosetsNode.Dist + GetDistance(ClosetsNode.name,AdjNodes.get(i));
                if(AdjNode.Dist >= 0) {
                    if(Distance<AdjNode.Dist) {
                        AdjNode.Dist = Distance;
                        AdjNode.Pre = ClosetsNode.name;
                    }
                } else {
                    AdjNode.Dist = Distance;
                    AdjNode.Pre = ClosetsNode.name;
                }
            }
        }
    }
    public static void GetPathTo(String N, ArrayDeque<String> Path) {
        String CN = N;
        while(!CN.equals(StartNode)) {
            String Temp = CN;
            Path.addFirst(Temp);
            CN = Nodes.get(NodesIndex.get(CN)).Pre;
        }
        Path.addFirst(StartNode);
    }
    public static void main(String[] args) {

       new node("Alta Garcia");
       new node("Loango");
       new node("Castillo");
       new node("Waterfall");
       new node("Santa Barbara");
       new node("Acono");
       new node("Caurita");
       new node("USC");
       new node("Mountain View");
       new node("Avondale");
       new node("Whaf Trace");
       new node("Maracas Gardens");
       new node("Poolside");
       new node("Tunapuna");
       new node("Riverside");
       new node("Curepe");
       new node("Piarco");
       new node("St Joseph");
       new node("Port of Spain");


       new edge("Alta Garcia","Loango",3);
       new edge("Alta Garcia","Castillo",5);
       new edge("Loango","Castillo",10);
       new edge("Castillo","Waterfall",10);
       new edge("Castillo","Santa Barbara",8);
       new edge("Santa Barbara","Acono",7);
       new edge("Acono","Caurita",6);
       new edge("Acono","USC",4);
       new edge("USC","Avondale",3);
       new edge("USC","Mountain View",5);
       new edge("Mountain View","Avondale",4);
       new edge("Avondale","Maracas Gardens",8);
       new edge("Maracas Gardens","Poolside",10);
       new edge("Poolside","Wharf Trace",5);
       new edge("Wharf Trace","Mountain View",10);
       new edge("Poolside","Riverside",12);
       new edge("Poolside","St Joseph",18);
       new edge("St Joseph","Riverside",6);
       new edge("St Joseph","Port of Spain",30);
       new edge("St Joseph","Curepe",6);
       new edge("St Joseph","Piarco",30);
       new edge("Piarco","Port of Spain",60);
       new edge("Piarco","Curepe",25);
       new edge("Piarco","Tunapuna",20);
       new edge("Curepe","Riverside",10);
       new edge("Curepe","Tunapuna",10);
       new edge("Riverside","Caurita",25);
       new edge("Riverside","Tunapuna",17);
       new edge("Tunapuna","Caurita",30);
       new edge("Tunapuna","Piarco",20);



       SetStartNode("Santa Barbara");
       Dijkstras();

       ArrayDeque<String> Path = new ArrayDeque<String>();
       GetPathTo("Maracas Gardens",Path);
       int Size = Path.size();
       for(int i=0;i<Size;++i) {
           System.out.println(Path.getFirst());
           Path.poll();
       }
    }
}

Recommended Answers

All 8 Replies

Exception in thread "main" java.lang.NullPointerException
at Main.GetAllAdjcentNodes(Main.java:69)

Look at line 69 and find what variable has a null value. Then back track in the code to find out why that variable does not have a valid non-null value.

Whats "Whaf Trace"? :P

Regards,
Emil Olofsson

ok it is suppose to be wharf trace i missed that thanks

i need to find a way to be able to have multiple source and destination because it is a taxi sevice that have about 179 or less different routes because some might be going the same place can i be helped

Well, you could run it several times then?

do you mean like a for loop?

or you mean every time i run it change the source and destination

I suggest you make a for loop, first with start destination = "Santa Barbara" and end destination = "Maracas Gardens". Save that path, then change start destination to "Maracas Gardens" and set end destination to the next stop on your route. That would work, wouldn't it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.