0

When trying to access the Edge array in my SetAdjList() method, I get a Null pointer exception. Attempting to access the Edge array in any of my other methods in this class have no errors and return correct results. I am not changing the Edge array in any of my other classes. Any ideas are greatly appreciated.

public class ReadInput {
    Vertex[] vertex = new Vertex[25252];
    final String DELIMITER = ",";
    int indexVertex = 0;
    public Edge[] edge = new Edge[127807];
    int indexEdge = 0; 
      Map<Vertex,List <Edge> > edgeHash= new HashMap();

    public Graph readFromStream() throws NumberFormatException, IOException {
         Graph graph = new Graph();
         //25252 number of elements in Place.txt file   

        //Delimiter used in CSV file
            String line = "";
            //Create the file reader
            BufferedReader fileReader = new BufferedReader(new FileReader("Place.txt"));
            String IDString = null;
            String name = null;
            int ID = 0;
            //Read the file line by line
            while ((line = fileReader.readLine()) != null) 
            {
                //Get all tokens available in line
                String[] tokens = line.split(DELIMITER);

                     IDString = tokens[0];
                     name = tokens[1];
                     ID = Integer.parseInt(IDString);

                 vertex[indexVertex] = new Vertex(ID,name);

                 //graph.addVertex(ID,name);
                //System.out.println(indexVertex +":" + vertex[indexVertex].getID());
                indexVertex++;
            }
            fileReader.close();


        String line2 = "";
        BufferedReader fileReader2 = new BufferedReader(new FileReader("Road.txt"));
        String valueString = null;
        String vertex1IDName = null;
        String vertex2IDName = null;
        String extra = null;
        float value = 0;
        int vertex1ID = 0;
        int vertex2ID = 0;
        //Read the file line by line
        while ((line2 = fileReader2.readLine()) != null) 
        {
            //Get all tokens available in line
            String[] tokens2 = line2.split(DELIMITER);

                vertex1IDName = tokens2[0];
                vertex2IDName = tokens2[1];
                valueString = tokens2[2];
                if(tokens2.length - 1 == 3) {
                    extra = tokens2[tokens2.length - 1];
                }
                else {
                    extra = "";
                }
                vertex1ID = Integer.parseInt(vertex1IDName);
                vertex2ID = Integer.parseInt(vertex2IDName);
                value = Float.parseFloat(valueString);


                graph.addVertex(vertex1ID, " ");
                graph.addVertex(vertex2ID, " ");

                Vertex v1 = new Vertex(0, " ");
                Vertex v2 = new Vertex(0, " ");
                for(int i = 0; i<indexVertex; i++) {
                    if(vertex1ID == vertex[i].getID()) {
                         v1 = vertex[i];     
                    }
                    else if(vertex1ID != 0) {
                        v1.setID(vertex1ID);
                    }
                     if(vertex2ID == vertex[i].getID()) {
                        v2 = vertex[i];
                    }
                     else if(vertex2ID != 0) {
                        v2.setID(vertex2ID);
                    } 

                }

                if(v1.getID() != 0 && v2.getID() !=0) {
                    edge[indexEdge] = new Edge(value,v1, v2, extra);
                    System.out.println("["+ indexEdge +"] Edge v1: " + edge[indexEdge].getStartVertex().getID() + " v2: " + v2.getID());
                    v1.addNeighbour(edge[indexEdge]);
                    edgeHash.put(v1, v1.getAdjacenciesList());
                    indexEdge++;
                }   


        } 
        //This test prints correctly
        //System.out.println("Test:"  + edge[0].getStartVertex().getID());
        fileReader2.close();
        return graph;
    }
    public Vertex calcualteDestination() {

        Scanner scanUserInput = new Scanner(System.in);
        System.out.println("Enter the Destination Name:");
        String destinationName = scanUserInput.nextLine();
        scanUserInput.close();

         Vertex Destination = new Vertex(0,null);

       for(int i = 0; i<indexVertex; i++) {
           if(destinationName.equals(vertex[i].getName())){
               //Destination = vertex[i];
               Destination.setID(vertex[i].getID());
               Destination.setName(vertex[i].getName());
               //Destination.setAdjacenciesList(vertex[i].getAdjacenciesList());
           }
       }   
        for(int k = 0; k<indexEdge; k++) {
            if(Destination.getID() == edge[k].getTargetVertex().getID()){
                Destination.setID(edge[k].getTargetVertex().getID());
                Destination.setAdjacenciesList(edge[k].getTargetVertex().getAdjacenciesList());
                //System.out.println("ID set from edge");
            }
       } 
        return Destination;
    }

    public Vertex calculatesSource() {
        Scanner scanUserInput = new Scanner(System.in);  
        System.out.println("Enter the Source Name:");
        String sourceName = scanUserInput.nextLine();

        Vertex Source = new Vertex(0, null);

        for(int i = 0; i<indexVertex; i++) {
            if(sourceName.equals(vertex[i].getName())){
                //Source = vertex[i];
                Source.setID(vertex[i].getID());
                //System.out.println("ID: " + Source.getID());
                Source.setName(vertex[i].getName());
                //Source.setAdjacenciesList(vertex[i].getAdjacenciesList());
            }
        }   

            for(int k = 0; k<indexEdge; k++) {
                if(Source.getID() == edge[k].getStartVertex().getID()){
                    Source.setID(edge[k].getStartVertex().getID());
                    Source.setAdjacenciesList(edge[k].getStartVertex().getAdjacenciesList());
                    //System.out.println(" Source adj size: "+Source.getAdjacenciesList().size());
                }
            }
        return Source;
    }
    public Vertex SetAdjList(Vertex actualVertex) {
        System.out.println("setAdjList id:" + actualVertex.getID());
        //edge[0] out of scope for some reason, works for other funcs
        //NULL POINTER exception on below line
        System.out.println("Test:"  + edge[0].getStartVertex().getID());
        for(int j = 0; j<edge.length; j++) {
            System.out.println("j:" + j);
            if(actualVertex.getID() == edge[j].getStartVertex().getID()){ //.getID()
                actualVertex.setAdjacenciesList(edge[j].getStartVertex().getAdjacenciesList());
                System.out.println(" Source adj size: "+actualVertex.getAdjacenciesList().size());
            }
        }
        return actualVertex;
    }
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
da2423
  • 69
  • 1
  • 8
  • Your edge array should have been populated before calling SetAdjList? Are you sure that it did? – Subramanian Mariappan Feb 29 '20 at 16:22
  • Yes, my edge array is populated. I am doing a very similar functionality in my calculateSource() method and SetAdjList() method, and calculateSource() works fine – da2423 Feb 29 '20 at 16:29
  • Order of methods being called is important here. From the code above, if you call your SetAdjList after SetAdjList. It will work because you are populating edge over there. – Subramanian Mariappan Feb 29 '20 at 16:36
  • SetAdjList is called after ReadFromStream, and edge is populated. I was calling SetAdjList and ReadInputFromStream() with different instances of the ReadInput class could this be the issue? – da2423 Feb 29 '20 at 16:53
  • 1
    Yes. You should be calling them with the same instance. – Subramanian Mariappan Feb 29 '20 at 17:02

0 Answers0