-1
public class GameActivity extends ActionBarActivity {
    private int highScore;
    private String displayWord = "";
    private String answerWord;
    private String guessList = "";
    private EditText userGuess;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game2);
        int i =0;

        ImageView mainImage = (ImageView) findViewById(R.id.gallows);
        mainImage.setImageResource(R.drawable.gallow);

        try {
            randWord(readFile());
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println(answerWord);

        while (i < answerWord.length()){
            displayWord += "-";
            i++;
        }
        TextView displayText = (TextView) findViewById(R.id.display_word);
        displayText.setText(displayWord);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private List readFile() throws Exception {
        List<String> list = new ArrayList<>();
        FileInputStream in = (FileInputStream) getAssets().open("food.txt");
        Scanner scnr = new Scanner(in);

        while (scnr.hasNext()){
            list.add(scnr.next());
        }
        scnr.close();
        return list;
    }
    private void randWord(List list) throws Exception{


        Random rnd = new Random();
        answerWord = (String) list.get(rnd.nextInt(list.size()));

    }
}

I am trying to make a relatively simple hangman app in Android Studio but I have run into a problem with reading in a text file and handling IOExceptions for the file reading. The error that comes up in the console is:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
        at cisc181.myproject_1.GameActivity.onCreate(GameActivity.java:44)

From that error, I know that answerWord is never having a value assigned to it. So, how can I fix this problem? Could it be the way that I am reading in the file?

nconway
  • 11
  • 4

1 Answers1

1

One of the problems is that you are getting an Exception throw when you don't want one throw, because you code AFTER the try-catch block needs an exception to NOT be throw.

That is because randWord or readFile is throwing the exception. That method is what assigns answerWord a value. Only when answerWord has a value, can you call .length()

So, try this...

    try {
        randWord(readFile());

        System.out.println(answerWord);

        while (i < answerWord.length()){
            displayWord += "-";
            i++;
        }
        TextView displayText = (TextView) findViewById(R.id.display_word);
        displayText.setText(displayWord);

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

Right now you will still be getting an Error....actually you will get to understand why the Exception is being throw

Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58
  • That fixed the IOException, but my file is still not being read correctly. I am getting FileNotFound error in the log but the program is stable. Is the way that I am reading the file in the correct way for an android app? – nconway May 18 '15 at 23:14
  • OK. I seen that way you dealt with files, and I knew that must have been a source of issue too. Take a look here ... http://stackoverflow.com/questions/12421814/how-can-i-read-a-text-file-in-android – Christopher Rucinski May 18 '15 at 23:18
  • I see you are dealing with Assets. Where is the file stored...what is the path? – Christopher Rucinski May 18 '15 at 23:24
  • C:\\myProj\\app\\src\\main\\assets – nconway May 18 '15 at 23:52
  • So in the assets folder you have food.txt? – Christopher Rucinski May 19 '15 at 00:51
  • Yes, but I believe I have figured it out and it seems to have begun to work after a few changes. Here is the updated readFile method if you want to see it. http://pastebin.com/cicsR2hS – nconway May 19 '15 at 03:32