Results 1 to 16 of 16
Thread: NullPointerException
- 10-26-2012, 01:07 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
NullPointerException
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: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(); } }
13
105
112
..
..
..Last edited by jwl; 10-26-2012 at 01:14 AM.
- 10-26-2012, 01:15 AM #2
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: NullPointerException
You need a constructor for your ReadFile class that initializes your Scanner....
- 10-26-2012, 01:19 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: NullPointerException
Why?
- 10-26-2012, 01:21 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
- 10-26-2012, 01:24 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.)
- 10-26-2012, 01:31 AM #6
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
- 10-26-2012, 01:46 AM #7
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: NullPointerException
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)
?
- 10-26-2012, 01:49 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 10-26-2012, 01:56 AM #9
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: NullPointerException
Yes, I typed all of it. I got the concepts off of YouTube and Java Concepts 6th Edition.The code you posted is your real code, right?Last edited by jwl; 10-26-2012 at 02:01 AM.
- 10-26-2012, 02:04 AM #10
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: NullPointerException
OK, sorry, I missed the openFile call initially....
you may want something like:
if openFile is throwing NPE something else is happening....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(); } }
You can try (for testing purposes):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....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; }Last edited by SJF; 10-26-2012 at 02:09 AM.
- 10-26-2012, 02:06 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 10-26-2012, 02:08 AM #12
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: NullPointerException
I had a println statement in the openfile method that caused the above output.
line 8.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); }
- 10-26-2012, 02:10 AM #13
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Re: NullPointerException
Success!
- 10-26-2012, 02:10 AM #14
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
- 10-26-2012, 02:12 AM #15
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
- 10-26-2012, 02:16 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Similar Threads
-
GUI and NullPointerException
By Humphrey Bogart in forum New To JavaReplies: 12Last Post: 10-12-2012, 06:47 AM -
NullPointerException
By rokit boy in forum New To JavaReplies: 8Last Post: 03-18-2012, 07:10 AM -
NullPointerException
By jayragz in forum NetBeansReplies: 5Last Post: 05-12-2011, 05:19 PM -
NullPointerException
By GPB in forum New To JavaReplies: 8Last Post: 02-21-2010, 03:05 PM -
NullPointerException
By Juuno in forum New To JavaReplies: 1Last Post: 02-11-2010, 05:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks