Results 1 to 15 of 15
- 04-18-2012, 06:41 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
trying to debug and keep getting Exception in thread "main" java.lang.NullPointerExce
For this class project, i have to read a listings.txt file and out put to overviewfile.txt. at line 23 i get the message Exception in thread "main" java.lang.NullPointerException
at ket.task2.b.KetTask2B.main
Java Code:public static void main(String[] args) throws FileNotFoundException, IOException { // calls for the input file names Scanner console = new Scanner(System.in); System.out.print("Type in the name of the file - listings.txt -: "); String inputFile = console.next(); // writes to the new file overview.txt File input = new File(inputFile); PrintWriter out = new PrintWriter("overview.txt"); BufferedReader in = new BufferedReader(new FileReader(input)); int count = 0; double sum = 0; // put the sum and count for the properities String content = null; count++; String [] propertylist = content.split("[\\s}]"); Double propertyvalue = Double.parseDouble(propertylist [2]); for (int i=0; i < propertylist.length; i++) sum += propertyvalue; System.out.println("Total properties listed: " + count); System.out.println("Total value of properties listed: " + sum); in.close(); try { ArrayList<String> propids = new ArrayList<String>(); Scanner idfile = new Scanner((input)); //print property ids while (idfile.hasNextLine()) { content = idfile.nextLine(); String[] fields = content.split("[\\s}]"); String propertyids = (fields [0]); for (int j = 0; j < fields.length; j++) { propids.add(propertyids); System.out.println(propertyids); in.close(); } } //close Print Writer out.flush(); out.close(); }catch(Exception e){ System.out.println("There was a problem:" + e); } } }
- 04-18-2012, 06:48 PM #2
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
Look at line 20. Then look at line 23. See the problem? You can't dereference a null value.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-18-2012, 07:16 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
here is my fix,
but now i get Exception in thread "main" java.lang.NumberFormatException: empty StringJava Code:while ((content = in.readLine())!= null) { count++; String [] propertylist = content.split("[\\s}]");
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1011)
at java.lang.Double.parseDouble(
at ket.task2.b.KetTask2B.main
here is new improved code
Java Code:// calls for the input file names Scanner console = new Scanner(System.in); System.out.print("Type in the name of the file - listings.txt -: "); String inputFile = console.next(); // writes to the new file overview.txt File input = new File(inputFile); PrintWriter out = new PrintWriter("overview.txt"); BufferedReader in = new BufferedReader(new FileReader(input)); int count = 0; double sum = 0; // put the sum and count for the properities while ((content = in.readLine())!= null) { count++; String [] propertylist = content.split("[\\s}]"); Double propertyvalue = Double.parseDouble(propertylist [2]); for (int i=0; i < propertylist.length; i++) sum += propertyvalue; } System.out.println("Total properties listed: " + count); System.out.println("Total value of properties listed: " + sum); in.close(); try { ArrayList<String> propids = new ArrayList<String>(); Scanner idfile = new Scanner((input)); //print property ids while (idfile.hasNextLine()) { content = idfile.nextLine(); String[] fields = content.split("[\\s}]"); String propertyids = (fields [0]); for (int j = 0; j < fields.length; j++) { propids.add(propertyids); System.out.println(propertyids); in.close(); } } //close Print Writer out.flush(); out.close(); }catch(Exception e){ System.out.println("There was a problem:" + e); } } }
- 04-18-2012, 07:19 PM #4
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
What String are you trying to read in? What are you trying to do with it?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-19-2012, 01:32 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
the .txt document has a list of properties and a list of values. I need to be able to count the property and sum the values. this portion of code should add up the total property value and out.println() im trying to split what is counted and whats is a sum. Im not sure if that answers the question or not?
- 04-19-2012, 01:56 PM #6
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
Not really. Print the line out as soon as you read it in, before you do anything with it. What is that value? What are you trying to do with that value that causes the error?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-19-2012, 02:41 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
Example listings.txt file:
110001 commercial 500000.00 101
110223 residential 100000.00 101
110020 commercial 1000000.00 107
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110
this is what i want it to look like ##
Total properties listed: 7
Total value of properties listed: 2120000.00
110001
110020
110223
110333
110421
110442
112352
this is the print out.
110001
110001
110001
110001
110001
110020
110020
110020
110020
110020
110223
110223
110223
110333
110421
110421
110421
110442
112352
- 04-19-2012, 02:45 PM #8
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
What I'm trying to get at is what value is causing the error. The error says you're passing an empty String to the parseDouble() method. Why is that? You can figure that out by running your program with a debugger and stepping through it, or by adding strategic print statements to figure out where that empty String is coming from. Does your program read some of the file, and fail in the middle? Does it not read anything in correctly? Does it only fail at the end? These are the kinds of questions you should be asking yourself.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-19-2012, 04:55 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
so i changed some things around, and now i'm stuck back on java.lang.NullPointerException at the split. when i add in a try catch. i just get the list of property ids. (see above) so i know its reading the file. how do i get past the null pointer?
Java Code:while ((content = in.readLine())!= null) { count++; String [] propertylist = line.split("[\\s}]"); { sum += Double.parseDouble(propertylist [2]); } }
- 04-19-2012, 05:33 PM #10
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
You have to use a debugger or some print statements to figure out what's going on. For example, what is content each time? What is line? What is propertylist?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-20-2012, 12:09 AM #11
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
I added System.out.println(content); right before the error. I get as output
Type in the name of the file - listings.txt -: listings.txt
110001 commercial 500000.00 101
Exception in thread "main" java.lang.NullPointerException
So how do i get it to read the whole file? that is just the first line. I believe i want "content" to be the whole file...?
- 04-20-2012, 12:19 AM #12
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
Okay, so that's content. What's propertylist? What's line?
Your program is reading in a single line, and then encountering an error, which prevents the program from continuing to read the file. Your job is to figure out what's causing that error, and you'll do that by finding out the answers to the questions I've been asking.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-20-2012, 01:29 AM #13
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
working good now!!! Found the mistake myself. But some where i messed up and it will output file "overview.txt," but doesn't write anything to it. looking for a proof read to get the text into the text file....?
Java Code:{ // calls for the input file names Scanner console = new Scanner(System.in); System.out.print("Type in the name of the file - listings.txt -: "); String inputFile = console.next(); // writes to the new file overview.txt PrintWriter out = new PrintWriter("overview.txt"); File input = new File(inputFile); BufferedReader in = new BufferedReader(new FileReader(input)); String content = null; int count = 0; double sum = 0; try { // put the sum and count for the properities while ((content = in.readLine())!= null) { count++; //System.out.println(content); String [] propertylist = content.split("[\\s}]"); //System.out.println(content) { sum += Double.parseDouble(propertylist [0]); } } //this is the output lines for count and sum System.out.println("Total properties listed: " + count); System.out.println("Total value of properties listed: " + sum); //in.close(); }catch(IOException | NumberFormatException e){ System.out.println("There was a problem:" + e); } ArrayList<String> propids = new ArrayList<String>(); Scanner idfile = new Scanner((input)); //print property ids while (idfile.hasNextLine()) { content = idfile.nextLine(); String[] fields = content.split("[\\s}]"); String propertyids = (fields [0]); { propids.add(propertyids); System.out.println(propertyids); //in.close(); } } //close Print Writer out.flush(); out.close(); } }
- 04-20-2012, 11:42 PM #14
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
Guess it had nothing to do with me asking questions pointing you directly at the problem. I'll be on my way now.
I've given you a process of asking questions about the error. You need to follow that. All you're doing is asking other people to debug your code for you, when you should be trying to answer questions like the ones I asked you before. Where do you think it should write to the file? Is that executing? Why or why not?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-22-2012, 04:28 PM #15
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer
Found it, lessons learned. If you copy paste from the top to the bottom, the mistakes just repeats. Forgot to delete the system. from system.out.println ()
Java Code:out.println(propertyids); //in.close(); } } System.out.print("The Overview file is ready to read: "); //closes Print Writer
Similar Threads
-
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 06:01 PM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/star/lang/XEventLi
By baktha.thalapathy in forum Advanced JavaReplies: 3Last Post: 06-01-2010, 03:01 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks