Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

object question

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi everybody, i have the following code:

///////////////////////////////////////////////////
class listItem {
Object data = null;listItem next = null;listItem prev = null;
public pbInfo getData () {
return data;
}
public listItem (Object obj) {
data = obj; next = null; prev = null;
}
}

class doublyLinkedList {

listItem front = null;
listItem rear = null;
int numItems = 0;

public int getNumItems() {
return (numItems);
}

public void addData (Object obj)
{
if (front == null) {
front = new listItem (obj);
rear = front;
// initialization
}
else {
listItem listPtr = new listItem (obj);
listPtr.prev = rear;
rear.next = listPtr;
rear = rear.next;
} numItems++;
}

public String toString ()
{
listItem listPtr = front;
String s = "List: (" + numItems + " items)";
int i = 1;
while (listPtr != null) {
s += "\nItem# " + i + ": " + listPtr.getData();
i++;
listPtr = listPtr.next;
}
return s + "\n";
}

public listItem getItemAtPosition (int i)
{
listItem listPtr = front;
for (int n=1; n<i; n++) {
listPtr = listPtr.next;
} return listPtr;
}
}
///////////////////////////////////////////////////
// Then I have a class object called info
class info {
public info () {
}
}
///////////////////////////////////////////////////
public void main (String[] argv) {

doublyLinkedList linkList = new doublyLinkedList ();
for (int n=0;n<10;n++) {
info newInfo = new info();
callList.addData(newInfo); // creates a link list of info
}
info tempInfo = new info();
tempInfo = linkList.getItemAtPosition(n).getData(); // *** ERROR HERE
/ / linkList.getItemAtPosition(n).getData(); returns an Object which
is not compatible with info
}
///////////////////////////////////////////////////
/* linkList creates a link list of listItem object (which contains an
info object) by calling doublyLinkedList.addData */
/* then a temporary info object is created */
/* so that the info object can be pulled out from listItem */
/* linkList.getItemAtPosition(n) returns a listItem */
/* then listItem.getData returns data, which is of type Object which
here would be an info Object */
/* but then i get that error */
/* any help would very much appreciated */
/* Thank you */
///////////////////////////////////////////////////
 
hi johnguest,

Firstly at listItem class, what's that pbInfo? If we make a little changes to listItem class:

class listItem {
info data = null;listItem next = null;listItem prev = null;

public Object getData () {
return data;
}
public listItem (Object obj) {
data = (info)obj; next = null; prev = null;
}
}

and then your main program:
public static void main (String[] argv)
{

doublyLinkedList linkList = new doublyLinkedList ();
int k=0;

for (int n=0;n<10;n++, k=n)
{
info newInfo = new info();
linkList.addData(newInfo); // creates a link list of info
}
info tempInfo = new info();
tempInfo = (info)linkList.getItemAtPosition(k).getData(); // *** Should work
}

So, if your method returns Object, you have to cast it to right type.
And you can't use plain object as data-node because you can't cast it to your own class.
 
public void main (String[] argv) {
doublyLinkedList linkList = new doublyLinkedList ();
for (int n=0;n<10;n++) {
info newInfo = new info();
callList.addData(newInfo); // creates a link list of info <-- shouldn't it be linkList.addData(newInfo) instead? You will get a null pointer exception too since there is no object at index 10 and you called getData() with it.

Just a thought...


If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top