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

2021/3/11

Getting boolean from other class

liekevaniep01 liekevaniep01

2021/3/11

#
Hi again, I want to pass a boolean from 'Bij' class to the 'Tijd' class from Actor. I will explain the scenario. There is a 'Bij' and he gets points while playing. If the timer is on 0, I want to check wether the Bij has enough points to go to the next level, so I need the boolean if there are enough points from the class 'Bij' I have this, but it don't work. I get the error NullpointerException... In class Tijd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Bij koppelingBij;
 
public void checkEind(){
           if (resterendeTijd == 0) {
               if (koppelingBij.aantalPunten == false){
               World s = new Level1_Verlies(); // creates an AllstarsWorld object and names it 'a'
               s.addObject(new Bij_1(), 500, 500);
               Greenfoot.setWorld(s); // sets the new AllstarsWorld ('a') active
               World wereld = getWorld(); // as a World object, members of MyWorld are inaccessible
               Level1 level1 = (Level1) wereld; // the world cast as a MyWorld object
               level1.achtergrondMuziek.stop(); // stopping the music
               
               if (koppelingBij.aantalPunten == true){
               World s = new Level2(); // creates an AllstarsWorld object and names it 'a'
               s.addObject(new Bij_2(), 500, 500);
               Greenfoot.setWorld(s); // sets the new AllstarsWorld ('a') active
               World wereld = getWorld(); // as a World object, members of MyWorld are inaccessible
               Level1 level1 = (Level1) wereld; // the world cast as a MyWorld object
               level1.achtergrondMuziek.stop(); // stopping the music
               
           }
   }
   public void stelBijIn (Bij kb) {
       koppelingBij = kb;
   }
In class Bij
1
2
3
4
5
6
7
8
public boolean aantalPunten = false;
 public boolean aantalPunten ()
    {
        if (punten <= 9)
        return true;
        else
        return false;
    }
Can somebody help me?
danpost danpost

2021/3/11

#
Where and when does the stelBijIn method in Tijd class get called? Where does the aantalPunten field (line 1 in Bij class code above) get its value updated?
liekevaniep01 liekevaniep01

2021/3/12

#
Hello, I don't really know where stelBijIn method is being called. My teacher said that if you want a reference between classes that this is the way to do it... The aantalPunten field is getting updated when a bullet touches a object: Class Bij:
1
2
3
4
5
6
7
8
9
10
11
12
int punten = 0;
   public void addPoints (int p) {
       punten += p;
       updateText();
   }
  public boolean aantalPunten ()
   {
       if (punten <= 9)
       return true;
       else
       return false;
   }
Class bullet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Bij koppelingBij;
public void act()
    {
        move(3);
        checkRaken();
    }
public void checkRaken()
    {
        // als de schildpad aan de rand komt verdwijnt ie
        if (eindWereld())
        {
            getWorld().removeObject(this);
        }
        else
            if (isTouching (Bloem_blauw.class)){
                Bloem_blauw bloem_blauw = (Bloem_blauw)getOneIntersectingObject (Bloem_blauw.class);
                bloem_blauw.schadeErbij(1);
                World wereld = getWorld();
                wereld.removeObject(this);
[b]                koppelingBij.addPoints(1);[/b]
            
   public void stelBijIn (Bij kb) {
        koppelingBij = kb;
    }
liekevaniep01 liekevaniep01

2021/3/12

#
so the method addPoints in line 20 updates it (, doen't belong there, but I thought that it would make this line bold)
danpost danpost

2021/3/12

#
liekevaniep01 wrote...
I don't really know where stelBijIn method is being called. My teacher said that if you want a reference between classes that this is the way to do it...
That method is only the mechanism. You now need to use it to pass the reference to the Tijd object (in world, after creating both the Tijd and the Bij objects).
The aantalPunten field is getting updated when a bullet touches a object: << Codes Omitted >>
I do not see any line beginning with:
1
aantalPunten =
(other than the declaration/initialization line in Bij). As far as I can tell, its value remains false. However, let's deal with the NullPointerException first.
liekevaniep01 liekevaniep01

2021/3/12

#
Yes. I don't get why it is saying that, because when the timer hits zero the class 'Bee' and the class 'Tijd' itself aren't removed. The error comes on the line: if (koppelingBij.aantalPunten == false){} and if (koppelingBij.aantalPunten == true){} from the class Tijd, but maybe that's because of the missing of aantalPunten = ..
danpost danpost

2021/3/12

#
liekevaniep01 wrote...
but maybe that's because of the missing of aantalPunten = ..
No. It is because of the missing call to stelBijIn(Bij), which would assign an object to koppelingBij.
liekevaniep01 liekevaniep01

2021/3/12

#
Where can I use stelBijIn(Bij)?
danpost danpost

2021/3/12

#
liekevaniep01 wrote...
Where can I use stelBijIn(Bij)?
danpost wrote...
That method is only the mechanism. You now need to use it to pass the reference to the Bij object (in world, after creating both the Tijd and the Bij objects).
liekevaniep01 liekevaniep01

2021/3/12

#
Oké thank you
You need to login to post a reply.