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

2013/5/21

Have an error: non static method cannot be referenced from a static context

FlyGirl95 FlyGirl95

2013/5/21

#
public AussieLand()
    {
        super(600, 400, 1); 
        TitlePage.imageTwo();
        
    }
I have this code, but it gives me the error saying non-static method imageTwo() cannot be referenced from a static context. What does this mean exactly, I've seen it on discussions before, but I'm still a little hazy on the subject, I'm relatively new to Greenfoot. More importantly, though, how do I go about fixing this?
danpost danpost

2013/5/21

#
It means you cannot call that method on a class (or, you can only call the method on an object of that class). You have to create a TitlePage object first, then call the method on that object.
TitlePage titlePage = new TitlePage();
titlePage.imageTwo();
FlyGirl95 FlyGirl95

2013/5/21

#
When I changed it to that, it gave me a new error: constructor TitlePage in calss TitlePage cannot be applied to given types; required: int, greenfoot.World;
FlyGirl95 FlyGirl95

2013/5/21

#
Sorry here is the rest of the error: found: no arguments; reason: actual and formal agreement lists differ in length;
danpost danpost

2013/5/21

#
danpost danpost

2013/5/21

#
Actually, it might be better if you just post the whole class here.
davmac davmac

2013/5/22

#
As danpost says, the error is caused by you trying to call a method directly on the class (TitlePage) when the method works on objects that are instances of the class. A class is just a template for an object, it isn't an object itself. However, the solution given by danpost might not be correct, depending on what you are actually trying to achieve. Were you trying to call the method on an existing instance of TitlePage? In that case, you need to use a reference to the instance (probably a variable) in place of 'TitlePage'. Or, is there not yet any instance of TitlePage, but you want to create one and then call the imageTwo() method? In that case the solution given by danpost is correct, except that you need to give appropriate arguments to the TitlePage constructor (or create a constructor in that class which accepts no arguments), and you possibly also need to add TitlePage to the world (if it is an actor) or set it as the active world (if it is a world). Or the perhaps the method doesn't really need to work on an instance of TitlePage, in which case the fix could be to change the method declaration so that the method is a static method. Or, was it something else? The problem here is that you haven't said what the problematic line of code is actually supposed to do. If you just want the scenario to compile, an easy fix would be to just delete that line of code. If you want suggestions that are actually helpful, you need to give more information about your problem!
FlyGirl95 FlyGirl95

2013/5/22

#
Ok so, I moved it into my act method and it works fine by creating the TitlePage object, thanks a bunch. So yes, I did want to simply create a new instance of TitlePage and call the imageTwo() method. I just had a hard time articulating it, but it all worked out. Thanks so much for your help, and when posting on here in the future, I will definitely include more information so people actually know what I'm talking about haha
You need to login to post a reply.