0

I'm trying to build a simple app for Android can't figure out why my app keeps crashing I have traced it down to this line of code

<code>WG.setRandomWord(words.get(rand.nextInt(words.size())));</code>

I have check my logcat and it showing that my word file is not even being detected com.sagproductions.gamertaggenerator W/System.err: java.io.FileNotFoundException: words.txt: open failed: ENOENT (No such file or directory) but I have my file word file in the document structure.

for more context here is my code:

JAVA wordGenerator

package com.sagproductions.gamertaggenerator;


public class WordGenerator {
    private String mRandomWord;
    private int mRandomNum;

    public WordGenerator(String randomWord, int randomNum){
        mRandomNum=randomNum;
        mRandomWord=randomWord;
    }

    public int getRandomNum(){
        return mRandomNum;
    }

    public void setRandomNum(int randomNum){
        mRandomNum=randomNum;
    }

    public String getRandomWord(){
        return mRandomWord;
    }

    public void setRandomWord(String randomWord){
        mRandomWord=randomWord;
    }
}

Java MainActivity

package com.sagproductions.gamertaggenerator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.lang.String;
import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    WordGenerator WG;
    private Button mSingleWordGenerator;
    private Button mTwoWordGenerator;
    private Button mThreeWordGenerator;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSingleWordGenerator = (Button)findViewById(R.id.button);
        mSingleWordGenerator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                generateWord();
                editText=(EditText)findViewById(R.id.editText);
               editText.setText(WG.getRandomWord(),TextView.BufferType.EDITABLE);
            }
        });

        mTwoWordGenerator = (Button)findViewById(R.id.button2);
        mTwoWordGenerator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        mThreeWordGenerator = (Button)findViewById(R.id.button3);
        mThreeWordGenerator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    public void generateWord(){
        final File file = new File("words.txt");
        ArrayList<String> words= new ArrayList<String>();
        Random rand=new Random();

        try(final Scanner scanner=new Scanner(file)){
            while(scanner.hasNextLine()){
                String line = scanner.nextLine();
                words.add(line);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        WG.setRandomWord(words.get(rand.nextInt(words.size())));
    }
}

2000rs25
  • 47
  • 6
  • https://stackoverflow.com/questions/14768191/how-do-i-read-the-file-content-from-the-internal-storage-android-app – OneCricketeer Jan 12 '18 at 00:03
  • https://stackoverflow.com/questions/18302603/where-do-i-place-the-assets-folder-in-android-studio and https://stackoverflow.com/questions/9544737/read-file-from-assets – OneCricketeer Jan 12 '18 at 00:05

1 Answers1

0

The WG field is never initialized, so it's null. So you're probably getting a NullPointerException when you call that method. You should initialize it somewhere, perhaps when you declare it (so WordGenerator WG; would be replaced by something like WordGenerator WG = new WordGenerator(INITIAL_RANDOM_WORD, INITIAL_RANDOM_VALUE);, where INITIAL_RANDOM_WORD and INITIAL_RANDOM_VALUE are some values you define elsewhere).

Edit: It seems that that line is causing the problem because words.size() is 0, which is because of the FileNotFoundException. The correct way to read that file will depend on where you're putting it. See the answers to this question (and the pages they link to) for different ways to correctly read text files in Android.

drmercer
  • 430
  • 8
  • 11
  • I tried that to no avail. I checked the android monitor again and it is saying that the exception is java.lang.IllegalArgumentException at Random.nextInt at generateword – 2000rs25 Jan 11 '18 at 22:50