【问题标题】:I'm getting a NullPointerException error in Eclipse我在 Eclipse 中收到 NullPointerException 错误
【发布时间】:2013-01-10 21:05:43
【问题描述】:

我在 Eclipse 中收到 NullPointerException 错误。现在的代码:

Java:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System. * ;

public class MadLib {
    private ArrayList<String> verbs = new ArrayList<String>();
    private ArrayList<String> nouns = new ArrayList<String>();
    private ArrayList<String> adjectives = new ArrayList<String>();

    public MadLib() {}

    public MadLib(String fileName) {
        //load stuff
        try {
            Scanner file = new Scanner(new File(fileName));
        }
        catch(Exception e) {
            out.println("Houston we have a problem!");
        }
    }

    public void loadNouns() {
        nouns = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner("nouns.dat");
            while (chopper.hasNext()) {
                nouns.add(chopper.next());
            }
            chopper.close();
            out.println(nouns);
        }
        catch(Exception e) {
            out.println("Will");
        }
    }

    public void loadVerbs() {
        verbs = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner("verbs.dat");
            while (chopper.hasNext()) {
                verbs.add(chopper.next());
            }
            chopper.close();
        }
        catch(Exception e) {
            out.println("run");
        }
    }

    public void loadAdjectives() {
        adjectives = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner("adjectives.dat");
            while (chopper.hasNext()) {
                adjectives.add(chopper.next());
            }
            chopper.close();
        }
        catch(Exception e) {}
    }

    public String getRandomVerb() {
        String verb = "";
        int num = 0;
        num = (int)(Math.random() * verbs.size());
        verb = verbs.get(num);
        return verb;
    }

    public String getRandomNoun() {
        String noun = "";
        int num = 0;
        num = (int)(Math.random() * nouns.size());
        noun = nouns.get(num);
        return noun;
    }

    public String getRandomAdjective() {
        String adj = "";
        int num = 0;
        num = (int)(Math.random() * adjectives.size());
        adj = adjectives.get(num);
        return adj;
    }

    public String toString() {
        String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
        return output;
    }
}

Eclipse 指出发生在num = (int)(Math.random()*nouns.size()); 行的问题,但这对我来说似乎没有多大意义。

我在loadNouns 方法中初始化了私有ArrayList&lt;String&gt;。我最初将ArrayList&lt;String&gt; nouns 初始化为getRandomNoun(),但这引发了不同的错误,因此建议我将初始化语句移至loadNouns 方法。

跑步者类:

import static java.lang.System.*;

public class Lab16d 
public static void main( String args[] ) {
     //make a new MadLib
     MadLib fun = new MadLib();
     out.println(fun);
}

更新:

真正的问题似乎是ArrayList&lt;String&gt; nouns 永远不会被应该从 nouns.dat 文件中扫描的单独字符串“加载”

更新 2:

Java:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System. * ;

public class MadLib {
    private ArrayList<String> verbs = new ArrayList<String>();
    private ArrayList<String> nouns = new ArrayList<String>();
    private ArrayList<String> adjectives = new ArrayList<String>();

    public MadLib() {
        loadNouns();
        loadVerbs();
        loadAdjectives();
        out.println(nouns);
    }

    public MadLib(String fileName) {
        //load stuff
        loadNouns();
        loadVerbs();
        loadAdjectives();
        try {
            Scanner file = new Scanner(new File(fileName));
        }
        catch(Exception e) {
            out.println("Houston we have a problem!");
        }
    }

    public void loadNouns() {
        nouns = new ArrayList < String > ();
        try {
            //nouns = new ArrayList<String>();
            String nou = "";
            Scanner chopper = new Scanner(new File("nouns.dat"));

            //chopper.nextLine();
            while (chopper.hasNext()) {
                nou = chopper.next();
                out.println(nou);
                nouns.add(nou);
                //chopper.nextLine();
            }
            //chopper.close();
            out.println(nouns.size());
        }
        catch(Exception e) {
            out.println("Will");
        }
    }

