0

I am getting an exception in this program. I have tried to do some changes but its still not working. I am trying to write data from demo.txt to demo1.txt but its giving a NullPointerException. What am I doing wrong here?

import java.io.*;

class CopyFile{

    public static void main(String[] args){

        String str = null ;

        try {
        File f = new File("/home/newlabuser/workspace/CopyFileDemo/src/demo.txt");
        if(f.exists())
        {
            if(f.canRead())
            {
                FileInputStream fin = new FileInputStream(f);
                BufferedInputStream bin = new BufferedInputStream(fin);
                DataInputStream din = new DataInputStream(bin);

                while((str=din.readLine())!=null)
                    System.out.println(str);
                    writeTextFile("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt",str);
                    din.close();
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    }

    public static void writeTextFile(String fileName, String s) {
        FileWriter output;
        try {
            output = new FileWriter("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt");
            BufferedWriter writer = new BufferedWriter(output);
            writer.write(s);
            writer.close();
        } 
        catch (IOException e) {
        e.printStackTrace();
        }
    }
}

java.lang.NullPointerException

at java.io.Writer.write(Writer.java:140)

at CopyFile.writeTextFile(CopyFile.java:37)

at CopyFile.main(CopyFile.java:21)

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • 4
    The exception should also give you the line number where it is thrown, which should help you narrow down the problem. – assylias Apr 16 '13 at 14:37
  • And can you mark the line visually in the code sample? Really helps us out if you say, "I'm getting exception and it's happening on the following line." Would also be helpful if you posted the actual exception output. – jefflunt Apr 16 '13 at 14:38
  • NullPointerException occurs when there is null pointer and it tells the line number. – Ahmet DAL Apr 16 '13 at 14:38
  • 1
    @assylias The post contains the stacktrace..., at the very end of this post – Reporter Apr 16 '13 at 14:39
  • Fault happens at `writeTextFile("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt",str); ` per stack trace. – Floris Apr 16 '13 at 14:41
  • @user2229178 I am wondering why you need `fileName` as an argument, but don't use it in your `FileWriter`. – Joetjah Apr 16 '13 at 14:44

5 Answers5

8

You're missing braces around the body of your while loop.

Therefore, Java parses it like this:

while((str=din.readLine())!=null) {
    System.out.println(str);
}

writeTextFile("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt",str);
din.close();

Thus, the writeTextFile() line only runs after the while loop finishes – when str is null.


The moral is Always indent your code correctly.
Use your IDE to automatically format your code, and this kind of problem will be much more obvious.

Alternatively, switch to a whitespace-sensitive langauge such as Python.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
8

Don't underestimate the use of curly braces with loops. Here's your code:

while((str=din.readLine())!=null)
    System.out.println(str);
    writeTextFile("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt",str);
    din.close();

Here, only System.out.println(str) will be running in a loop, and the next line will be executed after the loop exits, and obviously str here will be null. So when you pass null to the BufferedWriter's write() method - you get a NullPointerException. Fix your code to be:

while((str=din.readLine())!=null) {
        System.out.println(str);
        writeTextFile("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt",str);
}
din.close();

and you should be fine.

Egor
  • 39,695
  • 10
  • 113
  • 130
0

Try this:

File fdir = new File("/home/newlabuser/workspace/CopyFileDemo/src");
FileWriter file = new FileWriter(new File(fdir,"demo1.txt"));
file.write(new Scanner(new File(fdir,"demo.txt")).useDelimiter("\\Z").next());
file.close();
Daniel Kec
  • 529
  • 2
  • 8
0

Also, to avoid more trouble later:

You pass the fileName to thr writeTextFile-methode, but never use it. So instead of doing:

new FileWrite("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt");

You sould do:

new FileWriter(fileName);

The original question has already been answered by Egor.

Bluddymarri
  • 313
  • 1
  • 3
  • 12
0

It may be going wrong here:

while((str=din.readLine())!=null) {
    System.out.println(str);
    writeTextFile("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt",str);
    din.close();
}

You close din in each loop. Perhaps it should be outside of the loop.

It may be going wrong here:

while((str=din.readLine())!=null) {
    System.out.println(str);
    writeTextFile("/home/newlabuser/workspace/CopyFileDemo/src/demo1.txt",str);
}
din.close();
Joetjah
  • 6,292
  • 8
  • 55
  • 90