我在Eclipse中遇到NullPointerException错误

时间:2013-01-10 21:05:43

标签: java arrays arraylist nullpointerexception

我在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;
    }
}

1 个答案:

答案 0 :(得分: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个您从未初始化的ArrayLists,因此它们为null ...

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

希望有所帮助