0

I've written a main method for it also, but I can't tell the source of the error. This is the code I'm using:

import java.io.*;
import java.util.*;
 public class WordList{
  private ArrayList<String> words;

 public WordList(String filename){
ArrayList<String> words = new ArrayList<String>();
}

public ArrayList<String> openFile(String filename) throws IOException{
FileReader fr= new FileReader(filename);
//create a Filereader object
BufferedReader textReader= new BufferedReader(fr);
//create a BR object
String line = textReader.readLine();
while (textReader.readLine() != null){
  words.add(line);
  textReader.readLine();
}
textReader.close();
return words;
}
Random r= new Random();
public String getRandomWord(){
String x= new String();
int y=r.nextInt(words.size());
x= words.get(y);
return x;

 }
}

This is the main method I used for testing my code:

import java.io.*;
import java.util.*;
 public class Test{
  public void main(String args[])throws IOException{
 String path= "C:/Users/Cyril/Desktop/COMP 202/Assignment 4/Text files/Majors.txt" ;

 try {
WordList list = new WordList(path);
ArrayList<String> majors = new ArrayList<String>();
majors = list.openFile(path);
System.out.println(majors);
}

catch (IOException e){
  System.out.println( e.getMessage());
  }
 }
}

I am getting a null pointer error. I can't find the source of it. The question I had was:

Write a class WordList with a private arraylist that reads a textfile and stores every line as an entry in the arraylist. I've added the random method to generate random words from the arraylist.

2
  • Can you post the stack trace? Commented Dec 3, 2013 at 19:24
  • The instance should use the interface, e.g. List<String> not ArrayList. Commented Dec 3, 2013 at 19:25

4 Answers 4

3

You've declared a local variable which shadows the instance member

public WordList(String filename){
    ArrayList<String> words = new ArrayList<String>();
}

change to

public WordList(String filename){
    words = new ArrayList<String>();
}

But also see Kugathasan's answer...which they just deleted.

This snippet

String line = textReader.readLine();
while (textReader.readLine() != null){
    words.add(line);
    textReader.readLine();
}

You are reading 3 lines from the input stream. Is that what you want?

Sign up to request clarification or add additional context in comments.

Comments

0
private ArrayList<String> words;

public WordList(String filename){
ArrayList<String> words = new ArrayList<String>();
}

In this part of your code , the constructor is creating another array list and the one in your class(instance variable ) is not linked to it.

please change your code to :

private ArrayList<String> words;

public WordList(String filename){
this.words = new ArrayList<String>();
}

It should then work :)

Comments

0

You are declaring a local variable with same name as instance variable in the constructor instead of assigning the instance variable.
So Change the declaration to assignment in your constructor ,like this.

public WordList(String filename){
   words = new ArrayList<String>();
}

Comments

0

Change

public WordList(String filename) {
    ArrayList<String> words = new ArrayList<String>();
}

to

  public WordList(String filename) {
     words = new ArrayList<String>();
}

Because you have shadowed your instance member words in the constructor, words is not initialized and defaults to null and null.Something is a NullPointerException

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.