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

2014/3/24

static / non-static confusion

SandDealer SandDealer

2014/3/24

#
i'm writing a method that sets all objects of that class to the world.. problem is that i have 4 pretty mutch same classes "levels" that i want to add to the world randomly.. so i'm using a class controler to determine what class will be added.. sth like this..
void controler(){
     if (izbor.length() == 0) { izbor = "0123"; } 
  // variable "izbor" is declared as a String, earlyer..
     else {
       int a = Greenfoot.getRandomNumber(izbor.length());
       String b = ""+izbor.charAt(a);
       int c = Integer.parseInt(b);
       switch (c) {
        case 0 : izbor = izbor.replaceAll("0","");
                 mapa1.set();
                 break;
        case 1 : izbor = izbor.replaceAll("1","");
                 mapa2.set();
                 break;
        case 2 : izbor = izbor.replaceAll("2","");
                 mapa3.set();
                 break;
        case 3 : izbor = izbor.replaceAll("3","");
                 mapa4.set();
                 break;
        }  }  }
and my set() method look's like this..
static void set(){
    getWorld().removeObjects(getWorld().getObjects(Actor.class));
    put1 p1 = new put1();
    getWorld().addObject(p1, 250, 250);    
    }
So im getting this error telling me that getWorld() method can't be referenced from a static context. But if I make my set() method just void, I cant reference it from main - controler method. How can i solve this confusion? can anyone help? :) **This is a test code, just to make my levels appear randomly, this is my only problem left, so I'll be extra happy if smvn helps :)
SandDealer SandDealer

2014/3/24

#
I did my research, so I found that in order to call a non-static method, you must call the method on an instance of an object. So I added... switch (c) { case 0 : izbor = izbor.replaceAll("0",""); mapa1 lvl1 = new mapa1(); lvl1.postavi(); break; ....... but it's error again!! "java.lang.NullPointerException" what am I doing wrong ?
SandDealer SandDealer

2014/3/25

#
also i have "start button" class that looks like this public void act() { if (Greenfoot.mouseClicked(this)){ controler ctrl = new controler(); ctrl.izaberiMapu(); } } How should those constructors look like? someone pls....
danpost danpost

2014/3/25

#
Can you post the entire controler class (knowing the kind of class, what fields you are using, etc. may help in determining how to proceed). Also, there may be a better way to process all this, but with the limited amount of information, it is hard to say. You did give a lot of fragments; but to get context, more info is needed. btw, you wrote 'how should these constructors look like'; but, I do not see any constructors, just methods.
SandDealer SandDealer

2014/3/25

#
Take a look at my scenario pls. That is a game that I've been making.. I showed it to my mentor so he said that its all ok but game has no sense. So he told me to make another 3 maps like this, that should be executed randomly. So first level should be each of this maps, randomly, with 2 mins limit for each, second 1:30 min, third 1:00 .... My code there is pretty confusing, so I've started with new app. My first goal is to make my maps add randomly to the world. You dont see any constructors because there is no any. I have to make them because of that NullPointerExc, but i dont know how to... Here is the whole controler..... import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class kontroler here. * * @author (your name) * @version (a version number or a date) */ public class controler extends Actor { String izbor = "0123"; public void act() { } void klik(){ if (Greenfoot.mouseClicked(this)){ setRotation(getRotation() + 90); } if (getRotation() == 360){ setRotation(0); } } void chooseAmap(){ if (izbor.length() == 0) { izbor = "0123"; } else { int a = Greenfoot.getRandomNumber(izbor.length()); String b = ""+izbor.charAt(a); int c = Integer.parseInt(b); switch (c) { case 0 : izbor = izbor.replaceAll("0",""); mapa1 lvl1 = new mapa1(); lvl1.set(); break; case 1 : izbor = izbor.replaceAll("1",""); mapa2 lvl2 = new mapa2(); lvl2.set(); break; case 2 : izbor = izbor.replaceAll("2",""); mapa3 lvl3 = new mapa3(); lvl3.set(); break; case 3 : izbor = izbor.replaceAll("3",""); mapa4 lvl4 = new mapa4(); lvl4.set(); break; } } } }
davmac davmac

2014/3/25

#
Where does chooseAmap() get called from? getWorld() returns the current world that an actor is in (not neccessarily the world that is set as the current world!). If you create a new actor and do not add it into the world, then its getWorld() method will return null. That is why you are getting a NullPointerException. I think the best solution is to pass a reference to the world to the "set" method in the map classes as a parameter. So your set method should look like:
static void set(World theWorld)
{
    // ...
}
One more thing: words in class names in Java should begin with a capital . So your 'mapa1' should be 'MapA1' and so on.
SandDealer SandDealer

2014/3/26

#
It is called from "start button-s" act() .. I've just translated it wrong, cuz im writing it on Serbian, and translating those parts to English so it would be easyer for u to get it.. Guess I've missed that part. So "start button-s" act () looks like this: public void act() { if (Greenfoot.mouseClicked(this)){ controler ctrl = new controler(); ctrl.choseAmap(); } } Allright so your sugestion is to make more wold classes (4 in this case) and therefore "Set" method should choose what world is active at the moment, am I right? I think that this would solve me this static/non-static problem... But a NullPointerException gives an error on these lines of code " mapa1 lvl1 = new mapa1(); ".. So I guess that 'mapa1' need's a proper constructor, to add some value to 'lvl1'.. Please correct me if I'm wrong, and thank you for your time. Your advices helped me a lot!
danpost danpost

2014/3/27

#
SandDealer wrote...
Allright so your sugestion is to make more wold classes (4 in this case) and therefore "Set" method should choose what world is active at the moment, am I right?
I do not see where anyone suggested that. davmac suggested that you pass a reference of the world through the to the 'set' method. This would mean your controler object needs to be in the world or a reference to the world will need to be passed either to the conroler object itself when you create it or to the 'createAmap' method when you call it. I suggest you make the 'izbor' string 'static' so that it is not "0123" every time you create a new controler object to change levels. As far as the NullPointerException, please show the entire trace when you get a message like this. It makes it a whole lot easier to locate the problem.
You need to login to post a reply.