This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi, I want to read in strings, and store them in a binary tree, i know that they must be objects, the problem i'm having now is that i don't know how to write the insert method because it must have (Comparable object) has parameters and when i try to store a the word i read in as a string object - Object word = reader.readLine(); and then TRY to store it i get TreeTest1.java:14: insert(java.lang.Comparable) in Tree cannot be applied to (java.lang.Object) t.insert(word); ^ Any ideas? Thanks.
A String IS A Comparable class so it can be used as the input to the method. The method will treat it as Comparable class.
Dave.
leon matthew
Greenhorn
Joined: Oct 10, 2001
Posts: 27
posted
0
Thanks i tried that and now i'm getting a NullPointerException: This is the code for my tree: class Tree { // Constructs an empty Tree public Tree() { root = null; } /* Inserts a new node into the tree. @param obj the object insert */ public void insert(Comparable obj) { Node newNode = new Node(); newNode.data = obj; newNode.left = null; newNode.right = null; if (root == null) root = newNode; else root.insertNode(newNode); } /*Prints the contents of the tree in a sorted order */ public void print() { if (root != null) root.printNodes(); } private Node root; private class Node { /* Inserts a new node as a descendant of this node @param newNode the node to insert */ public void insertNode(Node newNode) { if (newNode.data.compareTo(data) < 0) { if(left == null) left = newNode; else left.insertNode(newNode); } else { if(right == null) right = newNode; else left.insertNode(newNode); } } /* prints this node and ALL of its descendants in sorted order */ public void printNodes() { if (left != null) left.printNodes(); System.out.println(data); if (right != null) right.printNodes(); } public Comparable data; public Node left; public Node right; } } I must have made a really dumb and obvious mistake but i can't see it!
If you're getting a NullPointerException, you are also getting a stack trace. Study this carefully - it tells you exactly which line is throwing the stack trace. This is usually a good clue about what your problem is. If you can't work out the problem, at least tell us which line the stack trace reports as the source of the error.
"I'm not back." - Bill Harding, Twister
leon matthew
Greenhorn
Joined: Oct 10, 2001
Posts: 27
posted
0
Sorry. These are the errors: >left.insertNode(newNode); insertNode method at Tree$Node.insertNode(TreeTest1.java:90) >root.insertNode(newNode); insert method
at Tree.insert(TreeTest1.java:56) >t.insert(word);
at TreeTest1.main(TreeTest1.java:14) The word being read in is through a BufferedReader reading a file specified at the command prompt. I've been at this problem for several hours now...its frustrating. [This message has been edited by leon matthew (edited October 25, 2001).]