netbeans 中的 java.lang.NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15553860/
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 20:02:17  来源:igfitidea点击:

java.lang.NullPointerException in netbeans

javanullpointerexception

提问by user2196191

i'm writing a bit of code and i keep getting this error, it's to do with where i call network.addstation

我正在编写一些代码,但不断收到此错误,这与我调用 network.addstation 的位置有关

here's the code, there are other methods in the network method but i thought i'd just show one, but it appears when you call any method. any help would be much appreaciated. thanks.

这是代码,网络方法中还有其他方法,但我以为我只会显示一个,但是当您调用任何方法时它都会出现。任何帮助都会非常感谢。谢谢。

public class MyNetworkReader implements NetworkReader {

    Network network = null;

    @Override
    public Network read(InputStream stream) {
        Scanner scan = null;
        scan = new Scanner(stream);
        while (scan.hasNext()) {
            String fromStation = scan.next();
            if (!fromStation.equalsIgnoreCase("connection:")) ;
            network.addStation(fromStation)
            System.out.println(fromStation);
        }
    }

    return network;
}

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class MyNetwork implements Network {


    HashMap<String, ArrayList> stations = new HashMap<String, ArrayList>();


    public void addStation(String station) {
        stations.put(station, null);
    }
}

回答by TonyArra

Are you constructing a MyNetwork instance anywhere?

您是否在任何地方构建 MyNetwork 实例?

Seems like you want

好像你想要

 Network network = null;

To be

成为

MyNetwork network = new MyNetwork();

Because you're trying to call a method on an uninitiated object. But your MyNetwork class needs a constructor.

因为您试图在未初始化的对象上调用方法。但是您的 MyNetwork 类需要一个构造函数。

回答by DragonLord

Note you can also get this error immediately upon running C++ code, with the default NetBeans setup, if your Project's Properties->Run->Console Type is set to Internal Terminal and not External Terminal; at least when using FLTK graphics on Ubuntu. In this case it's a NetBeans internal handshaking problem and not a source code problem.

请注意,如果您的项目的 Properties->Run->Console Type 设置为 Internal Terminal 而不是 External Terminal,则在使用默认 NetBeans 设置运行 C++ 代码时,您也可以立即收到此错误;至少在 Ubuntu 上使用 FLTK 图形时。在这种情况下,它是 NetBeans 内部握手问题而不是源代码问题。

回答by American Patriot

As DragonLord said, it is not a source code issue. You will be able to tell because that should happen whenever you run any C++ code. I had the same problem. I did something slightly different than DragonLord, though. I set the output of the program to Project Properties>Run>Console Type and set it to Standard Output.

正如 DragonLord 所说,这不是源代码问题。您将能够分辨出来,因为无论何时运行任何 C++ 代码都应该发生这种情况。我有同样的问题。不过,我做了一些与 DragonLord 略有不同的事情。我将程序的输出设置为 Project Properties>Run>Console Type 并将其设置为 Standard Output。

回答by A human being

You're yourself initializing network to null, in second line of your code, then it will obviously throw NullPointerException

您自己在代码的第二行将网络初始化为 null,然后它显然会抛出 NullPointerException