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

2017/2/3

NullPointer Exception in GreenfootImage constructor with the new Greenfoot

Super_Hippo Super_Hippo

2017/2/3

#
Hello, I just updated to 3.1.0 and I noticed that the GreenfootImage class doesn't work as it did before although the class didn't change (or at least I don't see any difference in the API). I use this constructor:
public GreenfootImage(java.lang.String string, int size, Color foreground, Color background) //... foreground - the color of the text. Since Greenfoot 3.0.4, passing null will use black. background - the color of the image behind the text. Since Greenfoot 3.0.4, passing null with leave the background transparent.
If 'foreground' and/or 'background' is 'null', it will give a NullPointer Exception. This did not happen before 3.1.0. That's the code:
import greenfoot.*;

public class World1 extends World
{
    public World1()
    {    
        super(600, 400, 1);
        GreenfootImage img = new GreenfootImage("text", 40, null, null);
    }
}
And this is the error:
	java.lang.NullPointerException
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:171)
	at World1.<init>(World1.java:8)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at greenfoot.core.Simulation.newInstance(Simulation.java:617)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$7(WorldHandlerDelegateIDE.java:430)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502)
	at greenfoot.core.Simulation.maybePause(Simulation.java:305)
	at greenfoot.core.Simulation.runContent(Simulation.java:218)
	at greenfoot.core.Simulation.run(Simulation.java:211)
Nosson1459 Nosson1459

2017/2/3

#
Are you asking a question or just stating that this happens? (It probably happens because of the new Color class. It used to be that you put in java.awt.Color.CYAN or something like that but now you get colors from Greenfoot so the coloring is different even in the unedited GreenfootImage class)
Super_Hippo Super_Hippo

2017/2/3

#
This is not a question. This is like a bug report so it will hopefully be fixed.
Super_Hippo Super_Hippo

2017/2/5

#
I noticed that if you import the ScoreBoard class, you also instantly get an error on the following line because the greenfoot.Color class doesn't have the needed constructor. It's not that I don't know how to fix it, but it is a bit weird to get an error when importing a class.
getImage().drawImage(new GreenfootImage(text, height, color, new Color (0, true)), x, y);
Not exactly related to the first bug, but I don't want to create another discussion for each one.
danpost danpost

2017/2/5

#
Super_Hippo wrote...
I noticed that if you import the ScoreBoard class, you also instantly get an error on the following line because the greenfoot.Color class doesn't have the needed constructor. It's not that I don't know how to fix it, but it is a bit weird to get an error when importing a class.
getImage().drawImage(new GreenfootImage(text, height, color, new Color (0, true)), x, y);
There IS a Color constructor that matches. This is in the Color class API documentation:
public Color(int rgba, boolean hasalpha) Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. If the hasalpha argument is false, alpha is defaulted to 255. Parameters: rgba - the combined RGBA components hasalpha - true if the alpha bits are valid; false otherwise
Are you sure that the error is directly related to that part of the code -- 'new Color (0, true)'? That line is exactly the same in my downloaded copy of the ScoreBoard class and I do not get any errors when creating an instance of the class.
Super_Hippo Super_Hippo

2017/2/5

#
Note that I use version 3.1.0. With this version, the classes Color and Font were introduced in Greenfoot and it automatically removes any import statement which imports the 'java.awt...' versions. You can check out the new API here: https://www.greenfoot.org/files/javadoc/
Nosson1459 Nosson1459

2017/2/5

#
This new version of Greenfoot came around while you (danpost) were "away".
danpost danpost

2017/2/5

#
Super_Hippo wrote...
Note that I use version 3.1.0. With this version, the classes Color and Font were introduced in Greenfoot and it automatically removes any import statement which imports the 'java.awt...' versions. You can check out the new API here: https://www.greenfoot.org/files/javadoc/
Nosson1459 wrote...
This new version of Greenfoot came around while you (danpost) were "away".
I see. Personally, to put it bluntly, I do not care much for these changes/additions. I am happy with USB (standalone) v2.3.0 w/JDK. Interestingly, it is not included within the previous version uploads. Should be a link to: http://www.greenfoot.org/download/files/old/bluej310-greenfoot230-standalone.zip
Super_Hippo Super_Hippo

2017/2/5

#
Yeah, there is no problem with that. I usually update to the newest version when there is one and I think that at least this new version should be without such bugs. I have to say that I like it that the Color and Font classes are integrated into the greenfoot package because they are used very often (at least by me) and have to be imported a lot of times. Right now, the version is pretty unusable though because I don't want to change all occurrences of this GreenfootImage constructor (see problem 1, first post) from null to a transparent color. It shouldn't be hard to remove this error (since the same thing already worked in the previous version). For the Color constructor, either this constructor also has to be integrated or the class which can be imported should be changed. Probably, changing that line in the class (and maybe something in other classes, I didn't test if there are errors too) is easier because at least I never used that constructor myself.
danpost danpost

2017/2/5

#
@Super_Hippo, you could change the line in the source code so that it will import with the change. You should find it at: Greenfoot/lib/greenfoot/common/ScoreBoard.java
nccb nccb

2017/2/6

#
Super_Hippo: Thanks for pointing this out. You're right that it is a bug. I've filed it and fixed it but obviously it won't be fixed until 3.1.1. We don't currently have a timeline for that (we're focusing on a BlueJ release at the moment). As you say, in the mean time you can either change your code to not pass null, write a helper method to use instead, or roll back to 3.0.4 for the time being if this is a major issue across your code base.
You need to login to post a reply.