Results 1 to 15 of 15
  1. #1
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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);
    	                   }
    	             
                }
    	        
    	}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  3. #3
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer

    here is my fix,
    Java Code:
     while ((content = in.readLine())!= null)    
    	            {
    	            count++;
               String [] propertylist = content.split("[\\s}]");
    but now i get Exception in thread "main" java.lang.NumberFormatException: empty String
    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);
    	                   }
    	             
                }
    	        }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  5. #5
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  7. #7
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  9. #9
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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]);
    	               }
    	            }

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  11. #11
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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...?

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer

    Quote Originally Posted by toamille View Post
    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
    Okay, so that's content. What's propertylist? What's line?

    Quote Originally Posted by toamille View Post
    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...?
    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!

  13. #13
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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();
    	          }
    	             
                }

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: trying to debug and keep getting Exception in thread "main" java.lang.NullPointer

    Quote Originally Posted by toamille View Post
    Found the mistake myself.
    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!

  15. #15
    toamille is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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

  1. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  2. Replies: 14
    Last Post: 04-08-2011, 07:01 PM
  3. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  4. Replies: 3
    Last Post: 06-01-2010, 04:01 PM
  5. Replies: 1
    Last Post: 11-20-2009, 08:58 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
  •