在 Java 中从节点创建边
每当我尝试从节点创建简单边时,都会收到错误。 基本上,我创建了两个自己的类,称为 Node 和 Edge。
Node 类如下:
public class Node {
public String ident;
public int numLinks;
public Edge[] neighbours;
public Node (String ident) {
this.ident = ident;
}
public void setNeighbour (Node start, Node end, int cost, int portNum) {
}
}
我的 Edge 类如下:
public class Edge {
Node start;
Node end;
int cost;
int portNum;
public Edge (Node a, Node b, int cost, int portNum) {
this.start = a;
this.end = b;
this.cost = cost;
this.portNum = portNum;
}
}
在我的主类中,我创建两个节点,即开始节点和结束节点。我从文本文件中读取成本和端口号(这两个节点侦听的端口号)并将它们保存到名为“linkCostList”和“portNumList”的数组列表中。
现在,由于每个起始节点可以有多个边(我基本上是在创建一个图),因此我按以下方式调用 setNeighbour() 方法:
for (int i = 0; i < startNode.numLinks; i++) {
nextNode = new Node (String name of node I read from text file)
startNode.setNeighbour (startNode, nextNode, linkCostList.get(i), portNumList.get(i));
}
我的 setNeighbour 方法如下:
public void setNeighbour (Node start, Node end, int cost, int portNum) {
for (int i = 0; i < start.numLinks; i++) {
neighbours[i] = new Edge (start, end, cost, portNum);
}
}
每当我编译它时,我都会得到以下类型的错误:
Exception in thread "main" java.lang.NullPointerException
at Node.setNeighbour(Node.java: *line number*)
at Start.startlsr(Start.java: *line number*)
at graph.main(lsr.java: *line number*)
}
我知道这是一个空指针异常,因此,在该循环中的某个位置,我一定做错了什么。有人可以帮我解决一下吗?
I get an error whenever I try to create a simple edge from a node.
Basically, I've created two of my own classes called Node and Edge.
The Node class is as follows:
public class Node {
public String ident;
public int numLinks;
public Edge[] neighbours;
public Node (String ident) {
this.ident = ident;
}
public void setNeighbour (Node start, Node end, int cost, int portNum) {
}
}
And my Edge class is as follows:
public class Edge {
Node start;
Node end;
int cost;
int portNum;
public Edge (Node a, Node b, int cost, int portNum) {
this.start = a;
this.end = b;
this.cost = cost;
this.portNum = portNum;
}
}
In my main class, I create two nodes, namely, the start and the end node. The cost and the port number (port number that both these nodes listen to on), I read from a text file and am saving them into an array list named "linkCostList" and "portNumList".
Now, since each start node can have more than one edge (I'm basically creating a graph), I'm calling the setNeighbour() method in the following way:
for (int i = 0; i < startNode.numLinks; i++) {
nextNode = new Node (String name of node I read from text file)
startNode.setNeighbour (startNode, nextNode, linkCostList.get(i), portNumList.get(i));
}
My setNeighbour method is as follows:
public void setNeighbour (Node start, Node end, int cost, int portNum) {
for (int i = 0; i < start.numLinks; i++) {
neighbours[i] = new Edge (start, end, cost, portNum);
}
}
Whenever I compile it, I get an error of the following sort:
Exception in thread "main" java.lang.NullPointerException
at Node.setNeighbour(Node.java: *line number*)
at Start.startlsr(Start.java: *line number*)
at graph.main(lsr.java: *line number*)
}
I understand that this is a null pointer exception, so, somewhere in that loop, I must be doing something wrong. Could anyone please help me figure it out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已在
Node
类中初始化了neighbours
?看起来异常是由于访问空数组 (neighbours[i]
) 引起的。另外,看起来
neighbours
数组会动态增长/收缩?在这种情况下,不要使用数组,而是考虑使用 ArrayList,这样您就不必自己增加邻居
。Have you initialzed
neighbours
in yourNode
class? It looks like the exception is from accessing a null array (neighbours[i]
).Also It looks like the
neighbours
array will grow/shrink dynamically? In this case, instead of using array, consider using ArrayList so you don't have to growneighbours
yourself.除了@Alvin的回答(你没有初始化邻居,也没有允许扩展),试试这个:
也利用java的“foreach”:
考虑将“neighbours”重命名为“edges”,因为它们就是那个 - 的边缘节点。
最后,看来你的逻辑有问题。似乎太复杂了。您可能试图保留邻居的邻居的参考资料。考虑在需要时简单地查找内容。
Further to @Alvin's answer (you haven't initialized neighbours nor allowed for expansion), try this:
Also make use of java's "foreach":
Consider renaming "neighbours" to "edges", since they are just that - the edges of the node.
Finally, it seems you have a bug in your logic. It seems too complicated. You may be attempting to keep references of neighbours of neighbours. Consider simply looking stuff up when you need it.