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

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?

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.

This article has been dead for over six months. Start a new discussion instead.