Results 1 to 3 of 3
  1. #1
    conderol is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Problem with Generics and BinaryTree

    HI. I have a task to write a BinaryTree that gets Generic object as data that the tree stores. Sounds simple and I was able to build it fast and tested with Integers and Strings. But when I build my own class (Person) it didn't work and I have no idea why. Can anyone help

    TreeNode class...

    Java Code:
    // class TreeNode definition
    class TreeNode<T extends Comparable<T>>
    {
       // package access members
       TreeNode<T> leftNode; // left node  
       T data; // node value
       TreeNode<T> rightNode; // right node
    
       // constructor initializes data and makes this a leaf node
       public  TreeNode( T nodeData )
       { 
          data = nodeData;              
          leftNode = rightNode = null; // node has no children
       } // end TreeNode constructor
       
       public T getTreeData()
       {
    	   return this.data;
       }
       // locate insertion point and insert new node; ignore duplicate values
       public  void insert( T insertValue )
       {
    	  // insert in left subtree
    	  // System.out.println(insertValue.compareTo(current));
          if ( insertValue.compareTo(data) <0 ) 
          {
             // insert new TreeNode
             if ( leftNode == null )
                leftNode = new TreeNode<T>( insertValue );
             else // continue traversing left subtree
                leftNode.insert( insertValue ); 
          } // end if
          else if ( insertValue.compareTo(data) >0 ) // insert in right subtree
          {
             // insert new TreeNode
             if ( rightNode == null )
                rightNode = new TreeNode<T>( insertValue );
             else // continue traversing right subtree
                rightNode.insert( insertValue ); 
          } // end else if
       } // end method insert
    } // end class TreeNode


    Tree class...
    Java Code:
    	public class Tree<T extends Comparable<T>> 
    	{
    	   private TreeNode<T> root;
    	
    	   // constructor initializes an empty Tree of integers
    	   public Tree() 
    	   { 
    	      root = null; 
    	   } // end Tree no-argument constructor
    	
    	   // insert a new node in the binary search tree
    	   public void insertNode( T insertValue )
    	   {
    		   if (root!=null)
    		     System.out.println(root.getTreeData());
    		   System.out.println(insertValue);
    	      if ( root == null )
    	         root = new TreeNode<T>( insertValue ); // create the root node here
    	      else
    	         root.insert( insertValue ); // call the insert method
    	   } // end method insertNode
    	
    	   
    	  
    	} // end class Tree
    Person class....

    Java Code:
    	class Person implements Comparable<Person> 
    	{
    	  private String _firstName;
    	  private String _lastName;
    	  private String _id;
    	  
    		//const'
    	  public Person(String firstName, 
    			        String lastName,
    			        String id) 
    	  {
    	    this._firstName = firstName;
    	    this._lastName  = lastName;
    	    this._id        = id;
    	  }
    	  //need this for comparation
    	  public String getFullName()
    	  {
    		  return _firstName+" "+_lastName;
    	  }
    	  public String toString() {
    	    return _firstName + " " + _lastName+" ["+_id+"]";
    	  }
    	  //implementation of abstract method
    	  public int compareTo(Person person) 
    	  {
    		return  (_firstName+" "+_lastName).compareTo(person.getFullName());
    	  }
    	  
    	}

    And test class..


    Java Code:
    	
    	// This program tests class Tree.
    	import java.util.InputMismatchException;
    	import java.util.Scanner;
    	
    	
    	public class TreeTest 
    	{
    	   public static void main( String args[] )
    	   {
    	   
                  Scanner scanner = new Scanner(System.in);
    	      System.out.println("\nPersons Tree....\n");
    	      Tree<Person> perTree = new Tree<Person>();
    	      int counter=0;
    	      Person [] persons = new Person[6] ;
    	      String firstName;
    	      String lastName;
    	      String id;
    	      while (counter<6)
    	      {
    	    	  
    	    	  try
    	    	  {
    	    		  System.out.print("Enter First Name(Person "+(counter+1)+"): ");
    	    		  firstName = (scanner.nextLine());
    	    		  System.out.print("Enter Last Name(Person "+(counter+1)+"): ");
    	    		  lastName  = (scanner.nextLine());
    	    		  System.out.print("Enter id Name(Person "+(counter+1)+"): ");
    	    		  id        = (scanner.nextLine());
    	    		  persons[counter] = new Person(firstName, lastName, id);
    	    		  System.out.println(persons[counter]);
    	    		  counter++;
    	    		  perTree.insertNode(persons[counter]) ;
    	    		  
    	    	  }
    	    	  catch(InputMismatchException e)
    	    	  {
    	    		  System.err.println(e.getMessage());
    	    	  }
    	      }
    	      System.out.println ( "\n\nPreorder traversal" );
    	      perTree.preorderTraversal(); // perform preorder traversal of tree
    	      System.out.println ( "\n\nInorder traversal" );
    	      perTree.inorderTraversal(); // perform inorder traversal of tree
    	      System.out.println ( "\n\nPostorder traversal" );
    	      perTree.postorderTraversal(); // perform postorder traversal of tree
    	      
    	   } 
    	} // end class TreeTest
    After I enter second Person I get

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at TreeNode.insert(TreeNode.java:29)
    	at Tree.insertNode(Tree.java:20)
    	at TreeTest.main(TreeTest.java:84)
    again... everything is working fine when I use Integers and Strings

  2. #2
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    I'm guessing that line 84 of TreeTest.main is the last line in this snippet?

    Java Code:
    	    lastName  = (scanner.nextLine());
    	    System.out.print("Enter id Name(Person "+(counter+1)+"): ");
    	    id        = (scanner.nextLine());
    	    persons[counter] = new Person(firstName, lastName, id);
    	    System.out.println(persons[counter]);
    	    counter++;
    	    perTree.insertNode(persons[counter]) ;
    When you increment counter and you do perTree.insertNode, your persons[counter] is null. This works with Integer and String because they are wrappers of primitive data. Primitive data have default values when they are not initialized (0 and "" respectively). Since the compiler doesn't know what the default value of your Person class is, it remains null.

    The fix is to make sure persons[counter] is not null before inserting it.

  3. #3
    conderol is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    You are right!!!! I could sit on this for hours and don't see it

Similar Threads

  1. Generics Problem (Bound Mismatch)
    By castiel in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 01:05 AM
  2. Generics problem
    By ankur.trapasiya in forum New To Java
    Replies: 2
    Last Post: 01-22-2011, 06:09 PM
  3. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 04:08 AM
  4. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-11-2008, 12:08 AM
  5. Generics
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 10:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •