Results 1 to 6 of 6
  1. #1
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default NullPointerException

    Java Code:
    BufferedReader br = null;
            try {  
            	String inputFileLine = JOptionPane.showInputDialog("Insert text file: ");
            	while((inputFileLine = br.readLine()) != null) {
    What am i doing wrong? It says Exception in thread "main" java.lang.NullPointerException due to the line
    Java Code:
    while((inputFileLine = br.readLine()) != null) {

  2. #2
    Nickk is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default

    Well, you need to instatiate the BufferedReader.

    br = new BufferedReader(new FileReader(filename));

    See Read from a file using a BufferedReader - A Java Code Example

  3. #3
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    thanks man!
    Last edited by Feng; 11-24-2007 at 06:03 PM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class ExpTest {
        public static void main(String[] args) {
            BufferedReader br = null;
            try {  
                String inputFileLine = JOptionPane.showInputDialog("Insert text file: ");
                // Do we have a file to read?
                if(!new File(inputFileLine).exists()) {
                    System.out.println("Can not find " + inputFileLine);
                }
                System.out.println("Attempting to read " + inputFileLine);
                // Do we have a BufferedReader to which "br" is pointing?
                // In other words, has "br" been instantiated which means
                // is it pointing to an object that has been created with
                // the "new" operator? Let's see:
                System.out.println("br = " + br);
                // Instantiate "br":
                br = new BufferedReader(
                     new InputStreamReader(
                     new FileInputStream(inputFileLine)));
                // Do we have a BufferedReader to which "br" is pointing?
                System.out.println("br = " + br);
                while((inputFileLine = br.readLine()) != null) {
                    System.out.println(inputFileLine);
                }
                br.close();
            } catch(IOException e) {
                System.out.println("Read error: " + e.getMessage());
            }
        }
    }
    Last edited by hardwired; 11-24-2007 at 06:23 PM. Reason: remove tabs

  5. #5
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    @hardwired

    a)why we say
    Java Code:
     BufferedReader br=null;
    before the try? and generally why we (need to) write it?

    b)you typed
    Java Code:
    br = new BufferedReader(
                     new InputStreamReader(
                     new FileInputStream(inputFileLine)));
    why you didnt type
    Java Code:
    BufferedReader br=new BufferedReader(new FileReader (inputFileLine));
    ?

  6. #6
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    If you read the API docs, you would see that BufferedReader took a Reader in its constructor. InputStreamReader is a subclass of Reader and FileReader is a subclass of InputStreamReader. That means that Reader is a superclass for both InputStreamReader and FileReader.

    Simply put, it doesn't matter which one he puts in there, but in InputStreamReader he has to specifically open an InputStream to the file, while with FileReader, the file is already opened.
    Last edited by staykovmarin; 11-24-2007 at 07:56 PM.

Similar Threads

  1. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM
  2. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM
  3. nullPointerException problem
    By conandor in forum Networking
    Replies: 1
    Last Post: 08-14-2007, 01:22 PM
  4. NullPointerException problem
    By warship in forum AWT / Swing
    Replies: 5
    Last Post: 08-10-2007, 04:43 PM
  5. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •