Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: morse code

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default morse code

    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...

    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;
    	}
    }
    Second Class:
    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();
    			}
    		}
    	}
    }
    Last edited by helloworld922; May 7th, 2012 at 11:23 PM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 Posts

    Default Re: morse code

    i am getting a null pointer
    Please post the full text of the error message.
    Look at the source line referenced in the error message and find the variable with the null value. Then back track in the code to find out why that variable does not have a valid non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  2. [SOLVED] Translate sentence to morse cod?
    By CSUTD in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 27th, 2012, 02:38 PM
  3. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 06:43 AM
  4. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 07:36 AM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 08:30 PM