java - Why do I get NullPointerException ? (Basic GPS logic) -


import java.math.bigdecimal; import java.util.*;   public class directiongraphe {      public arraylist<graphvector> shortest;     public arraylist<graphvector> current_path;     private arraylist <graphvector> fastest;     private integer weight_sum;     private integer current_gain;      public directiongraphe(){     }      private arraylist <graphvector> graph = new arraylist<graphvector>();      public arraylist <graphvector> getroutes(graphvector toncho){         arraylist<graphvector> list = new arraylist <graphvector> ();          for(graphvector eachvector : graph){             if(toncho.isroute(eachvector)){                 list.add(eachvector);             }         }          return list;     }      public void addvector(graphvector vector) throws exception{         if(isconnected(vector) || graph.isempty()){             graph.add(vector);           }         else throw(new exception (" vector not connected ! "));     }      public boolean isconnected(graphvector vector){     ///////////////////////////////////////////////////////////////////////////////         for(graphvector v : graph){             if(vector.isconnected(v)){                 return true;             }         }         return false;     }      public arraylist<graphvector> findshortestroute(deccoord start, deccoord end){         for(graphvector v : graph){             if(v.beginning.equals(start)){                 current_path.add(v);                 if(v.end.equals(end)){          //if in scope means have found vector b , within scope here checking                     if( shortest == null || shortest.isempty() || current_path.size() < shortest.size() ){ // see if vector shorter compared last vector                         shortest = current_path;                                 arraylist <graphvector> current_path2 = new arraylist <graphvector> ();///////////////////                         current_path2.addall(shortest);       /////////////                             // hack                          current_path.clear();                              shortest = current_path2; //////////                     }                 } else {                     for(graphvector p : this.getroutes(v)){         //                            system.out.println("shortest else " + shortest);                         system.out.println("current path else " + current_path);                           findshortestroute(p.beginning,end);                     }                 }                 current_path.remove(current_path.lastindexof(current_path));             }         }         return shortest;     }      public arraylist<graphvector> findfastestroute (deccoord start, deccoord end){         for(graphvector d : graph){             if(d.beginning.equals(start) && current_path != null){                 current_path.add(d);                 if(d.end.equals(end)){                     if(fastest == null || fastest.isempty() || gain(current_path) < gain(fastest)){                         current_path = fastest;                     }                 }                 else {                     for(graphvector z : this.getroutes(d)){                         findfastestroute(z.beginning,end);                     }                 }                 current_path.remove(current_path.lastindexof(current_path));             }         }           return current_path;         }      public integer gain (arraylist<graphvector> current_path){          for(graphvector e : current_path){             weight_sum =+ e.speed;         }         integer size = current_path.size();         current_gain = size/weight_sum;          system.out.println(current_gain);         return current_gain;         } }    import java.math.*; import java.util.arraylist; public class gps_fail {         public static void main(string[] args) throws exception {          int [][] coordinates = {                  {45,60,90,43,80},                 {90,43,87,123,50},                 {87,123,111,133,120},                 {90,43,40,56,60},                 {40,56,111,133,90},                 {90,43,145,543,70},                 {145,543,344,54,20},                 {344,54,432,54,50},                 {432,54,111,133,70}};         directiongraphe graphe = new directiongraphe();         for(int = 0; i< coordinates.length; i++ ){             graphe.addvector(new graphvector(new bigdecimal(coordinates[i][0]), new bigdecimal(coordinates[i][1]),new bigdecimal(coordinates[i][2]), new bigdecimal(coordinates[i][3]),(coordinates[i][4])));            }         deccoord lili = new deccoord(new bigdecimal(45),new bigdecimal(60));         deccoord pich = new deccoord(new bigdecimal(111),new bigdecimal(133)) ;         graphe.findshortestroute(lili, pich);         //graphe.findfastestroute(lili, pich);     } } 

hello ! have been assigned task develop basic gps logic. these main , 1 of additional classes have created far. problem encounter nullpointerexception after day of debugging couldn't find out why. exception appearing :

  • in main class @ line 29(calling method)
  • in subclass @ 49 (current_path.add(v);)

my "guide" said me should work , need find logical mistake within, couldn't until now.

i hope info enough , if not please ask whatever need in order understand code above.

you never initialize current_path, still null when call add(v) on it. throwing nullpointerexception. have create new arraylist , assign current_path before can use it. can create in constructor.

public directiongraphe(){     current_path = new arraylist<graphvector>(); } 

also, check other instance variables , make sure initialized correctly. (such weight_sum. looks add number before giving value)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -