This discussion is archived
5 Replies Latest reply: Mar 21, 2013 10:41 AM by 997855 RSS

Object in class is null, while it should not be

997855 Newbie
Currently Being Moderated
I am writing a class: Morse.java. This class should have the option to first add the Morse alphabet (e.g. Morse morse = new Morse(); morse.addCode(". -", new Character('E'));)
After that the class should enable the user to translate a text in morse to normal text with a get method (e.g. System.out.print(morse.get(".- "));

The Morse class uses a MorseNode class. Each node contains a character (e.g. A) and two 'children' nodes (left & right).

The problem in my code is that the get method in the Morse class does not work. It should select the right element, which contains a character. However the elements turn out to be null. I do not understand this, especially because the addCode method seems to work fine.

The code below contains all three classes.

Thanks in advance.

public class TestMorseNode {
     public static void main(String[] args) {
          Morse morse = new Morse();
          
          morse.addCode(". ", new Character('E'));
          morse.addCode(".- ", new Character('A'));
          
          System.out.print(morse.get(".- "));
          System.out.print(morse.get(". "));
          
     }
}     


public class Morse {
     private MorseNode root;
     
     /* The root will not contain an element.*/
     public Morse() {
          root = new MorseNode('$');
     }
     
     public void addCode(String s, Character e) {
          MorseNode current = root;
          if (s.charAt(0) == ' ') { 
               System.out.println("False input");
          }
          else {
               for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) == '.') 
                         current = current.left;
                    else if (s.charAt(i) == '-') 
                         current = current.right;     
                    else if (s.charAt(i) == ' ') 
                         break;
                    else
                         System.out.println("False input, the character " + s.charAt(i) + " is ignored");
               }     
               current = new MorseNode(e);
               
          }
     }
     
     /** This method seems to be the problem */
      public String get(String s) {
          MorseNode current = root;
          if (s.charAt(0) == ' ') {
               System.out.println("False input");
               return null;**
          }
          else {
               for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) == '.')
                         current = current.left;
                    else if (s.charAt(i) == '-') 
                         current = current.right;     
                    else if (s.charAt(i) == ' ')
                         break;
                    else
                         System.out.println("False input, the character " + s.charAt(i) + " is ignored");
                    
                    
               }          
               if (current == null) 
                    return "Error this is no morse code";
               else
                    return current.get();
          }
     }
               

}



public class MorseNode {
     protected Character element;
     public MorseNode left;
     public MorseNode right;
     
     public MorseNode(Character e) {
          element = e;
          
          if (left == null)
               left = new MorseNode();
          if (right == null)
               right = new MorseNode();
     }     
     
     public MorseNode() {
     }
     
     public String get() {
          if (element == null)
               return "This element is empty";
          else
               return element.toString();
     }

     public boolean empty() {
          boolean empty = false;
          if (element != null)
               empty = false;
          return empty;     
          
     }
}
Note: I use Java, jdk 1.7.0_11

Edited by: 994852 on 19-mrt-2013 10:50

Edited by: 994852 on 19-mrt-2013 10:53

Edited by: 994852 on 19-mrt-2013 10:54
  • 1. Re: Object in class is null, while it should not be
    939520 Explorer
    Currently Being Moderated
    I suggest filling your code with System.out.println() statements to see what is happening. You may also consider using your IDE's the debugger (breakpoints).
  • 2. Re: Object in class is null, while it should not be
    gimbal2 Guru
    Currently Being Moderated
    936517 wrote:
    I suggest filling your code with System.out.println() statements to see what is happening. You may also consider using your IDE's the debugger (breakpoints).
    Indeed.

    Also: spot the little bug in the empty() method. Can it ever be empty I wonder? I don't think so in the current state.

    That is already proof enough that the code simply needs to be examined further, it is way too soon to start asking for external help. The System.out trick is a good "I don't know how to debug" way to see whats going on in the code. Somewhere a wrong assumption is made, all you have to do is find it. Should be easy enough.
  • 3. Re: Object in class is null, while it should not be
    997855 Newbie
    Currently Being Moderated
    Thank you both for your reply. Indeed it is very useful to trace the program with System.out.println statements and so I did already before I posted this code. Therefore I found out that the morse.get method is not working as it should and that the morse.addCode method is working just fine. The bug in the empty method is indeed embarassing, but it does not matter as the method is not used.

    Furthermore I think I forgot to specify the output. When I run the program I get a NullPointerException. I think this is very weird, because I actually created these nodes first using the morse.addCode method. (the exception also shows that the error is in the first call of the morse.get() method).

    As you can see in the code I added a System.out.println statement, however this is exactly where the error occurs because I try to access the element of current (a MorseNode) which is null at that moment. This is frustrating because I added a character to the element field of this node using the morse.addCode method (and it worked, I checked it with System.out.println statements) and therefore I should get the character back instead of null.

    public String get(String s) {
              MorseNode current = root;
              if (s.charAt(0) == ' ') { 
                   System.out.println("False input");
                   return null;
              }
              else {
                   for (int i = 0; i < s.length(); i++) {
                        if (s.charAt(i) == '.') 
                             current = current.left;
                        else if (s.charAt(i) == '-') 
                             current = current.right;     
                        else if (s.charAt(i) == ' ') 
                             break;
                        else
                             System.out.println("False input, the character " + s.charAt(i) + " is ignored");
                        
                        System.out.println("test 2 " + current.element);
                   }          
                   if (current == null) 
                        return "Error this is no morse code";
                   else
                        return current.get();
              }
         }
  • 4. Re: Object in class is null, while it should not be
    maxkar Newbie
    Currently Being Moderated
    Hi. Your addCode method does not work correctly. It creates a new node for code ".- " but never attaches it to a tree. Try to print tree (from the root node) after each call to morse.addCode.

    One more note. At this time your tree building requires particular order of input items. You cannot add ".--. " as a first code, for example. Try to add nodes when your are walking down the tree and when appropriate node was found just set a new character to it (and, maybe, warn, if that node was already bound to another letter).
  • 5. Re: Object in class is null, while it should not be
    997855 Newbie
    Currently Being Moderated
    You are right. The addCode did not work properly. I have adjusted it and it works now! Thank you for your help!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points