graph algorithm - Error in java: Exception in thread "main" -


i trying apply dijkstra algorithm in java. input text file contains 3 columns first 1 start node , third end node , second 1 relation name not need right use first , last column. text file is:

12	ecrel	15  15	ecrel	18  11	ecrel	12  12	ecrel	14  11	ecrel	14  11	ecrel	18  14	maplink	17  12	maplink	17  14	maplink	10  18	maplink	10  14	maplink	16  15	maplink	19  18	maplink	19  12	maplink	19

can 1 please take code , me find problem in code cause error. code bellow:

package shortestpath; import java.util.scanner; import java.io.*; import java.util.*;  public class dijkstra {       public static int count;     //public static graph.edge[] graph = new graph.edge[count] ;      public static void countlines(string file) throws ioexception     {     linenumberreader  lnr = new linenumberreader(new filereader(new file(file)));     lnr.skip(long.max_value);     dijkstra.count=lnr.getlinenumber() + 1; //add 1 because line index starts @ 0     // finally, linenumberreader object should closed prevent resource leak     lnr.close();     //return dijkstra.count;     }      public static graph.edge[] readtextfile(string filename) {      string line = null;     graph.edge[] gr=new graph.edge[dijkstra.count];     try {         filereader filereader = new filereader("hsa00072.txt");         // wrap filereader in bufferedreader.         bufferedreader bufferedreader = new bufferedreader(filereader);         // bufferedreader br = new bufferedreader(new inputstreamreader(new         // fileinputstream(file)));         int i=0;         while ((line = bufferedreader.readline()) != null) {             string[] tokens = line.split("\\t+");             string s = tokens[0];             string e = tokens[2];             gr[i] =new graph.edge(s, e, 1);             i=i+1;         }          // close files.         bufferedreader.close();     } catch (filenotfoundexception ex) {         system.out.println("unable open file '" + filename + "'");     } catch (ioexception ex) {         system.out.println("error reading file '" + filename + "'");     }     //return dijkstra.graph;     return gr;     }          private static final string start = "10";        private static final string end = "12";         public static void main(string[] args) throws ioexception {           countlines("hsa00072.txt");            graph.edge[] graph=readtextfile("hsa00072.txt");           graph g = new graph(graph);           g.dijkstra(start);           g.printpath(end);             //g.printallpaths();        }     }      class graph {        private final map<string, vertex> graph; // mapping of vertex names vertex objects, built set of edges         /** 1 edge of graph (only used graph constructor) */        public static class edge {           public final string v1, v2;           public final int dist;           public edge(string v1, string v2, int dist) {              this.v1 = v1;              this.v2 = v2;              this.dist = dist;           }        }         /** 1 vertex of graph, complete mappings neighbouring vertices */        public static class vertex implements comparable<vertex> {           public final string name;           public int dist = integer.max_value; // max_value assumed infinity           public vertex previous = null;           public final map<vertex, integer> neighbours = new hashmap<>();            public vertex(string name) {              this.name = name;           }            private void printpath() {              if (this == this.previous) {                 system.out.printf("%s", this.name);              } else if (this.previous == null) {                 system.out.printf("%s(unreached)", this.name);              } else {                 this.previous.printpath();                 system.out.printf(" -> %s(%d)", this.name, this.dist);              }           }            public int compareto(vertex other) {              return integer.compare(dist, other.dist);           }        }         /** builds graph set of edges */        public graph(edge[] edges) {           graph = new hashmap<>(edges.length);            //one pass find vertices           (edge e : edges) {              if (!graph.containskey(e.v1)) graph.put(e.v1, new vertex(e.v1));              if (!graph.containskey(e.v2)) graph.put(e.v2, new vertex(e.v2));           }            //another pass set neighbouring vertices           (edge e : edges) {              graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);              //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // undirected graph           }        }         /** runs dijkstra using specified source vertex */         public void dijkstra(string startname) {           if (!graph.containskey(startname)) {              system.err.printf("graph doesn't contain start vertex \"%s\"\n", startname);              return;           }           final vertex source = graph.get(startname);           navigableset<vertex> q = new treeset<>();            // set-up vertices           (vertex v : graph.values()) {              v.previous = v == source ? source : null;              v.dist = v == source ? 0 : integer.max_value;              q.add(v);           }            dijkstra(q);        }         /** implementation of dijkstra's algorithm using binary heap. */        private void dijkstra(final navigableset<vertex> q) {                 vertex u, v;           while (!q.isempty()) {               u = q.pollfirst(); // vertex shortest distance (first iteration return source)              if (u.dist == integer.max_value) break; // can ignore u (and other remaining vertices) since unreachable               //look @ distances each neighbour              (map.entry<vertex, integer> : u.neighbours.entryset()) {                 v = a.getkey(); //the neighbour in iteration                  final int alternatedist = u.dist + a.getvalue();                 if (alternatedist < v.dist) { // shorter path neighbour found                    q.remove(v);                    v.dist = alternatedist;                    v.previous = u;                    q.add(v);                 }               }           }        }         /** prints path source specified vertex */        public void printpath(string endname) {           if (!graph.containskey(endname)) {              system.err.printf("graph doesn't contain end vertex \"%s\"\n", endname);              return;           }            graph.get(endname).printpath();           system.out.println();        }        /** prints path source every vertex (output order not guaranteed) */        public void printallpaths() {           (vertex v : graph.values()) {              v.printpath();              system.out.println();           }        }      }       

the error message :

exception in thread "main" java.lang.nullpointerexception  	at shortestpath.graph.<init>(dijkstra.java:125)  	at shortestpath.dijkstra.main(dijkstra.java:69)

line 69 is:

graph g = new graph(graph); 

line 125 is

if (!graph.containskey(e.v1)) graph.put(e.v1, new vertex(e.v1)); 

thanks comments special thank david wallace comment helped me know problem caused error. problem in line

graph.edge[] gr=new graph.edge[dijkstra.count];

i had set size count-1 instead of count.

thank you.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -