I am pretty new to JAVA so this will just be a basic question:
I get java.lang.nullpointerexception error when trying to reade and rewrite a story from some certain txt files, I have 3 classes in my Java project:
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) {
}
}
Input Reader:
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;
}
}
and Output Writer:
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);
}
}
}
ERROR LINE: for(int i = 0; i < story.size(); i++)
So I'm getting this Error when trying to read and rewrite a story, hope this isn't that bad explanation!