Results 1 to 4 of 4
  1. #1
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default [SOLVED] NullPointerException - FileInfo Program

    Hello I am writing a Program that gives information about what kind of characters a file contains. The file is read by console.

    This is the code:
    Java Code:
    import java.io.*;
    public class FileInfo {
        public static void main(String[] args) throws IOException, FileNotFoundException {
            BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("This program gives info about a file.");
            System.out.print("What is the filename?"); System.out.flush();
            String name = myIn.readLine();
            BufferedReader bufIn = new BufferedReader(new FileReader(name));
            char ch;
            int Letter = 0;
            int Digit = 0;
            while(true){
                String a = bufIn.readLine();
            for(int i = 0; i < a.length(); i++){
                 ch = a.charAt(i);
                if(Character.isLetter(ch) == true){
                    Letter++;
                }
                else if(Character.isDigit(ch) == true){
                    Digit++;
                }
                 
            }
              if(a == null)
                   break;  
            }
            System.out.println("This file contains: " + Letter + " Letters");
            System.out.println("This file contains: " + Digit + " Digits");
        }
    
    }
    But i get a nullpointerexception at line 14 That is where the for statement starts.

    I don't know what the exception means in this case and how to solve it.
    Can someone explain to me what the exception means in this case and some tips about how to solve it.

    Thanks keffie91
    Never give up! ;)

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default exception == null

    When you get a NPE, it means that your trying to use a variable/object that is null.
    Java Code:
    for(int i = 0; i < a.length(); i++)
    In the above code, the ONLY thing that could be null is "a". What is happening is your trying to read the next line when there is none, "a" becomes null and then you try to use "a" in the for statement which upchucks the NPE.

    Solution: change the comparation/check for nulll to directly AFTER the read line assigment.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    Thanks man, I understand.

    keffie91
    Never give up! ;)

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Welcome

    Glad to help,

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. I get a NullPointerException and don't know why
    By hendrix79 in forum New To Java
    Replies: 9
    Last Post: 12-14-2008, 06:18 AM
  2. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-18-2008, 11:34 PM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM
  4. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 07:51 PM

Posting Permissions

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