1

What I'm trying to do is add a substring from a String to an ArrayList. basically adding every letter in the string to an index in the ArrayList. After that i have a print statement just to see if the letters were added to the ArrayList (thats the second for loop under makearraylisOfChosenWord). However, when i run this with or without the print statement, it gives me a NullPointerException. Is it because I'm adding the letters to the arraylist in a wrong way in the first for loop? thanks for the help heres the code:

String[] wordList = {"apple", "orange", "strawberry", "banana"};
String chosenWord;

//Make an array list to hold one letter of the chosen word at each index
void makeArrayListOfChosenWord(){
    ArrayList<String> lettersOfChosenWord = new ArrayList<String> ();
    for (int i = 0; i < chosenWord.length(); i++) {
        lettersOfChosenWord.add(chosenWord.substring(i, i+1));
    }
    for (int i = 0; i < lettersOfChosenWord.size(); i++) {
        System.out.println((lettersOfChosenWord.get(i)).toString());
    }

}

//Let the game pick a random word from the word list
void setRandomWord(){
    int wordListLength = wordList.length;
    int pickRandomWord = (int) (Math.random() * wordListLength);
    String createRandomWord = wordList[pickRandomWord];
    chosenWord = createRandomWord;
    System.out.printf("the word is %s letters long", chosenWord.length());
}
10
  • The two methods are entirely separate - how are they related? It would really help if you could provide a short but complete program demonstrating the problem. Commented Dec 10, 2013 at 18:47
  • 1
    actually the setrandomword picks a random word from the word list, and if you look, the instance variable chosenword is given the random word picked, (its in the setrandomword method, line 4) and the makearraylist method uses the chosenword variable Commented Dec 10, 2013 at 18:49
  • Ah, with you. And is the second method definitely called before the first method? This is the sort of thing which a short but complete program would make clear. It would also help if you'd show the stack trace so we know where the exception is happening. Commented Dec 10, 2013 at 18:51
  • 1
    There is no problem, you need to call setRandomWord, then makeArrayListOfChosenWord, and no exception should be thrown Commented Dec 10, 2013 at 18:52
  • ok, working on it, thanks for the tips Commented Dec 10, 2013 at 18:52

2 Answers 2

0

I was just thinking about your problem and would try to use an ArrayList of Character instead of String. This is imo ok, because you mentioned that you are splitting up a String into single Characters, so ArrayList<Character>() seems a reasonable approach.

For splitting up the String I would make use of the method toCharArray():

String str = "abcd...";
ArrayList<Character> chars = new ArrayList<Character>();
for (char c : str.toCharArray()) {
  chars.add(c);
}
4
  • that sounds interesting, i've never seen that method before, thank you for mentioning it, i will try to implement that (after i understand it of course, lol) Commented Dec 10, 2013 at 19:01
  • however, does toCharArray() make it into a regular array or an array list? because I'd like to be able to delete certain letters from the arraylist when the program initiates Commented Dec 10, 2013 at 19:02
  • toCharArray returns you an array. The for-loop adds each of the array elements to your ArrayList. Using this way avoids the substring-method and possible ArrayOutOfBoundExceptions Commented Dec 10, 2013 at 19:04
  • Ok I see, I will try to do that then, Commented Dec 10, 2013 at 19:20
-1

If you call your setRandomWord method before you call makeArrayListOfChosenWord method no NullPointerException will be thrown. No if-check is needed in your code for this condition.

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.