java - 广度优先搜索 - 空指针异常
问题描述
我已经实现了一个学校项目的广度优先搜索,我将方向图的起始节点作为输入,我必须返回一个包含 BFS 顺序节点的链接列表
这是我的方法:
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;
}
我已经为几个图表测试了我的函数,并且在我自己的测试中没有得到任何错误。但是当我将我的代码上传到学校服务器并运行他们的测试时,我得到了以下错误:java.lang.NullPointerException in SearchTestng.testBFSCliqueNodes(SearchTestng.java:512)
我已经尝试重现这个问题好几个小时了,我的导师似乎都不知道是什么导致了异常。
你们中有人知道什么可能导致这里出现空指针异常吗?
解决方案
推荐阅读
- java - 错误:找不到符号方法 setAlpha(float)。setAlpha() 不适用于具有偏移量的数组
- regex - 正则表达式范围 1 25
- angular - 如何使用 angular8 将在线可编辑的 mat-table 数据传递到服务中?
- sparql - 如何使用 SPARQL 从 Wikidata 检索具有所有限定符的项目的所有属性?
- r - 为什么 pdftools 中的 pdf_text 只读取我的 pdf 列表中每个 pdf 元素的第一页?
- javascript - 如何在反应中从嵌套地图迭代中切换单个元素
- java - 导入、读取 csv 文件并将其显示到 javafx 的表视图中
- c# - 试图从十进制转换为二进制,它不工作 C#
- angular - 调用 platformBrowserDynamic().bootstrapModule(@NgModule) 返回 ASSERTION ERROR 传入的类型不是 NgModuleType,它没有 'ɵmod' 属性
- python - 在数组中解包的值太多(预期为 2)