链表迭代异常

发布于 2024-10-21 07:16:18 字数 1366 浏览 4 评论 0原文

我正在努力解决这个错误。我觉得它非常简单,但无法理解为什么会出现错误。

当我迭代链接列表中的值时,我不断收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

过期情话 2024-10-28 07:16:18

稍微更改一下代码会使其更长,但更容易找到错误...而不是这样做:

if (order.getOrderID().equalsIgnoreCase(im.getOrderID())) { 

将其更改为:

String orderID = order.getOrderID();
String imOrderID = im.getOrderID();

if(orderID.equals(imOrderID()) {

然后您就会知道是 order 还是 im为空。如果这两个都不为 null,则可能为 null 的是 orderIDimOrderID。现在找出其中哪一个为空就很简单了。

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

相反,如果 orderIDimOrderID 为 null,那么它将在 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"); }

然后跟踪它是如何设置为空的 。首先。

Changing your code a bit will make it longer, but much easier to find the error... instead of doing:

if (order.getOrderID().equalsIgnoreCase(im.getOrderID())) { 

Change it to:

String orderID = order.getOrderID();
String imOrderID = im.getOrderID();

if(orderID.equals(imOrderID()) {

Then you will know if order or im is null. If neither of those is null then the things that could be null are orderID and imOrderID. 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() or im.getOrderID() lines.

If, instead it is orderID or imOrderID that is null, then it will crash on if(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:

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

and then track down how it got set to null to begin with.

我的黑色迷你裙 2024-10-28 07:16:18

我的猜测是,您将 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.

罪#恶を代价 2024-10-28 07:16:18

您永远不会检查 im 是否为 null。我怀疑是的。

You never check to see if im is null. I suspect it is.

回忆那么伤 2024-10-28 07:16:18

首先看一下,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?

诗酒趁年少 2024-10-28 07:16:18

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

Either im is null or order.getOrderID() is returning null.

尴尬癌患者 2024-10-28 07:16:18

看起来 im 从未被声明/分配过。这

im.getOrderID()

可能就是产生空指针异常的地方。

-- Dan

编辑:

错过了 im 作为参数传入的情况。因此,留下了一些可能性(按可能性的顺序):

  1. im 为空(即用户调用带有空参数的函数)
  2. order.getOrderID() 返回空
  3. order 为空(即列表中有空值)

Edit2:

您的line

if (modify = true)

根本上是错误的,并且总是评估为 true (单个 equal 用于赋值,== 用于比较。)

当简单检查标志布尔值是 true 或 false 时,最好使用:

boolean flag = true;

if(flag)
{
    // True block
}
else
{
   // False block
}

Doesn't look like im is ever declared / assigned. So

im.getOrderID()

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):

  1. im is null (ie. user called function with null parameter)
  2. order.getOrderID() is returning null
  3. order is null (ie. the list has nulls in it)

Edit2:

Your line

if (modify = true)

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:

boolean flag = true;

if(flag)
{
    // True block
}
else
{
   // False block
}
青瓷清茶倾城歌 2024-10-28 07:16:18

如果您可以在该行之前添加一个调试点并查看哪个变量为空,那就太好了。查看代码,答案是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文