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