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