This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
SwarmDark
SwarmDark wrote ...

2017/8/10

How to fix java.lang.NullPointerException

SwarmDark SwarmDark

2017/8/10

#
Hello, I have been trying to code a reaction based game for greenfoot which revolves around pressing buttons to create objects, which can be converted into spells. I am trying to calculate how many actors of a particular class there are in the world. I have looked up how to "getObjects" and the how to find their ".size"; however when adding the actor, which calculates all these objects, into the world it gives me : java.lang.NullPointerException at Invoker.NumbersE(Invoker.java:89) at Invoker.<init>(Invoker.java:27)
int QuasNo = 0;
int ExortNo = 0;
int WexNo = 0;

    public Invoker()
    { 
      NumbersE();
      NumbersQ( );
       NumbersW();
       invoke();
        Cast();
      Animation();
      
     
    }
    
    
    public void invoke(){
     if(Greenfoot.isKeyDown("3")){
       
        if (QuasNo == 3){
           
        
        
        }
        if(ExortNo == 3){
        
        
        
        
        }
        if(WexNo ==3 ){
        
        
        
        }
        if(QuasNo == 2 && ExortNo == 1){
        
        
        }
         if(QuasNo == 2 && WexNo == 1){
            
            }
        
        if(ExortNo == 2 && WexNo == 1 ){
        
        }
         if(ExortNo == 2 && QuasNo == 1 ){
            
            }
        if(WexNo == 2 && ExortNo == 1 ){ 
        
        }
        if(WexNo == 2 && QuasNo == 1  ){
        
        }
        if(QuasNo == 1 && WexNo ==1 && ExortNo == 1){
        
        }
        
       
    
    }
    }

    
    
    public void NumbersE(){
         ExortNo = getWorld().getObjects(Exort.class).size();
        
      
    
    
    
    }
    public void NumbersQ(){
    
        QuasNo = getWorld().getObjects(Quas.class).size();
}
 public void NumbersW(){
    WexNo = getWorld().getObjects(Wex.class).size();
  
    
    }
danpost danpost

2017/8/10

#
Currently, the code from line 5 to line 15 is a constructor block, which is only executed when an Invoker object is created -- before the object being created is added into any world. However, the code within the constructor is stuff you want to happen while the actor is already in a world -- hence the error (getObjects is called on a reference that does not contain a world as getWorld returns a null value in the NumberE method that is called within the constructor -- see line 69). Just change 'Invoker' in line 5 to 'void act' so that the code is repeatedly executed while the actor is in the world instead of just once before.
danpost danpost

2017/8/10

#
All the if statements seem quite unnecessary. I am sure you can simplify them into one statement containing two conditions. Something like:
if (Greenfoot.isKeyDown("3") && QuasNo+ExortNo+WexNo == 3)
SwarmDark SwarmDark

2017/8/14

#
Thanks for all the help, this is my first time using Greenfoot and i am learning as I go. I will try to use your suggestion to simplify the code
You need to login to post a reply.