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

2015/7/6

NullPointerException

ElZombieIsra ElZombieIsra

2015/7/6

#
Hello, i'm creating a game and I have a trouble when I try to add score into my counter. The code that have troubles.
public class Disparo extends Actor
{
    public boolean soldadoIZ=true;
    public boolean soldadoDe=true;
    public void act() 
    {
        Actor enemigo= getOneIntersectingObject(Enemigo1.class);
        Actor enemigo2=getOneIntersectingObject(Enemigo2.class);
        Andersen1 heroe=new Andersen1();
        move(15);   
        if (alLimite()){
            quitar();
        }
        
        if(enemigo != null) 
        {
            getWorld().removeObject(this);
            soldadoDe=false;
            ((Contador) getWorld().getObjects(Contador.class).get(0)).addScore();
            //new Polonia2().juego();
        }
              
        if(enemigo2 != null){
           getWorld().removeObject(this);
           soldadoIZ=false;
           
        }
    } 
    public boolean alLimite()
    {       
        if(getX() <= 25 || getX() >= getWorld().getWidth() - 25){
            return true;
        }
        return false;
    }
    public void quitar()
    {
        getWorld().removeObject(this);
    }
    public void masEnemigosDe(){
        getWorld().addObject(new Enemigo1(),getWorld().getWidth()-20,520);
        getWorld().addObject(new Enemigo1(),getWorld().getWidth()-5,520);
    }
    public void masEnemigosIz(){
        getWorld().addObject(new Enemigo2(),20,520);
        getWorld().addObject(new Enemigo2(),5,520);
    }
}
The warning message
java.lang.NullPointerException
	at Disparo.act(Disparo.java:27)
I need help, please. I'm getting stressed.
davmac davmac

2015/7/6

#
You haven't posted all the class code, so I can't see which line is line 27 (the line the exception happens on). That makes it harder to help you. However I can see some problems with your code:
  • You call 'getWorld()' after removing 'this' from the world in several places. Once 'this' has been removed from the world, getWorld() will return null. You can fix it by checking that getWorld() is not null before you try to use it - if (getWorld() != null) ...
  • On line 19 you cast the world to a 'Contador' and then try to get objects inside it which are of class 'Contador'. That doesn't make sense. If Contador is a world class, then the world cannot contain Contador objects.
ElZombieIsra ElZombieIsra

2015/7/6

#
Ah, sorry. First: Here's the complete code.
import greenfoot.*;

/**
 * Write a description of class Disparo here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Disparo extends Actor
{
    public boolean soldadoIZ=true;
    public boolean soldadoDe=true;
    public void act() 
    {
        Actor enemigo= getOneIntersectingObject(Enemigo1.class);
        Actor enemigo2=getOneIntersectingObject(Enemigo2.class);
        Andersen1 heroe=new Andersen1();
        move(15);   
        if (alLimite()){
            quitar();
        }
        
        if(enemigo != null) 
        {
            getWorld().removeObject(this);
            soldadoDe=false;
        }
              
        if(enemigo2 != null){
           getWorld().removeObject(this);
           soldadoIZ=false;
           
        }
    } 
    public boolean alLimite()
    {       
        if(getX() <= 25 || getX() >= getWorld().getWidth() - 25){
            return true;
        }
        return false;
    }
    public void quitar()
    {
        getWorld().removeObject(this);
    }
    public void masEnemigosDe(){
        getWorld().addObject(new Enemigo1(),getWorld().getWidth()-20,520);
        getWorld().addObject(new Enemigo1(),getWorld().getWidth()-5,520);
    }
    public void masEnemigosIz(){
        getWorld().addObject(new Enemigo2(),20,520);
        getWorld().addObject(new Enemigo2(),5,520);
    }
}
Second: Can you help me to read an archive called "datos.dat"? I want to read the object inside the file, then transform that object into int. Here's the code that i'm implementing:
import greenfoot.*;
import java.awt.Color;
import java.io.*;
import javax.swing.JOptionPane;

/**
 * Write a description of class Contador here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Contador extends Actor
{
    int score = 0;
    int maxScore;
    boolean level1=true;
    boolean level2=false;
    boolean level3=false;
    boolean level4=false;
    boolean level5=false;
    String datos;
    public void act() {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
        score++;
        
        if(score>1000&&level1==true){
            level1=false;
            level2=true;
            getWorld().addObject(new Enemigo1(),getWorld().getWidth()-5,520);
            getWorld().addObject(new Enemigo2(),5,520);
        }
        if(score>2000&&level2==true){
            level2=false;
            level3=true;
            getWorld().addObject(new Enemigo1(),getWorld().getWidth()-5,520);
            getWorld().addObject(new Enemigo2(),5,520);
        }
        if(score>3000&&level3==true){
            level3=false;
            level4=true;
            getWorld().addObject(new Enemigo1(),getWorld().getWidth()-5,520);
            getWorld().addObject(new Enemigo2(),5,520);
        }
        if(score>4000&&level4==true){
            level4=false;
            level5=true;
            getWorld().addObject(new Enemigo1(),getWorld().getWidth()-5,520);
            getWorld().addObject(new Enemigo2(),5,520);
        }
        if(score!=0){
            try{
                ObjectOutputStream salida=new ObjectOutputStream(new FileOutputStream("datos.dat"));
                
                if(score>maxScore){
                    maxScore=score;
           
                    datos =String.valueOf(score);
                    salida.writeObject(datos);
                    salida.close();
                }
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
            //here i want to set maxScore with the object inside the file
        }
    }    
}
davmac davmac

2015/7/6

#
First: the code you posted now is different from the code you posted last time, and so I still can't tell what line the error occurs on. It still has the same problems that I told you about before, though, and I think it's likely that these problems that are what is causing the exception. You should try to address those problems, and then maybe you will no longer get an exception. Second: I suggest you post a second discussion topic for this second issue.
ElZombieIsra ElZombieIsra

2015/7/6

#
Thanks
You need to login to post a reply.