1

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?

Nadia
  • 13
  • 2
  • please point to the exact line where you get the exception, as we don't have line numbers here. – Tom Elias Aug 09 '21 at 13:06
  • 1
    *`if(this.containsValue(text) == true){`* cannot be true as 'text' is of type `String`. Note that you need to override `hashCode` and `equals` in `Word` for it to work properly in collections – g00se Aug 09 '21 at 13:09
  • I believe you are calling getters without assigning anything to the returned value. e.g. line 5 in your first snippet should probably be `String wordText = word.getText()` and then use `wordText` instead of `text` for the lookup. – Zyl Aug 09 '21 at 13:09

2 Answers2

0

If get(text) returns null you will get a NullPointerException because you try to call toString() on the returned object.

public String getTextDefinition(String text){
    String definition = "";

    Word word = new Word(text);
    word.getText(); // unused return

    if(this.containsValue(text) == true){
        this.get(word);
        definition = word.toString();
    }

    if(this.containsValue(text) == false){
            definition = get(text).toString(); // NullPointerException here
    }

    return definition;
} 

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.

null being returned from a Map is its way of telling you the key is unknown. See https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object- (adjust for the version of Java you are using)

You are supposed to return word.toString() in that case, so:

public String getTextDefinition(String text){
    String definition = "";

    Word word = new Word(text);
    text = word.getText(); // update text to use the lower-case standardization of Word

    if(this.containsValue(text) == true){
        this.get(word);
        definition = word.toString();
    }

    if(this.containsValue(text) == false){
            Word mappedWord = get(text);
            if (mappedWord != null) {
                definition = mappedWord.toString();
            } else {
                definition = word.toString();
            }
    }

    return definition;
} 
Zyl
  • 2,690
  • 2
  • 22
  • 27
0

In addition to the comments I made about overriding methods, this is what you want:

public String getTextDefinition(String text) {
   String definition = null;
   Word w = get(text);
   if (w != null) {
      definition = w.getDefinition();
   }
   return definition;
}

Making your run output:

In an equal part or degree;
A hold in which one arm is thrust under the corresponding armof the opponent.
Half drunk. [Slang: used only predicatively.] Spectator.
null

The above is based on an equals method that compares Word.text

g00se
  • 3,207
  • 2
  • 5
  • 9