This site requires JavaScript, please enable it in your browser!
Greenfoot back
Jeb_CC
Jeb_CC wrote ...

2015/3/4

How to access a variable from another class.

Jeb_CC Jeb_CC

2015/3/4

#
Yes, yes. I know you're immediately going to think; "Look up a tutorial, dammit!" Well, I have. I think I've tried... 20 at least. NONE of them have worked. I've ALWAYS gotten an error. Either; "Null point exception" or "Variable not found" and it's driving me insane. @_@ This is a little confusing at first but I need two classes to communicate to each other. (Pass variables) One is called Card2 which will have the variable needed. The other is Card which needs to access that variable. I've tried so many methods it's not even funny. I know how it works, but I can't for the life of me do it. I've looked at other people's code and everything. No, my Greenfoot is not bonkers. I've done it before, but that was copying off someone's work, and that took me 4 hours to figure out what the hell I was doing. I'm not going through that again. So yeah, what code do I need? And for what classes? I know some methods used the world class and others didn't need to at all. It's confusing.
danpost danpost

2015/3/4

#
It would help to have more information about your classes. Like, 'they are both Actor subclasses and Card2 extends Card', or something like that. Also how the variable is declared in the Card2 class; whether you have a getter method for it or not; etc. As far as the NullPointerException, giving the entire error message and the class the error occurred in would help.
Jeb_CC Jeb_CC

2015/3/4

#
Basically, I want the card to be able to check if the other one has been selected. If the other one is selected, then I won't allow the card to turn over. However if the other card is NOT selected, this card will be able to be selected. So it's a sort of radio button feature but with cards. The code I used here is a copy of what works with my other game. But of course, doesn't work here for some reason. I get multiple errors for this one. ._. http://i.imgur.com/cLRDq6K.png The errors point to in the world class (VoidWorld):
prepare();
And for some reason here.
Card card = new Card();
        addObject(card, 50, 210);
This I actually have no idea why is coming up as an error. I haven't touched my world class lately. Then there's this:
int FinalCheck = card2.getCheck();
Which is where I'm trying to set the variable FinalCheck to the same number I get from getCheck in the class Card2. Hope I've made enough sense. See lotsa code: \/ This is the relevant part of my Card2 class. Anything underneath was just setting images.
public class Card2 extends Actor
{
    public int Check = 1;
    public int getCheck() {
        return Check;
    }
And here is the Card class which is what takes and uses the variable.
private Card2 card2;
    public Card()
    {
        setImage("CardBack.png");  
    }
    int FinalCheck = card2.getCheck();
Hope this helps you out to understand it.
Super_Hippo Super_Hippo

2015/3/4

#
In your last code snippet, you have 'private Card2 card2;'. Since you don't set it to a Card2 object, it is 'null' and calling a method (in your case line 6 'getCheck') on 'null' causes a NullPointerException. By the way, the error message is build up like this: name of the error - the position where the error occurs - from where the method was called (if it is a method) - and so on... In your case: NullPointer while initiating the Card object which was created in the 'prepare' method of your world VoidWorld which was called from the constructor of the same world.
Jeb_CC Jeb_CC

2015/3/4

#
Okay. So how do I fix? NullPointers are a pain in the butt. X3
danpost danpost

2015/3/4

#
How are you currently assigning (or trying to assign) a value to the 'card2' field in the Card class?
Jeb_CC Jeb_CC

2015/3/4

#
int FinalCheck = card2.getCheck();
There's this which is me trying to set the variable FinalCheck to Card2's Check variable.
danpost danpost

2015/3/4

#
No. Well .. that is not what that line does. That line creates a variable names 'FinalCheck' and tries to assign to it a value returned by call 'getCheck' on an object that should already be assigned to 'card2'. It does not assign an object to 'card2'.
Jeb_CC Jeb_CC

2015/3/4

#
So how would I go about fixing it? @_@ Sorry I'm so blunt. I've just been struggling to comprehend the assigning and calling stuff for a while now. What line of code should I have there instead?
danpost danpost

2015/3/5

#
If you are going to assign 'card2' with an object, then there are several ways to go about it. One is passing the Card2 object to the Card constructor; one is finding the Card2 object when the Card object is placed into the world; and, another is using a method call to set a Card2 object to the field. The first and third ways are quite similar, in that they use parameters to the methods to pass the Card2 object to the "new" Card object. The second way, uses brute force to find the Card2 object in the world to get the object to be referenced in the field. Summary of the first and third ways:
// constructor with parameter
public Card(Card2 cardTwo)
{
    card2 = cardTwo;
}
// method with parameter
public void setCard2(Card2 cardTwo)
{
    card2 = cardTwo;
}
In the VoidWorld class prepare method, you would either:
// using constructor with parameter
Card2 card2 = new Card2();
Card card = new Card(card2);
// using method with parameter
Card card = new Card();
Card2 card2 = new Card2();
card.setCard2(card2);
after which the objects 'card' and 'card2' would be added to the world. The second way would require the Card2 object be placed into the world first and the Card class would use something like this:
protected void addedToWorld(World world)
{
    card2 = (Card2) world.getObjects(Card2.class).get(0);
}
You need to login to post a reply.