3
private String[] words;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDecorView = getWindow().getDecorView();

    loadWords();

    TextView tv = (TextView) findViewById(R.id.word);
    tv.setText(words[0]);
}

 public void loadWords()
{

    try {
        InputStream file = new FileInputStream("words.txt");
        InputStreamReader sr = new InputStreamReader(file);
        BufferedReader br = new BufferedReader(sr);

        int n = 0;
        while(br.readLine() != null)
        {
            words[n] = br.readLine();
            n++;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Ok, so I am only trying to print out the first element in the array, but the app crashes during launch and gives me the error "Attempt to read from null array"

EDIT - Solution
-I had not initialize the array.(I knew I had 100 lines)
-My input stream was incorrect (my file could not be found)
-I tried to update a TextView from a secound layout(that was not selected at the time)

String[] words = new String[100];

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDecorView = getWindow().getDecorView();
    loadWords();
}

public void changeView(View view) {

    setContentView(R.layout.game_view);
    TextView tv = (TextView) findViewById(R.id.word);
    tv.setText(words[0]);
}

public void loadWords()
{
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("words.txt")));
        for(int i = 0;i<words.length;i++)
        {
            words[i] = br.readLine();
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Siber
  • 33
  • 5
  • check `word.lenght >0` before accessing value from array. – Rustam Sep 03 '15 at 12:44
  • use dynamic list `ArrayList` instead of `String[]` – Rustam Sep 03 '15 at 12:46
  • @Rustam: probably a pretty bad idea; as his code looks `words` is null. Calling .length on null will not do much good though. And your second comment is also not too helpful - just exchanging the type will not help at all; as another declaration without initialization will still cause his program to fail with an NullPointerException. – GhostCat Sep 03 '15 at 12:47
  • @Jägermeister yes you are right. but second hint is for using dynamic list instead of static list. since it's just comment not actual code. hope you understand. – Rustam Sep 03 '15 at 12:48
  • 1
    Check http://stackoverflow.com/questions/16100175/store-text-file-content-line-by-line-into-array -- it appears to be exactly what you're looking for. – kheffner Sep 03 '15 at 14:31

2 Answers2

2

You need to initailize your array, which you didn't do. Declaration of array and initializing is different thing isn't it?

Initializing of array will be done like this :

private String[] words = new String[2000];

Please try. However, try replacing with ArrayList instead of array

Karthik R
  • 4,363
  • 2
  • 14
  • 27
1

Most likely you never initialized your array. You just declared it.

The point is: your code just says: I would like to use an array of Strings (String[] words).

But in order to actually do that - you have to create an array object to be filled ( see here on the various ways how to do that)

On the other hand: "just creating an array"; might be pretty hard; given the fact that you might not know how many rows you will need in your array (but you need to know that when initializing the array object).

So, I suggest to use a dynamic collection class like ArrayList<String> instead of the fixed-size array. Just google it; and to the research that you should have done before posting this question ... well, afterwards.

Community
  • 1
  • 1
GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • Thank you for a very helpful answer! I usually have a tendency to forget things like these, when I haven't been programming in a while... Anyway after initializing the array I was able to proceed to find a few more errors. I'll update my post with the working solution. Btw, I did search a whole lot on google in order to find my issue but I wasn't sure what my error came from(I might not be the best in finding information on google) but there is no need for that rudeness tho. – Siber Sep 03 '15 at 19:58