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

2015/1/26

HELP PLEASE

Sam146 Sam146

2015/1/26

#
I cannot tell you what the problem is but this is my code: public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse != null) { List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), TeamA1.class); for (Object object : objects) { if (object == this) { b spaceWorld = (b) getWorld(); Overallscore counter = spaceWorld.getOverallscore(); if(b == 0) { setImage(new GreenfootImage("?", 28, Color.BLACK, Color.WHITE)); b = 1; } if(Greenfoot.isKeyDown("0")){ setImage(new GreenfootImage("0", 28, Color.BLACK, Color.WHITE)); } if(Greenfoot.isKeyDown("1")){ setImage(new GreenfootImage("1", 28, Color.BLACK, Color.WHITE)); counter.TeamA11(); } if(Greenfoot.isKeyDown("2")){ setImage(new GreenfootImage("2", 28, Color.BLACK, Color.WHITE)); counter.TeamA12(); } if(Greenfoot.isKeyDown("3")){ setImage(new GreenfootImage("3", 28, Color.BLACK, Color.WHITE)); counter.TeamA13(); } if(Greenfoot.isKeyDown("4")){ setImage(new GreenfootImage("4", 28, Color.BLACK, Color.WHITE)); counter.TeamA14(); } if(Greenfoot.isKeyDown("5")){ setImage(new GreenfootImage("5", 28, Color.BLACK, Color.WHITE)); counter.TeamA15(); } if(Greenfoot.isKeyDown("6")){ setImage(new GreenfootImage("6", 28, Color.BLACK, Color.WHITE)); counter.TeamA16(); } if(Greenfoot.isKeyDown("7")){ setImage(new GreenfootImage("7", 28, Color.BLACK, Color.WHITE)); counter.TeamA17(); } if(Greenfoot.isKeyDown("8")){ setImage(new GreenfootImage("8", 28, Color.BLACK, Color.WHITE)); counter.TeamA18(); } if(Greenfoot.isKeyDown("9")){ setImage(new GreenfootImage("9", 28, Color.BLACK, Color.WHITE)); counter.TeamA19(); } } } } in my world class i have private Overallscore theCounter; public Overallscore getOverallscore() { return theCounter; } in the next class there is: public void TeamA11() { Am1 = 1; } public void TeamA12() { Am1 = 2; } public void TeamA13() { Am1 = 3; } public void TeamA14() { Am1 = 4; } public void TeamA15() { Am1 = 5; } public void TeamA16() { Am1 = 6; } public void TeamA17() { Am1 = 7; } public void TeamA18() { Am1 = 8; } public void TeamA19() { Am1 = 9; } the error is java.lang.NullPointerException at TeamA1.act(TeamA1.java:93) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205 please help
Sam146 Sam146

2015/1/26

#
by the way Am1 is an integer for a comparison
Sam146 Sam146

2015/1/26

#
HELP!!!
danpost danpost

2015/1/26

#
All one can tell is at the time this particular error message was created, in your act method at line 93 in your TeamA1 class, you were trying to access a method or field on a null reference. This information was ascertained by this line in the error trace:
at TeamA1.act(TeamA1.java:93)
Clear your terminal, recreate the error and post the error trace along with the entire class code using the 'code' link below the reply box to insert the code into your post.
Sam146 Sam146

2015/1/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;

/**
 * Write a description of class TeamA1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TeamA1 extends J
{
   int b1 = 0;
     
    /**
     * Act - do whatever the TeamA1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       MouseInfo mouse = Greenfoot.getMouseInfo();
       
      
               if (mouse != null)
           {
                List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), TeamA1.class);
            for (Object object : objects)
            {
                if (object == this)
                {
                   
                b spaceWorld = (b) getWorld(); 
                Overallscore counter = spaceWorld.getOverallscore();
            

               if(b1 == 0)
               {
               
               setImage(new GreenfootImage("?", 28, Color.BLACK, Color.WHITE)); 
               b1 = 1;
            }
               if(Greenfoot.isKeyDown("0")){
           setImage(new GreenfootImage("0", 28, Color.BLACK, Color.WHITE)); 
          
           
           
        }
        if(Greenfoot.isKeyDown("1")){
           setImage(new GreenfootImage("1", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA11();
          
        }
         if(Greenfoot.isKeyDown("2")){
           setImage(new GreenfootImage("2", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA12();
           
        }
         if(Greenfoot.isKeyDown("3")){
           setImage(new GreenfootImage("3", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA13();
           
        }
         if(Greenfoot.isKeyDown("4")){
           setImage(new GreenfootImage("4", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA14();
           
           
        }
         if(Greenfoot.isKeyDown("5")){
           setImage(new GreenfootImage("5", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA15();
           
        }
         if(Greenfoot.isKeyDown("6")){
           setImage(new GreenfootImage("6", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA16();
           
        }
         if(Greenfoot.isKeyDown("7")){
           setImage(new GreenfootImage("7", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA17();
           
        }
         if(Greenfoot.isKeyDown("8")){
           setImage(new GreenfootImage("8", 28, Color.BLACK, Color.WHITE)); 
           counter.TeamA18();
           
        }
         if(Greenfoot.isKeyDown("9")){
           setImage(new GreenfootImage("9", 28, Color.BLACK, Color.WHITE)); 
           
           
           counter.TeamA19();
           
        }
       }
      }
      }    
    } 
}
this time it occured else where java.lang.NullPointerException at TeamA1.act(TeamA1.java:60) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
Sam146 Sam146

2015/1/26

#
the error always occurs in the lines "counter.TeamA1."
davmac davmac

2015/1/26

#
Please fix your indentation before you post code.
Sam146 Sam146

2015/1/26

#
what is wrong with it?
Sam146 Sam146

2015/1/26

#
sorry if it is hard to read
fejfo fejfo

2015/1/26

#
just do Ctrl + Shift + i
davmac davmac

2015/1/26

#
what is wrong with it?
Read the link I gave you. As fejfo suggests, you can usually fix it just by pressing ctrl + shift + I.
danpost danpost

2015/1/26

#
It appears your 'theCounter' field in your world class was not assigned an Overallscore object.
You need to login to post a reply.