Results 1 to 9 of 9
  1. #1
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Help with homework - nullPointerException

    Hello again folks

    I have a homework assignment that I'm having some trouble with and hoping the nice folks here can help point out where I'm going wrong. It's the usual nullPointerException and I undderstand that I seem to be trying to envoke code on a null value, but can't see where I'm going wrong. Code below:

    Java Code:
    /**
     * Title: WordCounter class skeleton
     * Description: Complete this class as described 
     * @author M257 Module Team
     */
    
    package tma01q2;
    import java.io.*;
    import java.io.File;
    import java.util.*;
    import java.util.StringTokenizer;
    public class WordCounter
    {  
        //TODO add the required instance variables
        public String fileName;
        public Scanner sc;
        public int totalWords;
        public int totalEven;
        public int totalOdd;
        public int totalLetters;
    
        public void WordCounter(String aFile)throws IOException
                 //TODO add the required constructor
        {
            fileName = "";
            aFile = fileName;
    
        }
    
        //TODO add the openRead method
        public boolean openRead()
        {
            boolean yn = false;
            try
            {
                Scanner sc = new Scanner(new File("haiku.txt"));
                yn = true;
            }
            catch (IOException ex)
            {
                    System.out.println("problem with file " + ex);
            }
            return yn;
        }
               
        //TODO add the countWords method
        public boolean countWords()
        {
           if (openRead())
            {    
                  String lineIn = sc.toString();
                  StringTokenizer in = new StringTokenizer(lineIn, "\n\t\r");
            }
           sc.close();
           return countWords();
        }    
        //TODO add the updateStatistics method
        public void updateStastics(String word)
        {
           word = sc.next();
           int len = word.length();
           int bol = 2;
           totalWords++;
           totalLetters = totalLetters + len;
           if(len % bol > 0)
            {
               totalOdd++;
            }
           else
             {
               totalEven++;
             }
            System.out.println(word + " [" + word.length() + "]");
           }
        //TODO add the toString method
        @Override
        public String toString()
        {
          String tWords = new Integer(totalWords).toString();
          String tEven = new Integer(totalEven).toString();
          String tOdd = new Integer(totalOdd).toString();
          String tLetters = new Integer(totalLetters).toString();
          return fileName + tWords + tEven + tOdd + tLetters;
        }
    
        //TODO add the saveResults method
        public void saveResults (String outputFile) throws FileNotFoundException
        {
            PrintWriter results = new PrintWriter("C:\\Users\\jazzermonty\\uni\\M275\\TMA01\\TMA01Download\\TMA01Q2\\results.txt");
            results.println(toString());
    
        }
    }
    The main method:

    Java Code:
    /*
     * TestWordCounter.java skeleton class
     * Complete this class according to the question instructions
     */
    
    package tma01q2;
    
    
    public class TestWordCounter
    {
       //TODO add code to create a WordCounter for the haiku.txt file   
       //to gather statistics about the words in the file    
       //and to save the results
         public static void main(String[] args)
        {
             try
             {
             WordCounter j = new WordCounter();
             j.countWords();
             j.saveResults("results.txt");
             }
             catch (Exception exp)
             {
                 System.out.println("Problem with file " + exp);
             }
        }
         
    }
    I think the problem is in the openRead() method in the WordCounter class but not sure where the problem lies. Any help would be very much appreciated.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with homework - nullPointerException

    The problem is usually evident if you know which object is null. Can you tell us which line throws the exception?

  3. #3
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Re: Help with homework - nullPointerException

    Quote Originally Posted by Fubarable View Post
    The problem is usually evident if you know which object is null. Can you tell us which line throws the exception?
    Hi Fubarable

    Thanks for replying. How do I tell which line is the issue? I'm using netbeans 6.9.1. All the code is doing is throwing the exception in the main method.

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with homework - nullPointerException

    The exception will tell you the line numbers of the involved code.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with homework - nullPointerException

    Ah, you're shadowing a variable. Since you re-declare the sc variable in the openRead() method, the sc variable in that method is local to that method and only exists within the method. Once the method exits, that Scanner object is no longer visible or usable by the rest of the program.

    The solution is not to re-declare sc in openRead. Instead use the sc variable that is declared in the class and visible throughout the class.

  6. #6
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Re: Help with homework - nullPointerException

    Thanks for that

    Now getting a Exception in thread "main" java.lang.StackOverflowError. The error is on line at tma01q2.WordCounter.openRead(WordCounter.java:36).

  7. #7
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Re: Help with homework - nullPointerException

    I seem to have managed to create a continious loop between the openRead() and the countWords() methods. Not sure how I managed that.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with homework - nullPointerException

    Quote Originally Posted by elliotHenry View Post
    I seem to have managed to create a continious loop between the openRead() and the countWords() methods. Not sure how I managed that.
    You're calling the method countingWords() within the method countingWords(), and so it will repeat forever or until it runs out of memory:

    Java Code:
        public boolean countWords()
        {
           if (openRead())
            {    
                  String lineIn = sc.toString();
                  StringTokenizer in = new StringTokenizer(lineIn, "\n\t\r");
            }
           sc.close();
           return countWords(); // ****** here!
        }
    Also, shouldn't your openRead use the file name passed into the class's constructor, not some hard-coded String?

  9. #9
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Re: Help with homework - nullPointerException

    Ok, so I think I've got to the bottom of the problem, it seems that I was returning countWords() within the method countWords(). Not clever.

Similar Threads

  1. Homework help please
    By chick in forum New To Java
    Replies: 22
    Last Post: 03-19-2010, 07:39 AM
  2. Homework help
    By rclausing in forum New To Java
    Replies: 26
    Last Post: 11-24-2009, 06:06 AM
  3. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 03:37 PM
  4. Need help with homework.
    By JavaNewbie0000 in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 03:06 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
  •