-3

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!

low-key
  • 3
  • 1
  • 3
  • 2
    Need to see the full error message. – Sandeep Roy Nov 01 '17 at 13:08
  • 3
    You should at least tell us what line has the error. And print the stack trace/error message. Visit the [help] and read [ask]. Read about creating a [mcve]. – 001 Nov 01 '17 at 13:08

1 Answers1

0

In StoryCreator, you define the variable story:

private ArrayList<String> story;

But this variable is never initalized, so it will be set to the default value null. Then story.size() will give you a NullPointerException.

You need to initialize it (for example in the constructor):

story = new ArrayList<String>();

And you probably want to put some values into it (for example in your create* methods).

Furthermore, you should always try to work with interfaces. So change from

private ArrayList<String> story;

to

private List<String> story;
Stefan
  • 2,395
  • 4
  • 15
  • 32