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

2015/4/4

URGENT HELP PLEASE!!! IMAGES ARE NULL!

Hades Hades

2015/4/4

#
So the idea here is to create an image switch when the object BODY touches the object BORDER. The object BORDER only changes images and remains changed until object BODY is no longer touching it. The strings are used in my world class to setup the images during spawning and essentially the soundfile is a sound that plays when the object BODY is overlapping object BORDER. The error message is this: java.lang.NullPointerException: Filename must not be null. at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:272) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:108) at greenfoot.Actor.setImage(Actor.java:415) at Border.switchImage(Border.java:61) at Border.act(Border.java:36) This is essentially pointing at the setImage(img2) and setImage(img1). Upon inspecting the object in the world, i can see that String img1 and img 2 are both null and I cannot understand how to fix this. Please help as I am only a beginner. WITHIN THE BORDER ACTOR
public Border(String soundFile, String img1, String img2)
    {
        sound = soundFile; 
        touchingBody = false;
        isDown = false;
        img1 = ("Block1.png");
        img2 = ("Block2.fw.png");
        
     
        
    }
   /**
     * Constantly Running all methods; includes keyboard check method
     */
   public void act()
    {
        checkBody();
        switchImage();
        
        
        
    }
   public void checkBody()
   { 
       
       if (getOneIntersectingObject(Body.class) == null)
        {
           touchingBody = false;
            
        } 
       else
        {
            touchingBody = true;
        }
   }
   
    public void switchImage()
   {
     if (touchingBody && !isDown)
     {
         
       
         setImage(img2);
         isDown = true;
         
         //play();
     }
     if (!touchingBody && isDown)
     {
         isDown = false;
         setImage(img1);
         
     }
     
   }
Hades Hades

2015/4/4

#
The object BODY freezes the moment it touches the object BORDER.
danpost danpost

2015/4/4

#
The problem is due to the fact that you are defining local variables of 'img1' and 'img2' in the constructor at line 1. Lines 6 and 7 are assigning values to the 'img1' and 'img2' local variables -- not to the fields that I would presume are defined earlier in the class code. You could rename the variables defined on line 1 to avoid any confusion; or you can instead use 'this.' before the name on lines 6 and 7 (to refer to the fields defined earlier in the class. My only question is this: why do you even have those local variables defined in the Border constructor (line 1) when you are just re-assigning their values anyway with literals (lines 6 and 7)? or, was that just you, blindly trying different things, hoping you could get it to work?
Hades Hades

2015/4/4

#
wow, thanks! I didn;t realize that defining them like that would cause such a confusion. I had them defined because my code was pretty long and i got lazy when it came to typing it out literally :) Thanks again!
You need to login to post a reply.