0

I am having trouble finding why I am getting a null pointer exception. I have gone through the threads but just can't seem to locate exactly what my problem is and how to fix it.

public class Reading_CSV {
private final List<Vertex> verticies;
private final List<Edge> edges;
Reading_CSV() {
    this.edges=null;
    this.verticies=null;
}

public Reading_CSV(List<Vertex> vertecies, List<Edge> edges, String csvfile) throws FileNotFoundException, IOException {
    this.edges=edges;
    this.verticies=vertecies;
    CsvReader data = new CsvReader(csvfile);
    if(data.readHeaders())
    {
        for(int i=0;i<data.getColumnCount();i++)
        {
            data.get(i);
            Vertex v=new Vertex(Integer.toString(i),data.get(i));
            this.verticies.add(i,v);
        }
    }
    int j=0;
    while(data.readRecord())
    {
        Vertex Source=null;
        String sr=data.get(0);
        for(int i=0;i<this.verticies.size();i++)
        {
            if(sr == null ? vertecies.get(i).getName() == null : sr.equals(vertecies.get(i).getName()))
            {
                Source=vertecies.get(i);
                break;
            }
        }
        for(int i=0;i<data.getColumnCount();i++)
        {
            String d=data.get(i);
            int value=Integer.parseInt(d);
            if(value!=0)
            {
                String Dest=data.getHeader(i);
                Vertex Destination=null;
                for(int k=0;k<verticies.size();k++)
                    {
                    if(Dest==vertecies.get(i).getName())
                    {
                        Destination=vertecies.get(i);
                        break;
                    }
                    }
                this.edges.add(new Edge(Integer.toString(j), Source, Destination, value));
                j++;
            }
        }
    }
}

public List<Vertex> getVerticies() {
    return verticies;
}

public List<Edge> getEdges() {
    return edges;
}

}

public class Algo {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    /*
    Maintaining list of edges and vertices
    */
     List<Vertex> vertecies = null;
     List<Edge> edges = null;

     String csvfile="myfile.csv";
     Reading_CSV csv=new Reading_CSV(vertecies, edges,csvfile);
     vertecies=csv.getVerticies();
     edges=csv.getEdges();
     Graph myGraph=new Graph(vertecies, edges);
     Scanner scan=new Scanner(System.in);
     System.out.println("Enter the Source");
     String src,dest;
     src=scan.next();
     System.out.println("Enter the Destination");
     dest=scan.next();

     DijkstraAlgorithm algorithm=new DijkstraAlgorithm(myGraph);
     Vertex Source=null;
        for(int i=0;i<vertecies.size();i++)
        {
            if(src==vertecies.get(i).getName())
            {
                Source=vertecies.get(i);
                break;
            }
        }
     algorithm.execute(Source);
     Vertex Destination=null;
        for(int i=0;i<vertecies.size();i++)
        {
            if(dest==vertecies.get(i).getName())
            {
                Destination=vertecies.get(i);
                break;
            }
        }

        System.out.println(algorithm.getPath(Destination));
 }

}

Algo is my main class. I am getting exception thrown and I am sure this is an easy solution but I have been spending a while trying to figure this out. The exception thrown is:

Exception in thread "main" java.lang.NullPointerException
at ReadingCSV.Reading_CSV.<init>(Reading_CSV.java:45)
at Algo.main(Algo.java:34)

Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

  • You are passing `null` to your constructor, because `List edges = null;` sets it to `null`. – Elliott Frisch May 09 '16 at 22:10
  • List vertecies = new ArrayList(); // Since List is an interface, you cannot new it unless you get a working class, like ArrayList. – mhyst May 09 '16 at 22:17

1 Answers1

0

Well setting vertices to null in your main method sure isn't helping anything.

List<Vertex> vertecies = null;
List<Edge> edges = null;

Then in your Reading_CSV constructor you try adding elements to that null list

this.edges=edges;
this.verticies=vertecies;
CsvReader data = new CsvReader(csvfile);
if(data.readHeaders())
{
    for(int i=0;i<data.getColumnCount();i++)
    {
        data.get(i);
        Vertex v=new Vertex(Integer.toString(i),data.get(i));
        this.verticies.add(i,v);
    }
}

Try setting your list to new List instead of null.

CConard96
  • 874
  • 1
  • 7
  • 18