我在线程" main"中遇到了异常。显示java.lang.NullPointerException

时间:2016-05-09 22:08:00

标签: java multithreading import nullpointerexception

我无法找到为什么会出现空指针异常。我已经完成了线程,但似乎无法找到我的问题是什么以及如何解决它。

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是我的主要课程。我得到了异常抛出,我相信这是一个简单的解决方案,但我已经花了一段时间试图解决这个问题。抛出的异常是:

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

Java结果:1 建立成功(总时间:0秒)

1 个答案:

答案 0 :(得分:0)

在主方法中将顶点设置为null肯定不会有任何帮助。

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

然后在您的Reading_CSV构造函数中尝试向该空列表添加元素

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);
    }
}

尝试将列表设置为新列表而不是null。