I have been given an assignment to create the following method "public String getTextDefinition(String text), which have to be implemented as follows (from the assignment):
- Use the text parameter to instantiate a Word object.
- Get the text value of the instantiated Word object with its getText() method.
- Check if the text value of the instantiated Word object is contained in the keyset of the MasterDictorinary.
- If this text value is present in the keySet of the MasterDictionary, get its corresponding value of the type Word in the MasterDictorinary and assign the value of its toString() method to the definition variable.
- If this text value is not present in the keySet of the MasterDictionary, assign the toString() method of instantiated Word object of the original text parameter to the definition variabel.
I don't know if I have implemented the method in the correct way, but this is how I have implemented the getTextDefinition(String text) method:
public String getTextDefinition(String text){
String definition = "";
Word word = new Word(text);
word.getText();
if(this.containsValue(text) == true){
this.get(word);
definition = word.toString();
}
if(this.containsValue(text) == false){
definition = get(text).toString();
}
return definition;
}
But I can't get it to print the right output which is to look like this:
half:
Word Definition:
In an equal part or degree;
half nelson:
Word Definition:
A hold in which one arm is thrust under the corresponding armof the opponent.
half seas over:
Word Definition:
Half drunk. [Slang: used only predicatively.] Spectator.
half taste:
Word Definition:
No definition found.
The output that i get looks like this:
half:
Word Definition:
In an equal part or degree;
half nelson:
Word Definition:
A hold in which one arm is thrust under the corresponding armof the opponent.
half seas over:
Word Definition:
Half drunk. [Slang: used only predicatively.] Spectator.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "dictionaryModels.Word.toString()" because the return value of "dictionaryModels.MasterDictionary.get(Object)" is null
at dictionaryModels.MasterDictionary.getTextDefinition(MasterDictionary.java:27)
at dictionaryModels.MasterDictionary.main(MasterDictionary.java:47)
This is how the MasterDictionary class looks like:
public class MasterDictionary extends TreeMap<String, Word>{
public MasterDictionary(){
super();
}
public void put(Word word){
super.put(word.getText(),word);
}
public String getTextDefinition(String text){
String definition = "";
Word word = new Word(text);
word.getText();
if(this.containsValue(text) == true){
this.get(word);
definition = word.toString();
}
if(this.containsValue(text) == false){
definition = get(text).toString();
}
return definition;
}
public static void main(String[] args) {
MasterDictionary dictionary = new MasterDictionary();
Word word = new Word("HALF", "In an equal part or degree;");
dictionary.put(word);
word = new Word("HALF NELSON", "A hold in which one arm is thrust under the corresponding armof the opponent.");
dictionary.put(word);
word = new Word("HALF SEAS OVER", "Half drunk. [Slang: used only predicatively.] Spectator.");
dictionary.put(word);
word = new Word("HALF-AND-HALF", "A mixture of two malt liquors, esp. porter and ale, in aboutequal parts.");
dictionary.put(word);
System.out.println(dictionary.getTextDefinition("half"));
System.out.println(dictionary.getTextDefinition("half nelson"));
System.out.println(dictionary.getTextDefinition("half seas over"));
System.out.println(dictionary.getTextDefinition("half taste"));
}
And this is how the Word class looks like:
public class Word implements Comparable{
private String text;
private String definition;
public Word(String text){
this.text = text.toLowerCase();
this.definition = "No definition found";
}
public Word(String text, String definition){
this.text = text.toLowerCase();
this.definition = definition;
}
public String getText() {
return text;
}
public String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
@Override
public String toString() {
String definition = "";
definition = this.getText() + ":\n \t";
definition += "Word Definition: \n \t \t"+ this.getDefinition()+"\n";
return definition;
}
@Override
public int compareTo(Object o) {
int r;
r = getText().compareTo(getText());
return r;
}
public static void main(String[] args) {
Word word = new Word("HETEROGENEAL");
System.out.println(word);
word = new Word("HEINOUS", "Hateful; hatefully bad; flagrant; odious; atrocious; givinggreat great offense;");
System.out.println(word);
}}
So what am I doing wrong and why do I keep getting a NullPointerException?