简体   繁体   English

我的Java代码有什么问题?

[英]What is wrong with my java code?

I am new at java and I am trying to write a Linked-List Stack.. 我是java的新手,正在尝试编写一个链接列表堆栈。

public class Stack {

    private Node first;

    private class Node {
        int item;
        Node next;
    }

    public boolean IsEmpty()
    {
        return first==null;
    }

    public void push(int item)
    {
        Node oldfirst=first;
        first=new Node();
        first.item=item;
        first.next=oldfirst;
    }

    public int pop ()
    {
        int item=first.item;
        first=first.next;
        return item;
    }
}

import javax.swing.*;

public class main {

    public static void main(String[] args) {
        Stack ob=null;
        int num=0;
        while (true)
        {
            num=Integer.parseInt(JOptionPane.showInputDialog("Enter the number"));
            ob.push(num);
            if (num==0)
                break;
        }
        int k;
        k=ob.pop();
        JOptionPane.showMessageDialog(null, k);     
    }

now when I enter a number the compiler through an Execption java.lang.NullPointerException at main.main(main.java:18) 现在,当我输入数字时,通过main.main(main.java:18)上的Execption java.lang.NullPointerException编译器

Why this is happening and how to avoid it Please be patient and thanks in advance 为什么会发生这种情况以及如何避免这种情况请耐心等待并提前致谢

Your stack ob is null when you call push. 调用push时,堆栈ob为null。 You need to instantiate it. 您需要实例化它。 Instead of 代替

Stack ob=null;

you need to have 你需要

Stack ob = new Stack();

Your code: 您的代码:

Stack ob=null
...
ob.push(num);

ob is null , you never assign an object to it. obnull ,您永远不会为其分配对象。 Calling a method on null will always result in a NullPointerException . null上调用方法将始终导致NullPointerException

You can fix it like this: 您可以这样解决:

Stack ob = new Stack();

Most exceptions have a self-explanatory name. 大多数例外都有一个不言自明的名称。 This one, for example, is a NullPointerException - there is a null object that is being accessed and the exception is being called. 例如,这是一个NullPointerException-正在访问一个空对象,并且正在调用异常。

You are trying to define "ob" as a new Stack object, although you have simply defined it as null. 您试图将“ ob”定义为新的Stack对象,尽管您只是将其定义为null。 Between the definition and the method call (ob.push()), ob is not modified again and therefore it remains null, causing the exception. 在定义和方法调用(ob.push())之间,ob不会再次被修改,因此它保持为空,从而导致异常。

To fix this, you need to ensure that the object is not null when you call it. 要解决此问题,您需要确保在调用对象时该对象不为null。 There are two ways to fix the error. 有两种方法可以修复该错误。

Firstly, you could instantiate the object. 首先,您可以实例化该对象。 This would make the program work the way that you want it to. 这将使程序按您希望的方式工作。 To do this, simply define the object like so: Stack ob = new Stack(); 为此,只需像这样定义对象: Stack ob = new Stack(); rather than Stack ob = null; 而不是Stack ob = null; .

The second way to fix this would be to catch the error. 解决此问题的第二种方法是捕获错误。 This would make it so that the program continues if an error occurs. 这样可以使程序在发生错误时继续运行。 I recommend that you use this whenever there is a potential that an object could be null, so that the program doesn't just shut down. 我建议你使用这个每当有潜力的对象可以是零,从而使程序不只是关闭。 To do this, surround the line with a try-catch-clause: 为此,请使用try-catch-clause子句包围行:

try
{
    ob.push(num);
}
catch (NullPointerException e)
{
    System.out.println("Error at: ob.push(num);");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM