Results 1 to 16 of 16
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default NullPointerException

    Java Code:
    public class ReadFile {
        private Scanner read;
    
         private void openFile() {
           try {
               read = new Scanner(new File("Input.txt"));
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
       }
      
       private void readFile() {
           while(read.hasNext()) {
                String data = read.next();
                System.out.printf("%s/n", data);
           }
       }
       
       private void closeFile() {
           read.close();
       }
       
        public static void main(String[] args) {
            ReadFile file = new ReadFile();
            file.openFile();
            file.readFile();
            file.closeFile();
        }
    }
    I am getting a NullPointerException at lines 14 and 27. The file itself is a txt file in notepad and there are just single integers on each line:
    13
    105
    112
    ..
    ..
    ..
    Last edited by jwl; 10-26-2012 at 02:14 AM.

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: NullPointerException

    You need a constructor for your ReadFile class that initializes your Scanner....

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    Why?

  4. #4
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by SJF View Post
    You need a constructor for your ReadFile class that initializes your Scanner....
    I changed it around and I still get the same error.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: NullPointerException

    openFile() ought to initialise the scanner.

    It might be good idea to System.out.println() the value of read in that method to check that it isn't null. (I'd have thought it would throw a FileNotFoundException if it couldn't find the file, but I can't check that from here.)

  6. #6
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by pbrockway2 View Post
    openFile() ought to initialise the scanner.

    It might be good idea to System.out.println() the value of read in that method to check that it isn't null. (I'd have thought it would throw a FileNotFoundException if it couldn't find the file, but I can't check that from here.)

    Yes, it is null. Why is this?

  7. #7
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by jwl View Post
    Yes, it is null. Why is this?
    I forgot to put txt after the file name. I also moved the file into the ReadFile directory(not in src) and that seems to help but now this is what I'm getting in the console.

    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
    13/n105/n112/n105/n111/n112/n113/n107/n118/n108/n110/n109/n112/n108/nBUILD SUCCESSFUL (total time: 0 seconds)

    ?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: NullPointerException

    Ok, "can't" was an exageration, I've checked and new Scanner(File) throws an exception if anything goes wrong.

    The code you posted is your real code, right? I ask because people sometimes shortcut, an if you really say "Scanner read = new Scanner(etc" that makesa difference.

    Next check that you aren't actually getting the error message about not being able to open the file. Because the scanner will be null in that case.

    Third print a message in openFile() at the end to confirm you've opened the file. A good message might print the full path+name of thefile. Or (temporarily, perhaps) remove the try/catch and let your program crash and burn if the file isn't found.

  9. #9
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    The code you posted is your real code, right?
    Yes, I typed all of it. I got the concepts off of YouTube and Java Concepts 6th Edition.
    Last edited by jwl; 10-26-2012 at 03:01 AM.

  10. #10
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: NullPointerException

    OK, sorry, I missed the openFile call initially....


    you may want something like:

    Java Code:
         private boolean openFile() {
            boolean retVal = false;
            try {
                 read = new Scanner(new File("Input.txt"));
                 retVal=true;
            }
            catch (IOException e) {
                 System.out.println("Could not open file input");
            }
            return retVal;   
        }
           
        public static void main(String[] args) {
            ReadFile file = new ReadFile();
            if(file.openFile()){
               file.readFile();
               file.closeFile();
            }
        }
    if openFile is throwing NPE something else is happening....

    You can try (for testing purposes):
    Java Code:
        private boolean openFile() {
            boolean retVal = false;
            try {
                 read = new Scanner(new File("Input.txt"));
                 retVal=true;
            }
            catch (IOException e) {
                 System.out.println("Could not open file input");
            }
            catch (Exception e){
                  e.printStackTrace();
            }
            return retVal;   
        }
    Catching "Exception" is not usually a good thing, something really wrong may have happened and you can inadvertantly ignore it by doing this, but for debugging....
    Last edited by SJF; 10-26-2012 at 03:09 AM.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: NullPointerException

    Sorry for the slow posts, my glass is empty so there was even more delay with tbis...

    Your output is good - it's just that you have /n where you mean \n.

  12. #12
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by jwl View Post
    I forgot to put txt after the file name. I also moved the file into the ReadFile directory(not in src) and that seems to help but now this is what I'm getting in the console.

    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
    13/n105/n112/n105/n111/n112/n113/n107/n118/n108/n110/n109/n112/n108/nBUILD SUCCESSFUL (total time: 0 seconds)

    ?
    I had a println statement in the openfile method that caused the above output.

    Java Code:
    private void openFile() {
           try {
               read = new Scanner(new File("BarChart.txt"));
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
           System.out.println(read);
       }
    line 8.

  13. #13
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    Success!

  14. #14
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by pbrockway2 View Post
    Sorry for the slow posts, my glass is empty so there was even more delay with tbis...

    Your output is good - it's just that you have /n where you mean \n.
    Thanks

  15. #15
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: NullPointerException

    Quote Originally Posted by jwl View Post
    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
    This is caused by:
    Java Code:
        Scanner read = new Scanner(new File("SomeFile.txt"));
        System.out.println(read);
    It's not an error per se, it's Scanner's .toString() method!

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: NullPointerException

    Excellent!

    Youtube is ok, but three heads - and discussion - is better. I really don't know how us grey beards managed...

Similar Threads

  1. GUI and NullPointerException
    By Humphrey Bogart in forum New To Java
    Replies: 12
    Last Post: 10-12-2012, 07:47 AM
  2. NullPointerException
    By rokit boy in forum New To Java
    Replies: 8
    Last Post: 03-18-2012, 08:10 AM
  3. NullPointerException
    By jayragz in forum NetBeans
    Replies: 5
    Last Post: 05-12-2011, 06:19 PM
  4. NullPointerException
    By GPB in forum New To Java
    Replies: 8
    Last Post: 02-21-2010, 04:05 PM
  5. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 06:43 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
  •