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

2016/10/19

nullPointerException

Nosson1459 Nosson1459

2016/10/19

#
HuuuuuuuuuuuuuuuuuuuuuH???????????????????????
Nosson1459 Nosson1459

2016/10/19

#
how does a nullPointerExeption work wat is it and how would i fix it, it keeps on popping up by booleans
danpost danpost

2016/10/19

#
A nullPointerException is usually due to trying to call a method on an object reference that does not point to any object. For example:
Actor actor = null;
actor.setRotation(0);
Because 'actor' is 'null', there is no rotation to set; so, a nullPointerException is thrown.
Actor actor = getOneIntersectingObject(Actor.class);
if (actor.getRotation() == 180)
This may work -- sometimes; but, not all the time. As long as there is an intersecting actor, it will work. Once no actor intersect, a nullPointerException will be thrown and the scenario will stop. If there is a question as to whether a field or variable may be 'null', check it first:
Actor actor = getOneIntersectingObject(Actor.class);
if (actor != null && actor.getRotation() == 180)
Nosson1459 Nosson1459

2016/10/19

#
y would this throw one
public boolean move=false;
{
        if(getX()>=375)
        scene.move=true;
    }
public void act() 
    {
        move();
    }    
    private void move()
    {
        if(Greenfoot.isKeyDown("Right")&&scene.move==true)
        setLocation(getX()-7,getY());   
    }
it throws one if getX() is < 375 but if its > it doesnt even run it pauses as soon as i press run
danpost danpost

2016/10/19

#
You are showing three sets of code here -- and it is difficult to determine where the code actually is within the class (particularly the first two sets of code). Please show the entire class together.
Nosson1459 Nosson1459

2016/10/19

#
the 1st set of code is in the world the 2nd is in 1 actor the 3rd is in another actor i should put the full coding to all 3
Nosson1459 Nosson1459

2016/10/19

#
this is the world:
public int turnLeft;
    public int turnRight;
    public int lives=2;
    private int width=750;
    private int height=500;
    public int score=0;
    public int coins=0;
    public int level=1;
    public int addObjects;
    public boolean move=false;
    public Scene()
    {    
        super(750, 500, 1);    // define size and resolution
        move=false;
        setPaintOrder(GameOver.class,Coins.class,Level.class,Life.class,Score.class,Bar.class,Pengu.class,Snake.class,SnakeD.class,Cloud.class);
        addObject ( new CliffA(), 50, 405);
        addObject ( new cliffC(), 650, 405); 
        addObject ( new Cloud(), 369, 315 );
        addObject ( new Pengu(), 46, 291 );
        addObject(new Bar(),375,18);
        addObject(new SnakeD(),650,302);
        addObject(new Coins("Coins: "),685,18);
        addObject(new Life("Lives: "),55,18);
        addObject(new Score("Score: "),490,18);
        addObject(new Level("Level: "),260,18);
        addObject(new Block(),141,270);
        addObject(new Block(),141,310);
        addObject(new Block(),181,310);
        addObject(new Block(),221,310);
        addObject(new Block(),261,310);
        addObject(new Block(),261,270);
        addObject(new Snake(),201,275);
    }
    public void act()
    {
        if(lives<0)
        {
            addObject(new GameOver(),375,250);
            if(score==0)
            {
                showText(""+score,365,210);
            }
            else if(score>99&&score<1000)
            {
                showText(""+score,375,210);
            }
            else if(score>999&&score<10000)
            {
                showText(""+score,380,210);
            }
            else if(score>9999&&score<100000)
            {
                showText(""+score,385,210);
            }
            else if(score>99999)
            {
                showText(""+score,390,210);
            }
            Greenfoot.stop();
        }
    }
    public void reset()
    {
        List<Platform> platform=getObjects(Platform.class);
        List<Box> box=getObjects(Box.class);
        List<Pengu> pengu=getObjects(Pengu.class);
        List<cliffC> cliffC=getObjects(cliffC.class);
        List<CliffA> cliffA=getObjects(CliffA.class);
        List<Cloud> cloud=getObjects(Cloud.class);
        List<Monster> monster=getObjects(Monster.class);
        List<Block> block=getObjects(Block.class);
        removeObjects(platform);
        removeObjects(box);
        removeObjects(pengu);
        removeObjects(cliffC);
        removeObjects(cliffA);
        removeObjects(cloud);
        removeObjects(monster);
        removeObjects(block);
    }
    public void level1()
    {
        addObject ( new CliffA(), 50, 405);
        addObject ( new cliffC(), 650, 405);
        
        addObject ( new Cloud(), 369, 315 );
        
        addObject ( new Pengu(), 46, 291 );
        
        addObject(new SnakeD(),650,302);
        addObject(new Block(),141,270);
        addObject(new Block(),141,310);
        addObject(new Block(),181,310);
        addObject(new Block(),221,310);
        addObject(new Block(),261,310);
        addObject(new Block(),261,270);
        addObject(new Snake(),201,275);
    }
    public void level2()
    {
    }
    public void test()
    {
        addObject(new Help(),375,250);
    }
this is class pengu:
private static final int jumpStrength = 16;
    private Scene scene=(Scene)getWorld();
    public void act() 
    {
        coins();
        checkKeys();        
        checkFall();
        changeMove();
    }
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("pengu-left2.png");
            if(!touchingLeftSide()||!touchingLeftSide2()||!touchingLeftSide3())
               moveLeft();
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setImage("pengu-right2.png");
            if(!touchingRightSide()||!touchingRightSide2()||!touchingRightSide3())
               moveRight();
        }
        if (Greenfoot.isKeyDown("space") )
        {
            if (onGround())
                jump();
        }
    }    
    
    private void jump()
    {
        setVSpeed(-jumpStrength);
        fall();
    }
    
    private void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
        }
        else {
            fall();
        }
        
    }
    private void coins()
    {
        Scene scene=(Scene)getWorld();
        Coin coin=(Coin)getOneIntersectingObject(Coin.class);
        if(coin!=null)
        {
            scene.removeObject(coin);
            scene.score=scene.score+100;
            scene.coins=scene.coins+1;
        }
    }
    private void changeMove()
    {
        if(getX()>=375)
        scene.move=true;
    }
this is class cliff:
public void act() 
    {
        move();
    }    
    private void move()
    {
        if(Greenfoot.isKeyDown("Right")&&scene.move==true)
        setLocation(getX()-7,getY());   
    }
danpost danpost

2016/10/19

#
Line 2 of the pengu class will set the value of 'scene' to 'null'. It is assigned when the pengu object is created and before it is added into any world. Remove that line and adjust line 62 (take notes for the 'changeMove' method on how the 'coins' method is coded -- see line 50). Again, that is not the complete code for the cliff. You should show everything -- including import lines (Ctrl-A/Ctrl-C followed by Ctrl-V here).
Nosson1459 Nosson1459

2016/10/19

#
thnx
You need to login to post a reply.