为什么这会给我一个空指针异常?

huangapple go评论73阅读模式
英文:

Why is this giving me NullPointerException?

问题

我正在创建一个逐字回文"yes I can, can I yes?"),如下所示

public class Palindrome {

    LinkedQueue<String> queue;
    LinkStack<String> stack;

    public Palindrome() {
        LinkedQueue<String> queue = new LinkedQueue<>();
        LinkStack<String> stack = new LinkStack<>();
    }

    public boolean isPalindrome(String sentence) {

        String[] sentenceSplit = sentence.split(" ");

        for(String word : sentenceSplit) {
            queue.enqueue(word.toLowerCase());
            stack.push(word.toLowerCase());
        }

        while (stack.top() == queue.front()){
                stack.pop();
                queue.dequeue();
        }

        if(stack.size() == 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Palindrome test = new Palindrome();
        test.isPalindrome("can you you can");
    }
}

在这里,代码中的问题是在for each循环中,在将每个字符串入队和压栈时出现了nullpointerexception异常。尽管在字符串数组中测试过字符串确实存在,但为什么会出现此错误呢?谢谢。

英文:

I am creating a word-by-word palindrome ("yes I can, can I yes?") as follows:
'''
public class Palindrome {

LinkedQueue&lt;String&gt; queue;
LinkStack&lt;String&gt; stack;

public Palindrome() {
    LinkedQueue&lt;String&gt; queue = new LinkedQueue&lt;&gt;();
    LinkStack&lt;String&gt; stack = new LinkStack&lt;&gt;();
}

public boolean isPalindrome(String sentence) {

    String[] sentenceSplit = sentence.split(&quot; &quot;);

    for(String word : sentenceSplit) {
        queue.enqueue(word.toLowerCase());
        stack.push(word.toLowerCase());
    }

    while (stack.top() == queue.front()){
            stack.pop();
            queue.dequeue();
    }

    if(stack.size() == 0) {
        return true;
    }
    return false;
}

public static void main(String[] args) {
    Palindrome test = new Palindrome();
    test.isPalindrome(&quot;can you you can&quot;);
}

}
'''
In here, it says I receive a nullpointerexception during my for each loop where I enqueue and push each string in the string array, I have tested the strings in the string array and they do exist, so why am I receiving this error? Thanks.

答案1

得分: 1

Sure, here's the translated code:

queue = new LinkedQueue<String>();
stack = new LinkStack<String>();
英文:

Change

LinkedQueue&lt;String&gt; queue = new LinkedQueue&lt;&gt;();
LinkStack&lt;String&gt; stack = new LinkStack&lt;&gt;();

to

queue = new LinkedQueue&lt;String&gt;();
stack = new LinkStack&lt;String&gt;();

答案2

得分: 0

你的构造函数在声明变量,但你使用了未初始化的类属性。
将构造函数的内容移动到你的类属性中。

另外,在比较字符串时,你可能希望在 "while (stack.top() == queue.front())" 中使用 .equals 而不是 ==。

英文:

You constructor is declaring variables but you use your class attributes that are not initialized.
Move the content of your constructor into your class' attributes.

Also, you might want to use .equals instead of == when comparing Strings in
"while (stack.top() == queue.front())".

huangapple
  • 本文由 发表于 2020年4月7日 01:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61065060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定