0

I have been trying a lot of things and I just ready to ask for some help. if this is not enough info please let me know. I have tried Scanner, BufferReader, etc from searching posts with no luck.

I have the file words.txt right in the src directory.

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;


    /*
     * CLASS THAT HANDLES GETTING AND SETTING THE SECRET WORD RANDOMLY
     */

    public class secretWord {
        private ArrayList theWordList;
        private Scanner scanner; //Used to read the file into the array
        private Random random; //Generates a random number index for choosing a random array element
        private int randomIndex; //Holds the random index generated
        ArrayList theSecretWord= new ArrayList(); //The secret word converted to char[] for use in the program
        String tempSecretWord; //Secret word selected as string

        //Constructor: runs methods to create arraylist of words then select a random element for thesecretword
        public secretWord(){
            createArray();
            getSecretWord();
        }
        //Creates an ArrayList of words from file
        private void createArray(){
            theWordList= new ArrayList();
            String file= getClass().getResource("words.txt").getPath();
            File f= new File(file);
            try{
            Scanner scanner= new Scanner(f);

            while(scanner.hasNext()){
                theWordList.add(scanner.nextLine());
            }

            }catch(Exception e){
                e.printStackTrace();
            }finally{
                scanner.close();
            }
        }
        //Selects a random number from the ArrayList to use as the secret word
        private void getSecretWord(){
            random= new Random();
            randomIndex= random.nextInt(theWordList.size());
            theSecretWord.add(theWordList.get(randomIndex).toUpperCase());
        }
        //Removes the secretWord and gets a new one for another play
        void refreshWord(){
            theSecretWord.clear();
            getSecretWord();
        }
    }
2
  • Exception in thread "main" java.lang.NullPointerException at secretWord.createArray(secretWord.java:27) at secretWord.<init>(secretWord.java:21) at theFrame.<init>(theFrame.java:33) at theGame.main(theGame.java:6) Commented Jul 12, 2013 at 16:08
  • When i use Scanner scanner= new Scanner(new File(words.txt)); I get file not found exception. Commented Jul 12, 2013 at 16:08

2 Answers 2

1

Use the top level ContextClassLoader to get the file.

     InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("words.txt");
     Scanner in = new Scanner(inStream);
Sign up to request clarification or add additional context in comments.

Comments

0

Use this code line instead:

String file= getClass().getResource("words.txt").getFile();

Note the difference between path and file. And start your Java class names with capital letters, this is a convention.

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.