    public void loadVerbs() {
        verbs = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner(new File("verbs.dat"));
            while (chopper.hasNext()) {
                verbs.add(chopper.next());
                chopper.nextLine();
            }
            chopper.close();
        }
        catch(Exception e) {
            out.println("run");
        }
    }

    public void loadAdjectives() {
        adjectives = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner(new File("adjectives.dat"));
            while (chopper.hasNext()) {
                adjectives.add(chopper.next());
                chopper.nextLine();
            }
            chopper.close();
        }
        catch(Exception e) {}
    }

    public String getRandomVerb() {

        String verb = "";
        int num = 0;
        num = (int)(Math.random() * (verbs.size() - 1));
        verb = verbs.get(num);
        return verb;
    }

    public String getRandomNoun() {
        String noun = "";
        int num = 0;
        if (nouns == null) {
            loadNouns();
        }
        double rand = (Math.random());
        num = (int)(rand * (nouns.size() - 1));
        out.println(num);
        noun = nouns.get((int) num);
        out.print(noun);
        return noun;
    }

    public String getRandomAdjective() {
        String adj = "";
        int num = 0;
        num = (int)(Math.random() * (adjectives.size() - 1));
        adj = adjectives.get(num);
        return adj;
    }

    public String toString() {
        String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
        return output;
    }
}

【问题讨论】:

  • 当这行代码运行时,nouns 很可能是 null。您可以使用调试器轻松验证这一点。由于您没有发布任何调用MadLibs 类方法的代码,因此无法确定诊断问题。很可能loadNouns() 在此行执行时没有被调用。
  • 看起来就是这样,因为我在 Eclipse 中运行调试器并发现 ArrayList&lt;String&gt; nounsnull 的形式返回。我只是不知道为什么
  • 您何时/何地调用loadNouns()
  • 单步执行您的程序,直到出现问题。 loadNouns() 有人打电话吗?
  • loadNouns() 在从单独的运行器类调用整个类时被调用

标签: java arrays arraylist nullpointerexception


【解决方案1】:

您正在创建一个 MadLib 实例,然后在 Runner 类的 println 中打印该对象...

     //make a new MadLib
     MadLib fun = new MadLib();
     out.println(fun);

out.println 调用您在 MadLib 中覆盖的 toString() 方法...

    String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
    return output;

您的 MadLib 对象有 3 个您从未初始化过的 ArrayList,因此它们为空...

private ArrayList<String> verbs;
private ArrayList<String> nouns;
private ArrayList<String> adjectives

修复NullPointerException 的最简单方法是初始化变量......

private ArrayList<String> verbs = new ArrayList<String>();
private ArrayList<String> nouns = new ArrayList<String>();
private ArrayList<String> adjectives = new ArrayList<String>();

但是,我真正认为您想要做的是在构造对象时加载所有名词、动词和形容词,以便您的 toString 实际上打印一些有用的东西。我也会将此添加到您的构造函数中...

public MadLib() {
  loadNouns();
  loadVerbs();
  loadAdjectives();
}

编辑:您的 getRandom 方法需要检查列表是否为空以避免 IndexOutOfBounds 异常...

public String getRandomVerb() {
    String verb = "";

    if (!verbs.isEmpty()) {
        int num = (int) (Math.random() * verbs.size() - 1);
        verb = verbs.get(num);
    }

    return verb;
}

public String getRandomNoun() {
    String noun = "";

    if (!nouns.isEmpty()) {
        int num = (int) (Math.random() * nouns.size() - 1);
        noun = nouns.get(num);
    }

    return noun;
}

public String getRandomAdjective() {
    String adj = "";

    if (!adjectives.isEmpty()) {
        int num = (int) (Math.random() * adjectives.size());
        adj = adjectives.get(num);
    }

    return adj;
}

希望有帮助

【讨论】:

  • 我已经在Eclipse的代码中加载了所有的名词、动词和adjs,只是没有放在这里。它仍然显示 nouns 的大小为 0(打印出来检查)
  • 我只能关闭您列出的代码,并且在您的示例中永远不会调用加载方法。无论如何,我自己尝试了您在eclipse中列出的代码,并且我提供的答案没有引发任何异常。我不知道 dat 文件中有什么内容。
  • .dat 文件包含该类型单词 fyi 的垂直列表
  • IndexOutOfBounds 错误正是我得到的,我在将 .dat 文件中的单词作为 ArrayList 中的对象时遇到问题,我不确定是什么原因造成的它不起作用。
  • 您必须发布所有代码和堆栈跟踪才能得到您问题的良好答案。
猜你喜欢
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 2016-05-08
  • 1970-01-01
  • 2022-08-24
相关资源
最近更新 更多