我对JAVA很陌生,所以这只是一个基本问题:
在尝试从某些某些txt文件中重新编写故事时,我得到了java.lang.nullpointerexception错误,我的Java项目中有3个类:
StoryCreator:
import java.util.*;
public class StoryCreator {
private InputReader reader;
private OutputWriter writer;
private Random random;
private ArrayList<String> story;
private ArrayList<String> adjectives;
public StoryCreator() {
reader = new InputReader();
writer = new OutputWriter();
random = new Random();
}
public String randomAdjectives(String adjectiveFilename) {
ArrayList<String> adjective = reader.getWordsInFile(adjectiveFilename);
int index = random.nextInt(adjective.size());
return adjectives.get(index);
}
public void createAdjectiveStory(String storyFilename, String adjectiveFilename, String outputFilename) {
ArrayList<String> adjective = reader.getWordsInFile(adjectiveFilename);
for (int i = 0; i < story.size(); i++) {
String word = story.get(i);
if (word.contains("ADJEKTIV.")) {
word = randomAdjectives(adjectiveFilename) + (". ");
story.set(i, word);
}
if (word.contains("ADJEKTIV")) {
word = randomAdjectives(adjectiveFilename) + (" ");
story.set(i, word);
}
}
writer.write(story, outputFilename);
System.out.println(story);
}
public void createAdjectiveStory(String storyFilename, String outputFilename) {
}
public void createAdjectiveStoryFromDictionary(String storyFilename, String dictionaryFilename, String outputFilename) {
}
}
输入阅读器:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Class for reading input from file. You may want to expand it, if needed...
*
* @author (Per Lauvås & Tor-Morten Grønli)
* @version (2.0)
*/
public class InputReader {
/**
* Constructor for objects of class InputReader
*/
public InputReader() {
}
/**
* Return all the words in a file - BufferedReader implementation
*
* @param filename the name of the file
* @return an arraylist of all the words in the file
*/
public ArrayList<String> getWordsInFile(String filename) {
ArrayList<String> words = new ArrayList<>();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename),
"8859_1"));
String line = in.readLine();
while (line != null) {
String[] elements = line.split(" ");
for (int i = 0; i < elements.length; i++) {
words.add(elements[i]);
}
line = in.readLine();
}
in.close();
} catch (IOException exc) {
System.out.println("Error reading words in file: " + exc);
}
return words;
}
/**
* Return all the words in a file - scanner implementation
*
* @param filename the name of the file
* @return an arraylist of all the words in the file
*/
public ArrayList<String> getWordsInFileWithScanner(String filename) {
ArrayList<String> words = new ArrayList<>();
try {
Scanner in = new Scanner(new FileInputStream(filename));
while (in.hasNext()) {
words.add(in.next());
}
} catch (IOException exc) {
System.out.println("Error reading words in file: " + exc);
}
return words;
}
}
和输出作家:
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Class for writing information to a file.
*
* @author (Per Lauvås & Tor-Morten Grønli)
* @version (2.0)
*/
public class OutputWriter {
/**
* Constructor for objects of class OutputWriter
*/
public OutputWriter() {
}
/**
* Writes a list of words to a file. The words are separated by the 'space' character.
*
* @param output the list of words
* @param filename the name of the output file
*/
public void write(ArrayList<String> output, String filename) {
try {
FileWriter out = new FileWriter(filename);
for (String word : output) {
out.write(word + " ");
}
out.close();
} catch (IOException exc) {
System.out.println("Error writing output file: " + exc);
}
}
}
错误行:for(int i = 0; i < story.size(); i++)
所以我在尝试阅读和重写故事时遇到此错误,希望这不是那么糟糕的解释!
答案 0 :(得分:0)
在StoryCreator
中,您可以定义变量故事:
private ArrayList<String> story;
但是这个变量永远不会被初始化,所以它将被设置为默认值null。然后story.size()
将为您提供NullPointerException
。
您需要初始化它(例如在构造函数中):
story = new ArrayList<String>();
您可能希望在其中加入一些值(例如在create*
方法中)。
此外,您应该始终尝试使用接口。所以从
改变private ArrayList<String> story;
到
private List<String> story;