Results 1 to 7 of 7

Thread: reading a file

  1. #1
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default reading a file ending wid newline char

    Hi,

    I am trying to read a file which may or may not end with a newline character.
    I am able to read it if it does not end with a newline character, but if it does, it gives me nullpointerexception. here is my code:

    Java Code:
    String i;
            int k =0;
            
            FileReader fr = null;
            BufferedReader br = null;
            try {
                fr = new FileReader(args[0]);
                br = new BufferedReader(fr);
            } catch (FileNotFoundException ex) {
                System.out.println("File Not Found!");
            }
            do
            {
                i = br.readLine().trim();
                if (i.trim().length() != 0)
                {
                    k = Integer.parseInt(i);
                 }
            }while((i = br.readLine()) != null);
    Last edited by new_coder; 08-18-2009 at 04:50 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    This is wrong, you check for null values after read the line.

    Java Code:
            do
            {
                i = br.readLine().trim();
                if (i.trim().length() != 0)
                {
                    k = Integer.parseInt(i);
                 }
            }while((i = br.readLine()) != null);
    Change it to this and try.

    Java Code:
            while((i = br.readLine()) != null)
            {
                if (i.trim().length() != 0)
                {
                    k = Integer.parseInt(i);
                 }
            }

  3. #3
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    i tried that too, i am also getting the output as well as the exception with it.

    what i am trying to do is read a file that contains only one integer. but the file may end with '\n' or may not. so if there is a newline character after the integer, its throwing an exception. if its before, its not. i dunno y

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.NullPointerException
    at hoppity.main(hoppity.java:36)

    line 36 is

    i = br.readLine().trim();

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The problem is that br.readLine() returns null at EOF, so calling trim() on it will throw a NullPointerException. Don't trim the string until after the check for null.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by new_coder View Post
    Exception in thread "main" java.lang.NullPointerException
    at hoppity.main(hoppity.java:36)

    line 36 is

    i = br.readLine().trim();
    In my code segment there is nothing like this? Did you try that exactly as it is?

    The reason here is you get a null value for the read line, but trim() calls on that. Obviously it's not correct.

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  2. Reading XML from file
    By chriscant in forum New To Java
    Replies: 0
    Last Post: 03-17-2009, 01:34 PM
  3. Reading from file
    By kiab3000 in forum New To Java
    Replies: 0
    Last Post: 03-14-2009, 06:33 PM
  4. Help for reading a doc file
    By thehero in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 06:44 AM
  5. Reading from csv file.plz help!!!!!!!!!!
    By sanjayksaini in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 07:41 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
  •