mercredi 12 octobre 2016

Reading a txt-file works within a method, but the returned variable is always null

I have written a method to read a text file with some stats, and to return a string array with said stats, when it finds the line with attindx in it. If I print out the stats within the method, everything works fine. However, if I try to work with the returned string array outside getList() I always get a null pointer exception. Why is this?

public static String[] readList(String attindx) {
    File file = new File("D:\\Dokuments\\MyFolder\\SomeFolder\\example.txt");
    Scanner scanner;
    try {
        scanner = new Scanner(file);
        int lineNum = 0;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String[] data = line.split("\\s+");
            lineNum++;
            if(data[0].equals(Textdatei.attindx)) {
                //System.out.println(data[1]);
                Textdatei.readAttname(line);
                Textdatei.readAtttype(line);
                Textdatei.readAttschaden(line);
                Textdatei.readAttaccu(line);
                Textdatei.readAttap(line);
                Textdatei.readAttmode(line);
                return data;
            }
            else {

            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

Aucun commentaire:

Enregistrer un commentaire