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

2012/10/14

HELP!

Game/maniac Game/maniac

2012/10/14

#
how do I fix this? java.lang.NullPointerException at Worldy.StareX(Worldy.java:96) at enemyTankHead.Controll(enemyTankHead.java:28) at enemyTankHead.act(enemyTankHead.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) this is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
/**
 * Write a description of class Worldy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Worldy extends Scrollworld implements ActionListener
{
    public static final Font MED_FONT = new Font("SansSerif", Font.BOLD, 50);
    private Button startBtn;
    private Button onOffBtn;
    private boolean enabled = true;
    private Border redBorder = new LineBorder(Color.RED, 3);
    private Border blueBorder = new LineBorder(Color.BLUE, 3);
    private int L=0;
    private int T=0;
    private int X;
    private int Y;
    public tankBase tank;
    public enemyTankHead tH;
    /**
     * Constructor for objects of class Worldy.
     * 
     */
    public Worldy()
    {
        super(2500, 2500, 600, 600, 1250, 1250, 1);
        Level(1);
    }
    public void Level(int l)
    {
        if(l==1){
            menuSetup();
        }
        if(l==2){
            removeObject(startBtn);
            newBackground("rivets.jpg");
            addObject(new tankBase(),300,300);
            addObject(new tankHead(),300,300);
        }
        L=l;
    }
    public void act()
    {
        if(L==2){
            if(T==0){
                X=Greenfoot.getRandomNumber(2416)+42;
                Y=Greenfoot.getRandomNumber(2416)+42;
                addObject(new enemyTankBase(),X,Y);
                addObject(new enemyTankHead(),X,Y);
                T=2000;
            }else{
                T--;
            }
        }
    }
    public void done(int level)
    {
        Level(level+1);
    }
    public void menuSetup()
    {
        newBackground("Wall.jpg");
        startBtn = new Button(new GreenfootImage("roundedbtn-up.png"),
            new GreenfootImage("roundedbtn-down.png"),
            new GreenfootImage("roundedbtn-hover.png"));
        startBtn.setForeground(Color.BLACK);
        startBtn.setText("Start");
        startBtn.setSize(new Dimension(200, 65));
        startBtn.setFont(MED_FONT);
        startBtn.setID(1008);
        startBtn.addActionListener(this);
        addObject(startBtn, 300, 300);
    }
    public void actionPerformed(GUIComponent c) {
        if (c != onOffBtn) {
            done(1);
        } else {
            enabled = !enabled;
            
            startBtn.setEnabled(enabled);
            
            if (enabled) {
                c.setText("Disable\nComponents");
            } else {
                c.setText("Enable\nComponents");
            }
        }
    }
    public int StareX()
    {
        return tank.getX();
    }
    public int StareY()
    {
        return tank.getY();
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class tankHead here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class enemyTankHead extends Mover
{
    public int shotTimer=0;
    private GreenfootSound rotate;
    private GreenfootSound shoot;
    public enemyTankHead()
    {
        rotate = new GreenfootSound("Motor.wav");
        rotate.setVolume(70);
        shoot = new GreenfootSound("Bang.wav");
        shoot.setVolume(80);
    }
    public void act() 
    {
        Controll();
    }
    public void Controll()
    {
        Worldy worldy = (Worldy) getWorld();
        turnTowards(worldy.StareX(),worldy.StareY());
        if(Greenfoot.isKeyDown("left")){
            rotate.play();
        }else if(Greenfoot.isKeyDown("right")){
            rotate.play();
        }else{
            rotate.pause();
        }
        if (shotTimer > 0) {
            shotTimer--;
        }
        else if(Greenfoot.isKeyDown("space"))
        {
            Fire();
            shotTimer = 30;
            shoot.play();
        }
        Actor base;
        base = getOneIntersectingObject (enemyTankBase.class);
        setLocation(base.getX(),base.getY());
    }
    public void Fire()
    {
        Bullet b = new Bullet();  
        getWorld().addObject(b,getX(),getY());
        b.getImage().scale(5,5);
        b.setRotation(getRotation());  
        b.move(50);
    }
}
SPower SPower

2012/10/14

#
You got a null pointer exception: java.lang.NullPointerException which means you're using an object which is null. It happens at line 96 in the class Worldy at Worldy.StareX(Worldy.java:96) in the method stareX return tank.getX(); you're using the tank object, which is null at that point. You first need to set it. PS. are you using my Scrolling World scenario? If yes, thanks!
Game/maniac Game/maniac

2012/10/14

#
SPower wrote...
You got a null pointer exception: java.lang.NullPointerException which means you're using an object which is null. It happens at line 96 in the class Worldy at Worldy.StareX(Worldy.java:96) in the method stareX return tank.getX(); you're using the tank object, which is null at that point. You first need to set it. PS. are you using my Scrolling World scenario? If yes, thanks!
Your welcome I will be putting your name in the description when the game is playable
SPower SPower

2012/10/14

#
Thank you! And if you like the engine: I really appreciate it when you click like :)
Game/maniac Game/maniac

2012/10/14

#
I've liked it now
Game/maniac Game/maniac

2012/10/14

#
I am still having trouble with the nullpointerexception any ideas?
SPower SPower

2012/10/14

#
Have you set the tank?
// example:
tank = ...;
Game/maniac Game/maniac

2012/10/14

#
what do you mean by set the tank?
SPower SPower

2012/10/14

#
Well, I gave you an example, but I'll explain further. An object can be null, which can't be used. If you wanna use it (like in your StareX method), tanks furst musn't be null anymore. Setting the tank, is doing this:
tank = .....;
where the dots need to be some other tank object.
Game/maniac Game/maniac

2012/10/14

#
thank you
Game/maniac Game/maniac

2012/10/14

#
its working now
SPower SPower

2012/10/14

#
Great!
Game/maniac Game/maniac

2012/10/15

#
Sorry I found out I am actually using Busch2207's scroll world but your name will still be in the credit section for helping me with this bug
You need to login to post a reply.