0

I haven't written any Java in years and I went back to refresh my memory with a simple 'read-from-file' example. Here is my code..

import java.io.*;
public class filereading {

    public static void main(String[] args) {
        File file = new File("C:\\file.txt");
        FileInputStream fs = null;
        BufferedInputStream bs = null;
        DataInputStream ds = null;

        try
        {
            fs = new FileInputStream(file);
            bs = new BufferedInputStream(bs);
            ds = new DataInputStream(ds);

            while(ds.available()!= 0)
            {
                String readLine = ds.readLine();
                System.out.println(readLine);
            }

            ds.close();
            bs.close();
            fs.close();
        }

        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

This compiles fine (although apparently ds.readLine() is deprected), but at runtime, this gives me

Exception in thread "main" java.lang.NullPointerException at java.io.FilterInputStream.available(Unknown Source) at filereading.main(filereading.java:21)

What gives?

3
  • Also please note that .available() probably doesn't know what you think it does. Commented Jan 11, 2010 at 16:16
  • 1
    Not in the question, but you should close the resource (FileInputStream) in a finally block. To aid this pull assignment out of the try block (and make the variable final). Commented Jan 11, 2010 at 16:19
  • @Tom Hawtin. +1. And only do this once on the most-derived resource handle, in this case DataInputStream ds. Commented Jan 11, 2010 at 16:37

2 Answers 2

6

You made a simple typo:

ds = new DataInputStream(ds);

should be

ds = new DataInputStream(bs);

Your code is initializing the DataInputStream with a null source, since ds hasn't been created yet.

Having said that, Jon Skeet's answer gives a better way to write a file-reading program (and you should always use Readers/Writers rather than Streams when dealing with text).

Sign up to request clarification or add additional context in comments.

4 Comments

Solution: Use much better variable names!
The line above that is incorrect too - it should be BufferedInputStream(fs) rather than BufferedInputStream(bs)
Heh, I didn't even notice that one. Better variable names would definitely help here.
I've gotta stop writing code so early in the morning. Thanks!
3

To read a text file, use BufferedReader - in this case, wrapped round an InputStreamReader, wrapped round a FileInputStream. (This allows you to set the encoding explicitly - which you should definitely do.) You should also close resources in finally blocks, of course.

You should then read lines until readLine() returns null, rather than relying on available() IMO. I suspect you'll find that readLine() was returning null for the last line in the file, even though available() returned 2 to indicate the final \r\n. Just a hunch though.

String line;
while ((line = reader.readLine()) != null)
{
    System.out.println(line);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.