Question: I am trying to make a program that acts as a thesaurus, it has 3 classes: Entry, Thesaurus, and Proj6 (main method). I have everything

I am trying to make a program that acts as a thesaurus, it has 3 classes: Entry, Thesaurus, and Proj6 (main method). I have everything coded but I am getting a nullPointerException, I am getting it in thesaurus at line 34 (else if (entries[index].addSynonym(syn) == false)), and in my main at line 19(th.addEntry(word, synonyms[i]);) but I am not sure why or how to fix it?

Entry Class:

import java.util.*;

public class Entry {

// Fields:

private String word;

private String[] synonyms;

private int index;

// Constructor:

public Entry(String w) {

word = w;

synonyms = new String[10];

index = 0;

}

// Methods:

public String getWord() {

return word;

}

public boolean addSynonym(String syn) {

if (index == 10) {

return false;

}

if (isSynonym(syn) == true) {

return false;

}

else {

synonyms[index] = syn;

index++;

return true;

}

}

public String getSynonyms() {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < index; i++) {

sb.append(synonyms[i]);

sb.append(",");

}

String syns = sb.toString();

if (syns.length() > 0) {

syns = syns.substring(0, syns.length() - 1);

}

return syns;

}

public boolean isSynonym(String syn) {

for (int i = 0; i < synonyms.length; i++) {

if (synonyms[i].equalsIgnoreCase(syn)) {

return true;

}

}

return false;

}

}

Thesaurus Class:

import java.util.*;

public class Thesaurus {

// Fields:

private Entry[] entries;

private int index;

// Constructor:

public Thesaurus() {

entries = new Entry[7000];

index = 0;

}

private Entry getEntry(String word) {

for (int i = 0; i < index; i++) {

if (entries[i].getWord().equalsIgnoreCase(word)){

return entries[i];

}

}

return null;

}

public boolean addEntry(String word, String syn) {

if (getEntry(word) == null) {

Entry e = new Entry(word);

e.addSynonym(syn);

entries[index] = e;

index++;

return true;

}

if (getEntry(word) != null) {

if (index == entries.length) {

return false;

}

else if (entries[index].addSynonym(syn) == false) {

return false;

}

else if (entries[index].isSynonym(syn) == true) {

return false;

}

else {

entries[index].addSynonym(syn);

}

}

return true;

}

public String lookup(String word) {

if (getEntry(word) != null) {

String synonym = entries[index].getSynonyms();

return synonym;

}

else {

String synonym = entries[index].getWord() + " not found";

return synonym;

}

}

}

Main Method:

import java.util.*;

import java.io.*;

public class Proj6 {

public static void main(String[] args) throws IOException {

Scanner s = new Scanner(System.in);

Scanner inFile = new Scanner(new File("testThesaurus.txt"));

Thesaurus th = new Thesaurus();

while (inFile.hasNext()) {

String line = inFile.nextLine();

StringTokenizer st = new StringTokenizer(line, "[]");

String word = st.nextToken();

int bracketIndex = line.indexOf("]");

String syns = line.substring(bracketIndex + 2);

String[] synonyms = new String[10];

synonyms = syns.split(",");

for (int i = 0; i < synonyms.length; i++) {

th.addEntry(word, synonyms[i]);

}

System.out.println(th.lookup(word));

}

char option;

do {

System.out.print("Enter (l)ookup, (a)dd entry, or (q)uit: ");

option = (s.nextLine().toLowerCase()).charAt(0);

if (option == 'l') {

}

else if (option == 'a') {

}

else {

break;

}

}while (option != 'q');

//System.out.print(th.lookup(word));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!