-1

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

爪哇:

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());但这对我来说似乎没有多大意义。

ArrayList<String>在方法上初始化了私有loadNouns。我最初是在ArrayList<String> 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<String> nouns永远不会“加载”应该从 nouns.dat 文件中扫描的单独字符串

更新 2:

爪哇:

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;
    }
}
4

1 回答 1

0

您正在创建一个 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;
}

希望有帮助

于 2013-01-10T22:07:06.183 回答