This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Create an object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Create an object?" Watch "Create an object?" New topic
Author

Create an object?

leon matthew
Greenhorn

Joined: Oct 10, 2001
Posts: 27
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.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459


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
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!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
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).]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Looks wrong.
- Peter
leon matthew
Greenhorn

Joined: Oct 10, 2001
Posts: 27
I'm such an idiot! I've been staring at this thing for ages and never noticed. thanks :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create an object?
 
Similar Threads
Java binary search tree
Inorder traversal of a Binary Search Tree
Binary Search Trees
Anyone know How to solve this basic problem?
Tad Tree