I have perused numerous other solutions to NPEs, and I've tried to implement other suggestions, but none of them quite match up what I'm trying to do and it just leads to more eclipse errors. I have compiled and tried to run from the command line, giving the application I'm running a couple strings when running at the command line. Below is the main class, and the class containing the methods that the main is using.
Class with the main method:
package my.package.ext;
public class WordCounterApp {
/**
* @param args
* Two command line arguments: the first one needs to be in quotes, the string that will be used, the second optional argument
* is the unique word to be counted (countWord method).
* @param source
* @param word
*/
public static void main(String[] args) {
String source = null;
String uniqueword = null;
StringBuilder word = null;
WordCounter counter = new WordCounter(source, word);
WordCounter uniqueCounter = new WordCounter(source, uniqueword);
counter.countWords(source);
counter.countUniqueWords(source);
uniqueCounter.countWord(source, uniqueword);
}
}
Class with the other methods:
package my.package.ext;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Character;
import java.lang.StringBuilder;
public class WordCounter {
public Integer counter = 0;
public String source;
public HashSet<String> hashset;
public StringBuilder word;
public String uniqueword;
public WordCounter(String source) {
counter = new Integer(counter);
}
public WordCounter(String source, StringBuilder word) {
counter = new Integer(counter);
}
public WordCounter(String source, String uniqueword) {
counter = new Integer(counter);
}
/**
*
* @param line - the string parameter to get a total word count from.
*/
public int countWords(String source) {
boolean word = false;
int endOfLine = source.length() - 1;
Integer counter = 0;
for (int i = 0; i < source.length(); i++) {
if (Character.isLetter(source.charAt(i)) == true && i != endOfLine) {
word = true;
//} else if (Character.charValue(line.charAt(i)) == "-" && i != endOfLine) {
// word = true;
} else if (Character.isLetter(source.charAt(i)) == false && word == true) {
counter++;
word = false;
} else if (Character.isLetter(source.charAt(i)) && i == endOfLine) {
counter++;
}
}
System.out.println(counter);
return counter;
}
/**
*
* @param line - the string parameter that we will return the unique word count from. Randy recommends a HashSet.
* Put it into a hashset. Hashsets don't allow duplicate elements. Then do a count.
*/
public int countUniqueWords(String line) {
hashset = new HashSet<String>();
word = new StringBuilder();
int endOfLine = line.length() - 1;
boolean isWord = false;
String stringWord = null;
Integer counter = 0;
for (int i = 0; i < line.length(); i++) {
if (Character.isLetter(line.charAt(i)) == true && i != endOfLine) {
//System.out.println(i);
word.append(line.charAt(i));
isWord = true;
} else if (Character.isLetter(line.charAt(i)) == false && isWord == true) {
counter++;
//System.out.println("Counter is: " + counter);
stringWord = word.toString();
//System.out.println("stringWord is now: " + stringWord);
hashset.add(stringWord);
//System.out.println(hashset);
word = new StringBuilder();
isWord = false;
} else if (Character.isLetter(line.charAt(i)) && i == endOfLine) {
counter++;
stringWord = word.toString();
hashset.add(stringWord);
}
}
//System.out.println(counter);
System.out.println("There are " + hashset.size() + " unique words in this string");
System.out.println("These are the unique words in the string: " + hashset);
return counter;
}
/**
*
* @param source - the string the word is to be counted from
* @param word - the word to be counted
*
*/
public void countWord(String source, String word) {
String str = source;
Pattern p = Pattern.compile("\\s"+word+"\\s");
Matcher m = p.matcher(str);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("The word: " + "\"" + word + "\"" + " appears " + count + " times.");
}
}
I have identified the source of the NPE here:
public int countWords(String source) {
boolean word = false;
int endOfLine = source.length() - 1; //the source of the NPE is this line
Integer counter = 0;
So looking at that, I figure I'm not initializing source correctly. I have tried things like WordCounter source = new WordCounter()
But every variant of this I try, bringing in the correct constructor, gives me other eclipse errors. I can't seem to get there, and I'm afraid I'm going down the wrong path. I probably have other screw ups in here as well. I'm also unsure of the correct way to run from the command line while passing in some strings as arguments to feed the methods. Thanks in advance