0

When i run my java code:

public class LalaLuLu {
public static void main(String[] args) {

    try{ 
        String path = "D:/Gem/FINAL/Datatest.csv";
        File file = new File(path);
        InputStream is;
        System.out.println(file.exists());

        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        PrintWriter os = new PrintWriter ( new FileOutputStream("D:/Gem/FINAL/out_datatest1.txt"));
        String output = "";
        int count = 0;
        do{
            output = br.readLine();
            String[] a = output.split(";");
            System.out.println("total kolom: " + a.length);
            os.println(output);
            System.out.println(count++ );
        } while (!output.equals(""));

    } catch (Exception e) {
        e.printStackTrace();
    }
} }

When i run, the error on this line :

output = br.readLine();
String[] a = output.split(";");

And error I am getting is:

java.lang.NullPointerException
  at DAO.readdata.readdataCSV(readdata.java:47)

Can you tell me how to solve this error? Thank you

rurururu
  • 9
  • 1
  • What is there at line 47, did you check value of that variable in debugger ? If yes what is that value. – Vipin Mar 14 '17 at 03:48

1 Answers1

0

Check the docs for how readLine() works: https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine().

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

It is reading the end of the file and returning null.

The simplest fix would be the following

 do{
     output = br.readLine();
     if (output == null ) {
         break;
     }
     String[] a = output.split(";");
     System.out.println("total kolom: " + a.length);
     os.println(output);
     System.out.println(count++ );
 } while (!output.equals(""));
pgreen2
  • 3,601
  • 3
  • 32
  • 59