[Bluej-discuss] Problem with generic types on the workbench

jpagliar@sunyrockland.edu jpagliar at sunyrockland.edu
Fri Apr 21 20:38:25 BST 2006


I've been working with generic types in Java and I've come across an
interesting problem.
This occurred when I was actually working on a linked implementation of
binary trees, but I've traced it back to simpler forms of generics with a
linked list.

Here's the code:

// create a node, next field is null
Node<Integer> n1 = new Node<Integer>(1);
// create a node, next field links to n1
Node<Integer> n2 = new Node<Integer>(2, n1);

This code works fine in a method, but when I try and duplicate the
operations on the object workbench, the second constructor hangs BlueJ,
with the dialog box for the constructor still visible on the screen.

This occurs when I try and pass n1 as an already existing object on the
workbench, or if I instantiate it in the dialog box for the constructor.

Here is the code for the Node class:

// ****************************************************
// Class Node used in the implementation of ADT list
// ****************************************************
public class Node <T> {
  private T item;
  private Node<T> next;

  public Node(T newItem) {
    item = newItem;
    next = null;
  } // end constructor

  public Node(T newItem, Node<T> nextNode) {
    item = newItem;
    next = nextNode;
  } // end constructor

  public void setItem(T newItem) {
    item = newItem;
  } // end setItem

  public T getItem() {
    return item;
  } // end getItem

  public void setNext(Node<T> nextNode) {
    next = nextNode;
  } // end setNext

  public Node<T> getNext() {
    return next;
  } // end getNext
} // end class Node



Nothing unusual there. And as I say, the code works fine when executing it
from a method.

I'm running BlueJ 2.1.2 with the Java SDK 5.0 on a Windows XP machine.

Any ideas??

John Pagliarulo
Professor, Computer Studies
Rockland Community College






More information about the bluej-discuss mailing list