链表迭代异常
我正在努力解决这个错误。我觉得它非常简单,但无法理解为什么会出现错误。
当我迭代链接列表中的值时,我不断收到 NullPointerException
。
代码片段:
private void updateBuyBook(LimitOrder im) {
LimitOrder lm = null;
Iterator itr = buyBook.entrySet().iterator();
boolean modify = false;
while (itr.hasNext() && !modify) {
Map.Entry pairs = (Map.Entry) itr.next();
if ((((LinkedList<IMessage>) pairs.getValue()).size() > 0)) {
LinkedList<ILimitOrder> orders = (LinkedList<ILimitOrder>) pairs
.getValue();
ListIterator listIterator = orders.listIterator();
while (listIterator.hasNext() && !modify) {
LimitOrder order = (LimitOrder) listIterator.next();
if (order.getOrderID().equalsIgnoreCase(im.getOrderID())) { // error at this line
lm = order;
addToBuyMap(im);
modify = true;
break;
}
}
if (modify = true) {
orders.remove(lm);
break;
}
}
}
}
错误出现在这一行:
Exception in thread "main" java.lang.NullPointerException
order.getOrderID().equalsIgnoreCase(im.getOrderID()));
请帮忙。我的任务有什么错误吗??? 请帮忙!!! 谢谢
I am struggling with this error. I feel its really simple but cannot understand why am getting the error.
I keep getting a NullPointerException
when I iterate through values in my linked list.
Code Snippet:
private void updateBuyBook(LimitOrder im) {
LimitOrder lm = null;
Iterator itr = buyBook.entrySet().iterator();
boolean modify = false;
while (itr.hasNext() && !modify) {
Map.Entry pairs = (Map.Entry) itr.next();
if ((((LinkedList<IMessage>) pairs.getValue()).size() > 0)) {
LinkedList<ILimitOrder> orders = (LinkedList<ILimitOrder>) pairs
.getValue();
ListIterator listIterator = orders.listIterator();
while (listIterator.hasNext() && !modify) {
LimitOrder order = (LimitOrder) listIterator.next();
if (order.getOrderID().equalsIgnoreCase(im.getOrderID())) { // error at this line
lm = order;
addToBuyMap(im);
modify = true;
break;
}
}
if (modify = true) {
orders.remove(lm);
break;
}
}
}
}
Error is at this line:
Exception in thread "main" java.lang.NullPointerException
order.getOrderID().equalsIgnoreCase(im.getOrderID()));
Please help. Is my assignment wrong in any way???
Please help!!!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
稍微更改一下代码会使其更长,但更容易找到错误...而不是这样做:
将其更改为:
然后您就会知道是
order
还是im
为空。如果这两个都不为 null,则可能为 null 的是orderID
和imOrderID
。现在找出其中哪一个为空就很简单了。如果是 order 或 im,则程序将在
order.getOrderID()
或im.getOrderID()
行崩溃。相反,如果
orderID
或imOrderID
为 null,那么它将在if(orderID.equals(imOrderID()) {
上崩溃。然后,您可以使用 System.out.println (或更好的东西,如调试器)轻松找出问题所在。如果这些都不应该为空,那么我建议添加类似的内容,
然后跟踪它是如何设置为空的 。首先。
Changing your code a bit will make it longer, but much easier to find the error... instead of doing:
Change it to:
Then you will know if
order
orim
is null. If neither of those is null then the things that could be null areorderID
andimOrderID
. It is now a simple matter of finding out which one of those is null.If it is order or im then the program will crash on the
order.getOrderID()
orim.getOrderID()
lines.If, instead it is
orderID
orimOrderID
that is null, then it will crash onif(orderID.equals(imOrderID()) {
. You can then use System.out.println (or something better, like a debugger) do easily find out what is wrong.If neither of those should be null then I suggest adding something like:
and then track down how it got set to null to begin with.
我的猜测是,您将 null 传递到 im 中。我需要查看更多代码才能确定。
My guess would be that you're passing in the null, into im. I'll need to see more of the code to be sure.
您永远不会检查
im
是否为 null。我怀疑是的。You never check to see if
im
is null. I suspect it is.首先看一下,
im
是在哪里实例化的?你也可以尝试在你的IDE中调试一下,看看到底发生了什么?One first look, where is
im
instantiated? You can also try to debug in your IDE so as to see whats going on?im 为 null 或 order.getOrderID() 返回 null。
Either im is null or order.getOrderID() is returning null.
看起来 im 从未被声明/分配过。这
可能就是产生空指针异常的地方。
-- Dan
编辑:
错过了 im 作为参数传入的情况。因此,留下了一些可能性(按可能性的顺序):
Edit2:
您的line
根本上是错误的,并且总是评估为 true (单个 equal 用于赋值,== 用于比较。)
当简单检查标志布尔值是 true 或 false 时,最好使用:
Doesn't look like im is ever declared / assigned. So
is probably where the null pointer exception is generated.
-- Dan
Edit:
Missed that im is passed in as an argument. So that leaves a few possibilities (in order of likelihood):
Edit2:
Your line
Is fundamentally wrong and will always evaluate to true (single equal is for assignment, == is for comparison.)
When simply checking if a flag boolean is true or false, it is best to use:
如果您可以在该行之前添加一个调试点并查看哪个变量为空,那就太好了。查看代码,答案是 1. order 为 null 2. order.getOrderID() 为 null 或 3. im 为 null
It will be good if you could add a debug point before that line and see which variable is null. looking at the code the answer is 1. order is null 2. order.getOrderID() is null or 3. im is null