Results 1 to 4 of 4
- 11-08-2010, 04:11 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Help! Program cannot run. Error is "java.lang.NullPointerException".
I want to output the frequency of the words in a sentence. The program can compile, but cannot run. The error is "java.lang.NullPointerException". Thank you very much.
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Enumeration;
public class Main{
public void main(String[] args)
{
WordCount wc = new WordCount();
wc.processLine("da do ro ro ro, da do ro ro");
wc.print ();
}
}
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Enumeration;
public class WordCount
{
Hashtable ht;
public WordCount ()
{
ht = new Hashtable ();
}
public void processLine (String s)
{
StringTokenizer st = new StringTokenizer (s, " ,.");
while (st.hasMoreTokens()) {
String word = st.nextToken();
processWord (word.toLowerCase ());
}
}
public void processWord (String word)
{
if (ht.containsKey (word)) {
Integer i = (Integer) ht.get (word);
Integer j = new Integer (i.intValue() + 1);
ht.put(word, j);
}
else {
ht.put(word, new Integer (1));
}
}
public void print ()
{
Enumeration enum1 = ht.keys ();
while (enum1.hasMoreElements ()) {
String key = (String) enum1.nextElement ();
Integer value = (Integer) ht.get (key);
System.out.println ("{ " + key + ", " + value + " }");
}
}
}
- 11-08-2010, 04:13 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
I don't see any NPE when I run it, although I have a hard time running it since the main method is not static. ;)
Which line causes the problem?
- 11-08-2010, 04:20 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Thank you for replying. After i add " static ", it works. Thx.
- 11-08-2010, 04:23 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
Similar Threads
-
ERROR: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By PFworth in forum New To JavaReplies: 3Last Post: 04-30-2010, 08:44 PM -
compiler error:"AWT-EventQueue-0" java.lang.NullPointerException at java.io.Reader.
By Ms.Ranjan in forum New To JavaReplies: 4Last Post: 04-23-2010, 02:54 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 08:58 PM -
help debugging: exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By freqrush in forum AWT / SwingReplies: 5Last Post: 08-26-2009, 12:37 PM -
Definitely new to Java "Main" Java.lang NullPointerException Error
By lilyumestar in forum New To JavaReplies: 4Last Post: 10-09-2008, 07:08 PM
Bookmarks