so for this project I have to run the program through a command prompt and I'm getting some errors not sure how to run a debugger when going through command prompt.
im getting a null pointer exception at MorseCodeTree.createTree(MorseCodeTree.java:39) and at MorseCode.main(MorseCode.java:17)
MorseCodeTree is my code it can be altered.
MorseCode and BinaryTree(not posted but I can if needed) is instructors code and cannot.
Code
public class MorseCodeTree extends BinaryTree<String>
{
public MorseCodeTree()
{
//calls the 3-argument BinaryTree consutrctor
super("", null, null);
}
public void createTree(String fileName) throws FileNotFoundException
{
Scanner fileScan = new Scanner(new File(fileName));
BinaryTree.Node morseNode = root;
while(fileScan.hasNext())
{
String line = fileScan.nextLine();
Scanner morseScan = new Scanner(line);
String letter = morseScan.next();
String code = morseScan.next();
morseNode = root;
for(int i=0; i < code.length() - 1; i++)
{
if(code.charAt(i) == '.')
{
morseNode = morseNode.left;
morseNode.left = new BinaryTree.Node(letter);
}
else if(code.charAt(i) == '-')
{
morseNode = morseNode.right;
morseNode.right = new BinaryTree.Node(letter);
}
else
{
throw new RuntimeException("Invalid Morse code: " + code);
}
}
}
}
public String encodeMessage(String message)
{
StringBuilder strBuilder = new StringBuilder();
for(int i = 0;i < message.length(); i++)
{
strBuilder.append(encodeLetter(message.substring(i, i + 1), root, "") + " ");
}
return strBuilder.toString();
}
private String encodeLetter(String letter, BinaryTree.Node<String> morseNode, String code)
{
String retStr = null;
if(morseNode == null)
{
return null;
}
if(((String)morseNode.data).equals(letter))
{
retStr = letter;
}
if(retStr == null && morseNode.left != null)
{
retStr = encodeLetter(letter, morseNode.left, ".");
}
if(retStr == null && morseNode.right != null)
{
retStr = encodeLetter(letter, morseNode.right, "-");
}
return retStr;
}
public String decodeMessage(String message)
{
StringBuilder strBuilder = new StringBuilder();
Scanner messageScanner = new Scanner(message);
BinaryTree.Node morseNode = root;
while(messageScanner.hasNext())
{
String scannedMessage = messageScanner.next();
if(scannedMessage.equals("null"))
{
strBuilder.append(" ");
}
else
{
morseNode = root;
for(int i = 0; i < scannedMessage.length(); i++)
{
if(scannedMessage.charAt(i) == '.')
{
morseNode = morseNode.left;
}
else if(scannedMessage.charAt(i) == '-')
{
morseNode = morseNode.right;
}
else
{
throw new RuntimeException("Invalid Morse code: " + scannedMessage);
}
}
strBuilder.append((String)morseNode.data);
}
}
return strBuilder.toString();
}
}
Code
public class MorseCode
{
public static void main(String[] args) throws FileNotFoundException
{
if(args.length != 3 || !(args[0].equalsIgnoreCase("encode") || args[0].equalsIgnoreCase("decode")))
{
System.out.println("usage: java MorseCode encodeOrDecode inputFileName outputFileName");
return;
}
MorseCodeTree tree = new MorseCodeTree();
tree.createTree("morsecode.txt");
Scanner in = new Scanner(new File(args[1]));
PrintWriter out = new PrintWriter(args[2]);
if(args[0].equalsIgnoreCase("encode"))
{
while(in.hasNextLine())
{
out.println(tree.encodeMessage(in.nextLine()));
}
out.close();
System.out.println(args[1] + " encoded into " + args[2]);
}
else
{
while(in.hasNextLine())
{
out.println(tree.decodeMessage(in.nextLine()));
}
out.close();
System.out.println(args[1] + " decoded into " + args[2]);
}
}
}