• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Create an object?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
leon matthew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks wrong.
- Peter
 
leon matthew
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm such an idiot! I've been staring at this thing for ages and never noticed. thanks :-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic