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

2015/10/2

Object Constructor

Vellyxenya Vellyxenya

2015/10/2

#
Hi, I'm trying to "do" something that i'm not sure if i am allowed to do or not... Here are some codes: This is my Drop class:
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
26
27
28
29
30
31
import greenfoot.*;
 
public class Drop extends SmoothMover
{
     
    public Drop()
    {
         
    }
     
    public Drop(int x, int y)
    {
        if(Greenfoot.getRandomNumber(100) < 50)
        {
            Credit credit = new Credit();
            System.out.println(credit);
            getWorld().addObject(credit,x ,y );
        }
        else if(Greenfoot.getRandomNumber(100) >= 50)
        {
            Heal heal = new Heal();
            getWorld().addObject(heal,x , y);
        }
    }
     
    public void act()
    {
 
    }
 
}
And there's 2 other classes: Heal and Credit that extend the Drop class (they're empty) And then I've a method in my Monster class as following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void takeDamageNew(int hurt, Class classe)
    {
        if(getWorld() !=null)
        {
            if(this.isTouching(classe))
            {
                Life = Life - hurt;
                if(Life <=0)
                {
                    getWorld().addObject(new Drop(this.getX(), this.getY()), this.getX(), this.getY());
                    getWorld().removeObject(this);
                    Game.killedEnemies++;
                }
            }
        }
    }
The code compiles, but when i kill a monster, there's a java error, which tell me null pointer exception... The idea behind this code is that when a monster dies, I want to have 50% chance to have a Heal and 50% chance to have a Credit Could you tell me what's going wrong in my code please?
danpost danpost

2015/10/2

#
What is the entire error message you are getting? What line of code is the problem and what class is it in?
Vellyxenya Vellyxenya

2015/10/2

#
It tells me that: java.lang.NullPointerException at Drop.<init>(Drop.java:17) at Monsters.takeDamageNew(Monsters.java:87) at Monsters.act(Monsters.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) and the 3 first ones are underlined and red The problem is in the line : getWorld().addObject(new Drop etc...) (Monsters.class) and also in the Drop.class( line: getWorld().addObject(credit,x ,y );)
danpost danpost

2015/10/2

#
Vellyxenya wrote...
The problem is in the line : getWorld().addObject(new Drop etc...) (Monsters.class) and also in the Drop.class( line: getWorld().addObject(credit,x ,y );)
The code in your Drop class constructor between lines 13 and 23, inclusive, should be in your World subclass where you have 'new Drop(< x >, < y >)', replacing it.
Vellyxenya Vellyxenya

2015/10/2

#
Hm okay then there's no way to add a Drop class that can be either a Heal.class or a Credit.class I see^^
danpost danpost

2015/10/2

#
Vellyxenya wrote...
Hm okay then there's no way to add a Drop class that can be either a Heal.class or a Credit.class I see^^
Well, you can. Just remove the 'public Drop(int x, int y)' constructor from the Drop class. If problems persist, show the code to one of the subclasses (Heal or Credit).
Vellyxenya Vellyxenya

2015/10/2

#
I'm not sure to get it. So I must remove the drop constructor, and then put the
1
2
3
4
5
6
7
8
9
10
11
if(Greenfoot.getRandomNumber(100) < 50)
        {
            Credit credit = new Credit();
            System.out.println(credit);
            getWorld().addObject(credit,x ,y );
        }
        else if(Greenfoot.getRandomNumber(100) >= 50)
        {
            Heal heal = new Heal();
            getWorld().addObject(heal,x , y);
        }
in the Monster class?
danpost danpost

2015/10/2

#
Vellyxenya wrote...
I'm not sure to get it. So I must remove the drop constructor, and then put the < Code Omitted > in the Monster class?
Yes to removing the constructor. No to putting the code in the Monster class -- unless it requires a monster in the world to spawn a drop. I suggested previously that you place it in your World subclass act method.
Vellyxenya Vellyxenya

2015/10/2

#
well yes it requires a monster, since the drops are made by the dying monsters^^
danpost danpost

2015/10/2

#
Vellyxenya wrote...
well yes it requires a monster, since the drops are made by the dying monsters^^
Then it would indeed be placed in the Monster class at the location where a monster is killed. Add the drop (of either type -- Heal or Credit) before removing the monster to avoid NullPointerException issues.
Vellyxenya Vellyxenya

2015/10/3

#
Ok, thank you very much :)
You need to login to post a reply.