0

i would like to build a text data-cleaner in Java, which cleans the text from Smileys and other special charakter. I wrote a text reader, but he stops after 3/4 of Line 97 and i just don't know why he does it? Normally he should read the complete text file (ca. 110.000 Lines) and then stop. It would be really nice if could show me where my mistake is.

public class FileReader {

public static void main(String[] args) {
    String[] data = null;
    int i = 0;
    try {

        Scanner input = new Scanner("C://Users//Alex//workspace//Cleaner//src//Basis.txt");

        File file = new File(input.nextLine());

        input = new Scanner(file);


        while (input.hasNextLine()) {
            String line = input.nextLine();
            System.out.println(line);
            data[i] = line;
            i++;
        }
        input.close();
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }

    System.out.println(data[97]);
}

}

Alex
  • 27
  • 5
  • You never initialized array variable with proper array object. Instead `String[] data = null;` and when you call `data[i] = line;` it means you are trying to access `[i]` on `null` which isn't array. This either is not your real code or duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/q/218384) – Pshemo Mar 29 '18 at 14:32
  • Why are you making the file the input.nextLine()? try just making the new File argument the input. So it would be `File file = new File(input.nextLine());` – cjnash Mar 29 '18 at 14:33

1 Answers1

2

Your mistake is here: String[] data = null;

I would expect this code to throw null pointer exception...

You can use ArrayList instead of plain array if you want to have dynamic re-sizing

Maciej
  • 1,954
  • 10
  • 14
  • I changed it to String[] data = new String[111000]; My main problem is not the saving of the data. In the while loop every line entry gets printed and it stopps in Line 97. – Alex Mar 29 '18 at 15:13