Java LinkedList迭代异常

Java LinkedList迭代异常,java,exception,linked-list,nullpointerexception,Java,Exception,Linked List,Nullpointerexception,我正在与这个错误作斗争。我觉得这真的很简单,但我不明白为什么会出错 当我遍历链表中的值时,我不断得到一个NullPointerException 代码段: private void updateBuyBook(LimitOrder im) { LimitOrder lm = null; Iterator itr = buyBook.entrySet().iterator(); boolean modify = false; while (itr.hasNe

我正在与这个错误作斗争。我觉得这真的很简单,但我不明白为什么会出错

当我遍历链表中的值时,我不断得到一个
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;
            }
        }
    }
}
请帮忙。我的作业有什么错误吗??? 请帮忙!!!
谢谢

我猜您正在将空值传递到im中。我需要查看更多的代码才能确定。

您从不检查
im
是否为空。我猜是的。

第一眼,im的实例化在哪里?您还可以尝试在IDE中进行调试,以查看发生了什么?

看起来im从未声明/分配过。所以

im.getOrderID()
可能是生成空指针异常的位置

--丹

编辑:

没有注意到im作为一个参数传入。所以剩下一些可能性(按可能性的顺序):

  • im为null(即用户调用的带有null参数的函数)
  • order.getOrderID()返回null
  • 顺序为空(即列表中有空)
  • 编辑2:

    你的线路

    if (modify = true)
    
    从根本上讲是错误的,并且将始终计算为true(单个相等表示赋值,==表示比较)

    当简单地检查标志布尔值是真是假时,最好使用:

    boolean flag = true;
    
    if(flag)
    {
        // True block
    }
    else
    {
       // False block
    }
    

    im为null或order.getOrderID()返回null

    如果您可以在该行之前添加一个调试点,并查看哪个变量为null,那就好了。看看代码,答案是1。订单为空2。order.getOrderID()为null或3。im为空

    稍微更改代码会使代码变长,但更容易找到错误。。。而不是做:

    if (order.getOrderID().equalsIgnoreCase(im.getOrderID())) { 
    
    将其更改为:

    String orderID = order.getOrderID();
    String imOrderID = im.getOrderID();
    
    if(orderID.equals(imOrderID()) {
    
    然后您将知道
    order
    im
    是否为空。如果两者都不为空,那么可以为空的是
    orderID
    imOrderID
    。现在只需找出其中哪一个是空的

    如果是order或im,则程序将在
    order.getOrderID()
    im.getOrderID()
    行上崩溃

    相反,如果
    orderID
    imOrderID
    为空,则它将在
    If(orderID.equals(imOrderID()){
    上崩溃。然后,您可以使用System.out.println(或更好的工具,如调试器)轻松找出错误

    如果两者都不应为空,则我建议添加如下内容:

    if(orderID == null) { throw new IllegalStateException("orderID cannot be null"); }
    if(imOrderID == null) { throw new IllegalStateException("imOrderID cannot be null"); }
    

    然后跟踪它是如何被设置为null的。

    意外为null的可能是订单、订单ID或im。任何这些都会在此行中导致NPE。您可以暂时将System.err.println(im)放在其中进行检查。当我尝试打印订单时。getOrderId()我得到了null值。有很多代码。这是函数之一。谢谢,这就是它抛出NPE的原因。您正在执行null.equalsIgnoreCase(),它会爆炸。
    if(orderID == null) { throw new IllegalStateException("orderID cannot be null"); }
    if(imOrderID == null) { throw new IllegalStateException("imOrderID cannot be null"); }