Breadth First Search - Nullpointer Exception

时间:2018-06-04 17:06:55

标签: java nullpointerexception queue graph-theory breadth-first-search

I have implement a breadth first search for a school project where i get the start-node of a directional graph as an input and i have to return a Linked List containing the nodes in BFS order

This is my approach:

public List<Node> breadthFirstSearch(Node startNode){

    //node list that has to be returned  
    LinkedList<Node> nodeList = new LinkedList<Node>();

    resetState();

    // TODO

    //Queueu for the DfS Algorithm 
    Queue<Node> nodeQueue = new LinkedList<Node>();

    //Add start node to NodeList and Queue
    nodeQueue.add(startNode);
    nodeList.add(startNode);

    //While the Queue isn't empty
    while(!nodeQueue.isEmpty()) {
        Node v = nodeQueue.poll();
        //iterate over adjacent nodes of current node and add them to Queue and List 
        for (Node w : getAdjacentNodes(v)) {
            //don't add if already traversed node
            if (!nodeList.contains(w)) {
                nodeList.add(w);
                nodeQueue.add(w);
            }
        }
    }

    return nodeList;
}

I have tested my function for several graphs and I didn't get any errors at my own tests. But when I upload my code to the schools servers and they run their tests i get following error: java.lang.NullPointerException in SearchTestng.testBFSCliqueNodes(SearchTestng.java:512) I have been trying to reproduce the problem for hours now and none of my tutors seems to have an idea what could be causing the exception.

Does any of you have an idea what could possibly cause a null pointer exception here ?

0 个答案:

没有答案