*getting following error.
duplicate found
Exception in thread "main" java.lang.NullPointerException

    at binarysearchtree.main(binarysearchtree.java:185)

Java Result: 1
here is my code*

        public class node<T>
        {
             public node<T>  root ;
            public T data;
            public node left;
            public node right;


            public node (T newData)
            {
                data = newData;
                left = null;
                right = null;
                root=null;  
            }

            public node ()
            {
                data = null;
                left = null;
                right = null;
            }







            public void setData (T newData)
            {
                data = newData;
            }

            public T getData ()
            {
                return data;
            }

            public void setLeft (node newLeft)
            {
                left = newLeft;
            }

            public node getLeft ()
            {
                return left;
            }

            public void setRight (node newRight)
            {
                right = newRight;
            }

            public node getRight ()
            {
                return right;
            }
            public node getRoot ()
            {
                return root;
            }

            public void setRoot (node newRoot)
            {
                root = newRoot;
            }



            public boolean equals (node theOther)
            {
                return this.data.equals (theOther.data);
            }

          public int compareTo (node theOther)
            {
                return ((Integer) (this.getData ())).compareTo ((Integer) (theOther.getData ()));
            }



        }







        public class binarysearchtree<T>  extends node<T>
        {
            public node root;
            public binarysearchtree ()
            {
                root=null;
            }


            public boolean empty ()
            {
                return(root==null);
            }

           public boolean addOne (T newMember)
            {
                boolean success = true;
                node<T> newNode = new node<T> (newMember);
                if (empty ())
                    setRoot (newNode);
                else
                {
                    node p = null;
                    node c = getRoot ();
                    int compare = 0;

                    while (c != null)
                    {
                        compare = newNode.compareTo (c);
                        if (compare == 0)
                        {
                            System.out.println ("duplicate found");
                            success = false;
                            return success;
                        }
                        else
                        {
                            p = c;
                            if (compare > 0)
                                c = c.getRight ();
                            else
                                c = c.getLeft ();
                        }
                    }

                    if (compare > 0)
                        p.setRight (newNode);
                    else
                        p.setLeft (newNode);
                }
                return success;

            }

           public T delete (node key)
            {
               T dataInDeleted = null;
               if (!empty ())
               {
                   node p = null;
                   node c = getRoot ();
                   int compare = 0;

                    while (c != null)
                    {
                        compare = key.compareTo (c);
                        if (compare == 0)
                            break;
                        else
                        {
                            p = c;
                            if (compare > 0)
                                c = c.getRight ();
                            else
                                c = c.getLeft ();
                        }
                    }

                   if (c == null)
                   {
                       System.out.println  ("not found");
                       return dataInDeleted;
                   }
                   else
                   {
                       dataInDeleted = (T) c.getData ();
                       node betweenPnC  = null;



                       //2-child case, go right once then keep going left till null
                       node px = c;
                       node pc = c.getRight ();

                       if (pc.getLeft () != null)
                       {
                           px = pc;
                           pc = pc.getLeft ();
                       }

                       c.setData ((T) pc.getData ());

                       if (px == c)
                           px.setRight (pc.getRight ());
                       else
                           px.setLeft (pc.getRight ());
                   }

              }

             return dataInDeleted;



           }
           public void inOrder (node root)
            {
                if (root != null)
                {
                    inOrder (root.getLeft ());
                    System.out.println (root);
                    inOrder (root.getRight ());
                }
            }

           //use inorder traversal
           public void traverse ()
            {
                node startPoint = getRoot ();
                inOrder (startPoint);


            }
        public void setRoot (node newRoot)
            {
                root = newRoot;
            }
            public node getRoot ()
            {
                return root;
            }
            public node binarySearch (node key)
            {
                node found = null;
                node temp = getRoot ( );
                while (temp != null)
                {
                        int x = key.compareTo (temp);
                        if (x == 0)
                        {
                            found = temp;
                            break;
                        }
                        else if (x < 0)
                            temp = temp.getLeft ( );
                        else
                            temp = temp.getRight ( );
                }

                return found;
            }









         public static  void main(String[] args) {

        binarysearchtree<Integer> tree  = new binarysearchtree<Integer>();


        tree.addOne(88);
        tree.addOne(88);


        tree.delete(new node(88));
        tree.delete(new node(7));
        tree.addOne(90);
        tree.traverse();






                System.out.println(tree);





         }


        }    

read the line, read the error.
Based on the error, figure out what's wrong with the line, it's trivially easy and obvious.

Actually NullPointerException (NPE) is not always an easy error to resolve because the cause could happen somewhere else in the code before it hits the NPE. I have no idea where the NPE occurs in your error message. It could be from deleting the existing node (tree.delete(new node(88));), or it could happen right while searching to delete the node(7). However, it happens after adding the duplicated node (88) for sure because your code gets out of addOne(88) safely (from the message duplicate found).

What you need to do is to pin point where the cause of null pointer creation. If you are using an IDE, it should have a functionality for you to step-by-step execution. If you don't, insert System.out.println(whatever_message_you_need) to find out whether the program is doing what you are expecting it to do (i.e. inspect your current tree status, check if the node is properly inserted, etc.).

Once you find the cause of NPE, then you could formulate the fix. Hope this help.

After looking at your code again, I am wondered why you need this class to be generic... The reason is from your compareTo() method. If you want the tree to be generic, why do you convert the data to Integer? In other words, the method indicates that you want your class to compare integer with integer (or char with char). The class handles integer, Integer, and char types, but not String. These types aren't that different from one another and could be handled in a much simple way.

From my understanding, generic class is for handling a range of types of incoming object class. So comparison part will be very tricky because one needs to determine the domain and range of the data and how to compare them. If the domain and range are narrow (and specific), implementing the class to be generic is not worth the time because it is not really a generic class...

So I am guessing that this assignment teaches you syntax of generic class, but not the purpose of a generic class. I hope the instructor has pointed this out to you.

From my understanding, generic class is for handling a range of types of incoming object class. So comparison part will be very tricky because one needs to determine the domain and range of the data and how to compare them. If the domain and range are narrow (and specific), implementing the class to be generic is not worth the time because it is not really a generic class...

This is the reason we have interfaces like Comparable. It doesn't matter what your class is; as long as you are satisfying the Comparable contract, two objects of that type can be compared. This is how classes like TreeSet work under the hood; they are generic even though it requires comparing two objects before deciding their location in the tree. Since generic type parameters can have bounds, it's very much possible to have generic containers which allow storage of arbitrary types as long as the type implements the Comparable interface....

Actually NullPointerException (NPE) is not always an easy error to resolve because the cause could happen somewhere else in the code before it hits the NPE.

true, but on the line pointed to by the npe there's only one thing that can ever be null, so it's trivial to guard against it.
And then of course the real work is figuring out whether that thing should ever be expected to be null (and it almost certainly should here, given what it is).

So yeah, it's a trivial example of an NPE and how to deal with one.

So yeah, it's a trivial example of an NPE and how to deal with one.

I understand. However, for newbies, they can't distinguish between trivia and non-trivia NPE problem. I just want to let them know that they shouldn't think or treat their problem as trivia because they need to learn and experience as much as they can now. Later on, they could decide whether or not the problem is trivia. I hope you understand that too. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.