0

I'm basically running this code:

AddressBook.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addressbook;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author hassan
 */
public class AddressBook extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
        new AddressBookMapper().parseDataFile();
    }
}

AddressBookMapper.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addressbook;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
 *
 * @author hassan
 */
public class AddressBookMapper {
    private File dataFile;
    private FileWriter fileWriter;
    private PrintWriter printWriter;
    public ArrayList lines;

    private void openDataFile() {
        this.dataFile = new File("src/addressbook/datafile.txt");
    }
    public void writeData() {

        System.out.println(this.lines);
        Iterator iterator = this.lines.iterator();
        System.out.println(this.lines.get(0));
        while(iterator.hasNext()) {
            System.out.println();
            System.out.println("-----");
        }
    }
    public void createAddress(String fullName, String email, String telephoneNumber, String mobileNumber) {
        this.openDataFile();
        try {
            this.fileWriter = new FileWriter(this.dataFile, true);
            this.printWriter = new PrintWriter(this.fileWriter);
            this.printWriter.append(fullName + "," + email + "," + telephoneNumber + "," + mobileNumber + "\n");
            this.printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getLastId() {
        return 0;
    }
public void parseDataFile() {
    File dataFile = new File("src/addressbook/datafile.txt");

    try {
        Scanner scanner = new Scanner(dataFile, "UTF-8").useDelimiter("\\n");

        while(scanner.hasNext()) {
            System.out.println(scanner.next());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    this.writeData();
}
    private int findLastAddressId() {
        return 0;
    }
}

My data file text:

ff,ff,fff,ff
krfr,frffr,frfs,ff
a,a,b,c
d,a,f,e
fa,e,f,a

But, for some reason, I get this output:

ff,ff,fff,ff
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
krfr,frffr,frfs,ff
a,a,b,c
d,a,f,e
fa,e,f,a
null
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
    at addressbook.AddressBookMapper.writeData(AddressBookMapper.java:36)
    at addressbook.AddressBookMapper.parseDataFile(AddressBookMapper.java:69)
    at addressbook.AddressBook.main(AddressBook.java:36)
    ... 11 more
Exception running application addressbook.AddressBook
Java Result: 1

Any solution? I am basically trying to get the text from my data file line by line and stack it up into my ArrayList. Also, when writing my datafile adds a blank line in the end of the document.

Hassan
  • 13
  • 6

1 Answers1

1

Can you please try -

while(scanner.hasNext()) {
    lines.add(scanner.next());
}
Kartic
  • 2,575
  • 5
  • 19
  • 39