を取得するので、これは単なる基本的な質問になります。私はJAVAにかなり新しいですのjava.lang.NullPointerExceptionエラー
いくつかの特定のtxtファイルから話をREADEと書き換えしようとしたとき、私はjava.lang.NullPointerExceptionがエラーを取得します
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) {
}
}
入力リーダー:
、私は私のJavaプロジェクトでは3つのクラスを持っています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++)
は、だから私は読んで、話を書き換えしようと、このエラーを取得していますが、これはそれほど悪くないことを願います説明! StoryCreator
で
完全なエラーメッセージを確認する必要があります。 –
少なくとも、どの行にエラーがあるのかを教えてください。スタックトレース/エラーメッセージを出力します。 [help]にアクセスし、[ask]を読んでください。 [mcve]の作成について読む。 –