954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Text Game problem

Hi,
I've got a problem with my project. When I initialize an arraylist, the content is displayed and you can go through the arraylist by correctly answering the given word. But when I load a new arraylist from a file, the old arraylist is removed and the new one is filled in by the reader. But it isn't transfered to the setter of the other class. Neither is the size transfered to the setter of the other class. So when I run I sometimes have a NullPointer or I do not have a "repaint". I must say the code is in dutch.
This is my load():

public void laden() {
        this.lstWoorden = new ArrayList<String>();
        BufferedReader in;
        File file = null;
        String regel;
        int regelNummer = 1;
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File("C:/"));
        int returnVal = fc.showOpenDialog(Spelpaneel.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            try {
                in = new BufferedReader(new FileReader(file));
                while ((regel = in.readLine()) != null) {
                    System.out.println(regelNummer + ": " + regel);
                    lstWoorden.add(regel);
                    regelNummer++;
                    //spelling.setLengte(regelNummer);
                }
                spelling = new Spelling(lstWoorden);
                System.out.println("Woordenlijst.size: " + lstWoorden.size());
                int lengte = lstWoorden.size();
                spelling.setLengte(lengte);
                begin = true;
                System.out.println("Woordenlijst voor sluiten: " + lstWoorden);
                in.close();
                repaint();
                System.out.println("Woordenlijst na sluiten: " + lstWoorden);
            } catch (FileNotFoundException ex) {
                System.out.println("FileNotFound");
            } catch (IOException ie) {
                System.out.println("IO Exception");
            } //catch (NullPointerException ne) {
               // System.out.println("Nullpointer Exception");
            //}
        }
    }

And this is my other class which should get the arraylist:

public class Spelling {

   private ArrayList<String> lstWoorden;
    private int intLengte = 1;

    public Spelling(ArrayList<String> lstWoorden) {
        this.lstWoorden = lstWoorden;
        System.out.println("Nieuwe woordenlijst: " + lstWoorden);
    }

    public String getWoord(int index) {
       System.out.println("Woordenlijst: " + lstWoorden);
       return lstWoorden.get(index);
    }

    public int getLengte() {
        System.out.println("getLengte: " + intLengte);
        return intLengte;
    }
    
    public void setLengte(int lengte){
        System.out.println("setLengte: " + intLengte);
        this.intLengte = lengte;
    }
}

This is my output from System.out.println-testing:
New arraylist: [Turnhout, Tielen, Lichtaart, Mol, Dessel, Retie, Kasterlee, Westerlo, Tongerlo, Tessenderlo, Langemark, Ieper, Westouter, Lombardsijde, Kuttekoven, Wortel, Reet, Herstappe, Mesen, Bever, Zuienkerke]
Arraylist.size: 21 (same variable as the next one)
OtherClass.setLength: 1

sieuwe
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

I didn't go through your code that much, but why are you setting and getting lengths? Do you know that List has a method size(), which returns the number of objects in the list, i.e. the length?

What length is it that your setting?

SasseMan
Junior Poster
176 posts since Jan 2010
Reputation Points: 70
Solved Threads: 19
 

What is the point of the "Spelling" class? It's just an ArrayList, with some stuff for the "length" (current size) that is, as SasseMan said, totally redundant.
Your main problem comes because you have two ArrayLists of words, and updating one doesn't update the other. Fix this by not having two ArrayLists. Either get rid of the Spelling class and have an ArrayList of words in the main class, or keep the Spelling class (without the length variable) and get rid any other ArrayLists of words.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: