Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java 'forgetting' about my class instances
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: Tue Jul 19, 2005 3:30 am   Post subject: Java 'forgetting' about my class instances

I'm having some trouble with a block of code

code:

class ExprOpExpr extends Expr
{
    int value;
    Expr left, right;
    String oper;

    public ExprOpExpr (String op, Expr a, Expr b)
    {
        value = 0;
        if (a == null)
            System.out.println ("a == null");
        left = a;
        right = b;
        oper = op;
    }

    public int getValue()
    {
        if (left == null)
        {
            return -1;
        }
        ....more code...
    }
}


This is part of the creation of an abstract syntax tree. On the way down the tree, while I generate it, a and b are defined (System.out.println ("a == null"); is never called). However, on the way back up the tree where getValue() is called, left and right == null, and I can't figure out why.

Thanks in advance for any help.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Jul 19, 2005 11:47 am   Post subject: (No subject)

Since they're objects and thus being passed by reference, is it possible they get nullified after the constructor is called?
Martin




PostPosted: Tue Jul 19, 2005 12:50 pm   Post subject: (No subject)

I don't think so, but how would I make copies of them?
rizzix




PostPosted: Tue Jul 19, 2005 8:50 pm   Post subject: (No subject)

i think you have a logic problem that nullifies ur a and b objects somewhere down the line..

to make copies you are first required to implement the Cloneable inteface to the Expr class.. you should then override and implement the protected void clone(); method in that class (as inherited from the Object class) but override it as public, then in the client code above you simply call .clone();

PS: if you are fine with a "shallow copy" of the object you can just as well put the following line in the body of the clone() method: return super.clone();
Martin




PostPosted: Wed Jul 20, 2005 7:16 pm   Post subject: (No subject)

Figured it out. Me being stupid. Thanks for the help.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: