0
public class OpenFile {


public static void OpenFile() throws IOException {

    ArrayList<String> lineList = new ArrayList<String>();

    Variablen.desktop = Desktop.getDesktop();
    Variablen.fileChooseropen = new FileChooser();
    Variablen.fileChooseropen.setTitle("Open File");
    Variablen.fileChooseropen.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("TXT Files (*.txt)","*.txt"));
    Variablen.fileChooseropen.setInitialFileName("file.txt");
    Variablen.file = Variablen.fileChooseropen.showOpenDialog(null);

    if(Variablen.file != null) {
        Variablen.inputStream = new FileInputStream(Variablen.file);
        Variablen.bufferedReader = new BufferedReader(new InputStreamReader(Variablen.inputStream));
        Variablen.stringBuilder = new StringBuilder();
        String line;
        while ((line = Variablen.bufferedReader.readLine()) != null) {
            lineList.add(line);
        }

    }
        /**
         *  Creating an Array,to save the content and give it to the Textarea
         */
            String[] linearray = new String[lineList.size()];
            lineList.toArray(linearray);
            System.out.println(Arrays.toString(linearray));
            Variablen.bufferedReader.close();
            Variablen.textarea.setText(String.valueOf(linearray));
    }
}

Caused by: java.lang.NullPointerException at sample.OpenFile.OpenFile(OpenFile.java:45) at sample.Controller.ListenOpenFile(Controller.java:14) ... 54 more

Pat37
  • 11
  • 4
  • 2
    Not sure what exactly is null, but my guess would be on `textarea`. There's not enough info to help identify the source of this issue though. Note though that the way you're handling your data is extremely bad practice. You seem to be storing everything for static access even though it would be best to seperate the logic for reading data from a file and the gui; additionally access to the data should be limited as much as possible: E.g. note the your `BufferedReader` will not be of any use after the method terminates; there's absolutely no reason not to make it a local variable – fabian Feb 03 '20 at 17:36
  • Thanks for the comment! I know it's not perfect. Its my first own project and I just want to learn Java because its very interesting. I will correct the static access from the variables and seperate. That will be stressfull for me haha – Pat37 Feb 04 '20 at 11:15

0 Answers0