So basically what this code is supposed to do is Spellcheck one certain word taken as input from an EditText
field and display the suggestions in the TextFields
.
getAll()
is the function that calls all the other operations that need to be performed.
At the end after all functions have been executed Public member variable "list"
contains all the suggestions.
The last function sort()
adds all of the suggestions in the List
list
. And in the onClick()
we are trying to print them to textfield
. But
t2.setText(list.get(1).toString());
returns NullPointerException
Error.
The spellcheck code works completely fine when run independently on eclipse.
Here's the error log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.daipayan.myapplication, PID: 2667
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.daipayan.myapplication.MainActivity$1.onClick(MainActivity.java:69)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
And the code:
public class MainActivity extends AppCompatActivity {
public static char keyboard[][] = {{'0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','Q','W','E','R','T','Y','U','I','O','P','0'}, //mapping the keyboard to a multidimensional array
{'0','0','A','S','D','F','G','H','J','K','L','0'},
{'0','0','0','Z','X','C','V','B','N','M','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0'}};
public static String possibleArray[];
public static ArrayList<String> comb;
public static ArrayList<String> filtered;
public static Set<String> dictionary;
public static HashMap<String , Integer> distances;
public static List list;
public static String incorrect_word;
EditText input;
TextView t1,t2,t3;
Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=(EditText)findViewById(R.id.editText);
bt1 = (Button)findViewById(R.id.button);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText = input.getText().toString();
String words[] = inputText.split("\\s");
String lastWord = words[words.length-1];
incorrect_word = "HELLP";
try {
getAll();
} catch (IOException e) {
e.printStackTrace();
}
t1 = (TextView)findViewById(R.id.text1);
t2 = (TextView)findViewById(R.id.text2);
t3 = (TextView)findViewById(R.id.text3);
t1.setText(""+list.get(0));
t2.setText(list.get(1).toString());
t3.setText(list.get(2).toString());
}
});
}
public static void getAll() throws IOException {
loadDictionary();
Log.d("getall","called");
//System.out.println(checkWord(incorrect_word));
//
//fetching the location of characters on the keyboard
//
int index[][] = new int[incorrect_word.length()][2];
for ( int i = 0 ; i < incorrect_word.length() ; i++){
int xy[] = locate(incorrect_word.charAt(i));
index[i][0]=xy[0];
index[i][1]=xy[1];
}
//print(index, incorrect_word.length(), 2);
//
//Fetching all the possible words with the
//
ArrayList<String> possible = characters(incorrect_word, index);
{
possibleArray = possible.toArray(new String[0]);
comb = new ArrayList<>();
dfs("",0);
}
filtered = new ArrayList<String>();
filterCombinations(); //filter out the words that aren't in the dictionary
Levenshtein(incorrect_word);//Calculate distances between words and the incorrect word
printHash();//Print the hash map consisting of the words and their respective distances
sort();
}
public static int[] locate(char a){
int i=1,j=0;
for( ; i < 4 ; i++ ){
for( j=1 ; j < 11; j++ ){
if(keyboard[i][j]==a)
return new int[] {i,j};
}
}
return new int[] {i,j};
}
public static ArrayList<String> characters(String incorrect_word, int index[][]){
ArrayList<String> possible = new ArrayList<String>();
for ( int i = 0 ; i < incorrect_word.length() ; i++){
String character=""+incorrect_word.charAt(i);
int x=index[i][0] , y=index[i][1];
String a = (keyboard[x-1][y] != '0' ? character+=keyboard[x-1][y]: "0");//Upper Right
a = (keyboard[x-1][y-1] != '0' ? character+=keyboard[x-1][y-1]: "0");//Upper Left
a = (keyboard[x][y-1] != '0' ? character+=keyboard[x][y-1]: "0");//left
a = (keyboard[x][y+1] != '0' ? character+=keyboard[x][y+1]: "0");//right
a = (keyboard[x+1][y] != '0' ? character+=keyboard[x+1][y]: "0");//Lower Left
a = (keyboard[x+1][y+1] != '0' ? character+=keyboard[x+1][y+1]: "0");//Lower Right
possible.add(character);
}
return possible;
}
public static void dfs(String x,int i) {
if(i == possibleArray.length) { // there is no more string that can be generated
if(checkWord(x))
comb.add(x); // save the found string
return;
}
for(int j=0;j<possibleArray[i].length();j++) // for each character in the current string
dfs(x+possibleArray[i].charAt(j),i+1); // take the current character and move to the next string
}
public static void loadDictionary() throws IOException{
BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.txt"));
dictionary = new HashSet<>();
while (dictReader.ready())
{
String dictInput = dictReader.readLine() ;
String [] dict = dictInput.split("\\s");
for(int i = 0; i < dict.length;i++)
{
// key and value are identical
dictionary.add(dict[i]);
}
}
dictReader.close();
}
public static Boolean checkWord(String str){
String toCheck = str.toLowerCase();
if(dictionary.contains(toCheck)){
return true;
}
else
return false;
}
public static void filterCombinations(){ for(String x:comb)
if ( checkWord(x) )
filtered.add(x);
}
public static void printHash(){
Iterator iterator = distances.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = distances.get(key).toString();
System.out.println(key + " " + value);
}
}
public static void Levenshtein(String incorrect_word){
distances = new HashMap<String, Integer>();
for(String x:filtered)
distances.put(x,distance(incorrect_word , x ));
}
public static int distance(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
// i == 0
int [] costs = new int [b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
// j == 0; nw = lev(i - 1, j)
costs[0] = i;
int nw = i - 1;
for (int j = 1; j <= b.length(); j++) {
int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
nw = costs[j];
costs[j] = cj;
}
}
return costs[b.length()];
}
public static void sort(){
list=new ArrayList(distances.entrySet());
Collections.sort(list,new Comparator(){
public int compare(Object obj1, Object obj2){
return ((Comparable)((Entry)(obj1)).getValue()).compareTo(((Entry)(obj2)).getValue());
}
});
if(list.size() > 3)
list.subList(4, list.size()).clear();
Log.e("list","the values " + list.get(0) + list.get(1)+list.get(2));
}
}