gpt4 book ai didi

java - 从 main 调用方法会抛出 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 08:24:57 25 4
gpt4 key购买 nike

我尝试从我的 main 方法调用方法 ss,但它抛出以下异常

Exception in thread "main" java.lang.NullPointerException        at teste1.exp.ss(exp.java:16)        at teste1.Main.main(Main.java:64)Java Result: 1
public class Main {
public static void main(String[] arguments) {
...................
private static String[] ff;
exp mega = new exp();
mega.ss(ff);

}

class exp {

public void ss (String gvanswer[]){


String answer[] = new String[3];

answer[0] = "pacific ";
answer[1] = "everest";
answer[2] = "amazon ";

if (gvnswer[0].equals("pacific"))
{System.out.println("eeeeeeeeeeeeee ");}

if (gvanswer[1].equals(answer[1])){System.out.println("l ");}

}

最佳答案

你调用了mega.ss(ff)但是ff从未被初始化过:

ff = new String[1];
ff[0] = "foo";

关于java - 从 main 调用方法会抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4712722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com