Results 1 to 9 of 9
- 12-10-2011, 04:15 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
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:
The main method: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()); } }
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.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); } } }
Thanks
-
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?
- 12-10-2011, 04:54 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
-
Re: Help with homework - nullPointerException
The exception will tell you the line numbers of the involved code.
-
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.
- 12-10-2011, 05:08 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
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).
- 12-10-2011, 05:13 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
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.
-
Re: Help with homework - nullPointerException
You're calling the method countingWords() within the method countingWords(), and so it will repeat forever or until it runs out of memory:
Also, shouldn't your openRead use the file name passed into the class's constructor, not some hard-coded String?Java Code:public boolean countWords() { if (openRead()) { String lineIn = sc.toString(); StringTokenizer in = new StringTokenizer(lineIn, "\n\t\r"); } sc.close(); return countWords(); // ****** here! }
- 12-10-2011, 05:19 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
Homework help please
By chick in forum New To JavaReplies: 22Last Post: 03-19-2010, 07:39 AM -
Homework help
By rclausing in forum New To JavaReplies: 26Last Post: 11-24-2009, 06:06 AM -
Please Help with Homework
By theuser in forum Advanced JavaReplies: 2Last Post: 07-30-2009, 03:37 PM -
Need help with homework.
By JavaNewbie0000 in forum New To JavaReplies: 2Last Post: 07-31-2008, 03:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks