java 迭代器 - 空指针异常错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16603456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:26:32  来源:igfitidea点击:

Iterator - Null Pointer Exception Error

javaiteratornullpointerexception

提问by ron8

I am having a really frustrating issue:

我有一个非常令人沮丧的问题:

I am trying to run an iterator, but it keeps on coming up with java.lang.NullPointerException at the hasNext class.

我正在尝试运行一个迭代器,但它一直在 hasNext 类中出现 java.lang.NullPointerException。

I am not quite sure where it might be trying to use a null value. I am assuming it is something to do with current. I added a if statement to check if current is null. But then it returns and unexpected value.

我不太确定它可能在哪里尝试使用空值。我假设这与电流有关。我添加了一个 if 语句来检查 current 是否为空。但随后它返回和意外的价值。

Help appreciated.

帮助表示赞赏。

Code below:

代码如下:

private class Iterator implements Iterator
{
    private Link<T> current;

    public boolean hasNext () { 
        if(current.next == null)
            return false;
        return true;
    }

    public T next() throws OutOfBounds
    {
        if (this.hasNext())
        {
            T element = current.element;
            current = current.next;
            return element;
        }
        else 
            throw new OutOfBounds("No next element to call");
    }
}

private class Link<T> 
{
    private T       element;
    private int     priority;
    private Link<T> next;

    public Link(T t, int p, Link<T> n) 
    {
        this.element = t;
        this.priority = p;
        this.next = n;
    }
}

}

}

回答by Habib

You are probably not initializing current, so your check in the method hasNextshould compare for nullagainst currnetbefore checking against current.next

你可能不进行初始化current,所以你的方法检查hasNext应比较null反对currnet对前检查current.next

Modify your check

修改您的支票

if(current.next == null)

to:

到:

if(current == null || current.next == null)

Or modify your method as:

或将您的方法修改为:

public boolean hasNext () { 
   return (current != null && current.next != null);
}

回答by Juned Ahsan

Try to update your hasNext as below to find the issue:

尝试更新您的 hasNext 如下以查找问题:

public boolean hasNext () { 
        if(current == null) {
           System.out.println("current is null");
           return false;
        } else if(current.next == null)
            return false;
        }
        return true;
    }

回答by Juned Ahsan

You may use iterator.next() two times inside your while block. Make new object with iterator.next() then use it.

您可以在 while 块中使用 iterator.next() 两次。使用 iterator.next() 创建新对象然后使用它。

This is the correct way to use it

这是正确的使用方法

ArrayList<String> demo = new ArrayList<>();

demo.add("A");
demo.add("B");
demo.add("C");
demo.add("D");

System.out.println(demo);

//Get iterator
Iterator<String> iterator = demo.iterator();

//Iterate over all elements
while(iterator.hasNext()){
/* if you want to use the elemet two times then put in a varialbe and use it.*/
    //Get current element
    String value = iterator.next();
     System.out.println("fist time using"+ value)
    System.out.println( "second time using " + value );

}