Results 1 to 4 of 4
- 01-01-2009, 03:56 PM #1
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
[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:
But i get a nullpointerexception at line 14 That is where the for statement starts.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"); } }
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 keffie91Never give up! ;)
- 01-01-2009, 04:51 PM #2
exception == null
When you get a NPE, it means that your trying to use a variable/object that is null.
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.Java Code:for(int i = 0; i < a.length(); i++)
Solution: change the comparation/check for nulll to directly AFTER the read line assigment.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-01-2009, 05:04 PM #3
- Join Date
- Jun 2008
- Location
- The Netherlands
- Posts
- 35
- Blog Entries
- 1
- Rep Power
- 0
Thanks man, I understand.
keffie91Never give up! ;)
- 01-01-2009, 05:09 PM #4
Similar Threads
-
I get a NullPointerException and don't know why
By hendrix79 in forum New To JavaReplies: 9Last Post: 12-14-2008, 06:18 AM -
NullPointerException
By Aika in forum New To JavaReplies: 8Last Post: 11-18-2008, 11:34 PM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 04:20 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks