I have to make a morse code decoder using trees. Here is what i have so far with two classes... however, i am getting a null pointer and i cant figure out why...
Second Class:import java.util.Map.Entry; import java.util.Collection; public class MorseTree<E> { public Entry root = null; private class Entry { private E value; private Entry leftChild; private Entry rightChild; public Entry(E value, Entry leftChild, Entry rightChild){ this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } public Entry(E value){ leftChild = null; rightChild = null; } } public MorseTree(){ root = new Entry(null); } public void add(E key, String code){ Entry start = root; for(int i = 0; i< code.length(); i ++){ if(code.charAt(i) == '.'){ if(i<code.length()-1 && !start.leftChild.equals(null)){ start = start.leftChild; } else if(i<code.length()-1 && start.leftChild.equals(null)){ start= start.leftChild; start = new Entry(null, start.leftChild, start.rightChild); } else if(i==code.length()-1){ start.leftChild = new Entry(key, start.leftChild.leftChild, start.rightChild.rightChild); } } else if(code.charAt(i) == '-'){ if(i<code.length()-1 && !start.rightChild.equals(null)){ start = start.rightChild; } else if(i<code.length()-1 && start.rightChild.equals(null)){ start= start.rightChild; start = new Entry(null, start.leftChild, start.rightChild); } else if(i==code.length()-1){ start.rightChild = new Entry(key, start.leftChild.leftChild, start.rightChild.rightChild); } } } } public E decode(String code){ Entry start = null; E symbol = null; for(int i = 0; i < code.length(); i ++){ if(code.charAt(i) == '.'){ if(i<code.length()-1 && !start.leftChild.equals(null)){ start = start.leftChild; } else if(i<code.length()-1 && start.leftChild.equals(null)){ System.out.println("This code does not match any morse code in the tree."); } else if(i== code.length()-1 && !start.leftChild.equals(null)){ symbol = start.leftChild.value; } else if(i== code.length()-1 && start.leftChild.equals(null)){ System.out.println("This code does not match any morse code in the tree."); symbol = (E) ""; } } else if(code.charAt(i) == '-'){ if(i<code.length()-1 && !start.rightChild.equals(null)){ start = start.rightChild; } else if(i<code.length()-1 && start.rightChild.equals(null)){ System.out.println("This code does not match any morse code in the tree."); } else if(i== code.length()-1 && !start.rightChild.equals(null)){ symbol = start.rightChild.value; } else if(i== code.length()-1 && start.rightChild.equals(null)){ System.out.println("This code does not match any morse code in the tree."); symbol = (E) ""; } }else if(code.charAt(i) == '|'){ symbol = (E) " "; } else{ System.out.println("Warning: skipping: " + code.charAt(i)); symbol = (E) " "; } }return symbol; } }
import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class MorseDecoder { public static MorseTree<String> tree = null; public static void main(String[] args) { tree = new MorseTree<String>(); String[] codeLine = null; Scanner in = new Scanner(System.in); System.out.println("Enter an input file name: "); String inFile = in.nextLine(); System.out.println("Enter a output file name: "); String outFile = in.nextLine(); FileInputStream fis1 = null; File file = new File("morsecode.txt"); try{ fis1 = new FileInputStream(inFile); }catch(FileNotFoundException e){ e.printStackTrace(); } loadTree(file); try{ BufferedWriter decodeFile = new BufferedWriter(new FileWriter(outFile)); Scanner in1 = new Scanner(fis1); while(in1.hasNextLine()){ String fileLine = in1.nextLine(); codeLine = fileLine.split(" | " ); for(int i = 0; i < codeLine.length; i ++){ decodeFile.write(tree.decode(codeLine[i])); } } decodeFile.close(); fis1.close(); }catch(IOException e){ e.printStackTrace(); } } public static void loadTree(File file){ Scanner in = null; FileInputStream fis = null; try{ fis = new FileInputStream(file); }catch(FileNotFoundException e){ e.printStackTrace(); } in = new Scanner(fis); while(in.hasNextLine()){ String[] list = in.nextLine().split(" ");//splits the two pieces of information char letter = list[0].charAt(0);//letter String letter1 = Character.toString(letter); String code = list[1];//morse code tree.add(letter1, code); try{ fis.close(); }catch(IOException e){ e.printStackTrace(); } } } }