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

2018/3/22

loop-practice throws a java.lang.NullPointerException

paubel paubel

2018/3/22

#
from Chapter 9 in 'Introduction to Programming with Greenfoot SE : loop-practice The program throws a NullPointerException the first time I start the program. Any ideas?
danpost danpost

2018/3/22

#
Please show codes and error message.
paubel paubel

2018/3/22

#
The error shows up directly. I have not changed anytning ----- Error code java.lang.NullPointerException at greenfoot.GreenfootImage.<init>(GreenfootImage.java:171) at Text.<init>(Text.java:14) at ChalkBoard.write(ChalkBoard.java:56) at ChalkBoard.write(ChalkBoard.java:40) at ChalkBoard.practice(ChalkBoard.java:30) at ChalkBoard.<init>(ChalkBoard.java:22) 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import greenfoot.*;
 
/**
 * A board to scribble on.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class ChalkBoard extends World
{
    // Where to write the next text:
    private int x;
    private int y;
     
    /**
     * Create a new chalk board.
     */
    public ChalkBoard()
    {
        super(800, 600, 1);
        clear();
        practice();
    }
     
    /**
     * This is the method for you to practice. Add your code here.
     */
    public void practice()
    {
        write(7);  // an example of writing a number
         
        // Replace this with your own code
    }
     
    /**
     * Write a number onto the board.
     */
    public void write(int number)
    {
        write(String.valueOf(number));
    }
     
    /**
     * Write a character onto the board.
     */
    public void write(char character)
    {
        write(String.valueOf(character));
    }
     
    /**
     * Write some text onto the board.
     */
    public void write(String text)
    {
        addObject(new Text(text), x, y);
        x += 120;
        if (x > getWidth()-100) {
            x = 100;
            y += 80;
        }
    }
     
    /**
     * Wipe the board.
     */
    public void clear()
    {
        removeObjects(getObjects(Text.class));
        x = 100;
        y = 100;
    }
}
-----
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
 
 
/**
 * An object showing some text.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class Text extends Actor
{
    public Text(String text)
    {
        GreenfootImage img = new GreenfootImage(text, 64, Color.WHITE, null);
        setImage(img);
    }
}
paubel paubel

2018/3/22

#
I think I found it. In the class Text I changed last parameter from null to Color.BLACK. According to the API https://www.greenfoot.org/files/javadoc/greenfoot/GreenfootImage.html The constructor should be able to handle null since 3.0.4. (background - the color of the image behind the text. Since Greenfoot 3.0.4, passing null with leave the background transparent.) I'm running 3.1.0. Or is there something else wrong? public GreenfootImage(java.lang.String string, int size, Color foreground, Color background)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * An object showing some text.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class Text extends Actor
{
    public Text(String text)
    {
        GreenfootImage img = new GreenfootImage(text, 64, Color.WHITE, Color.BLACK); //changed last parameter from null to Color.BLACK
        setImage(img);
    }
}
Super_Hippo Super_Hippo

2018/3/22

#
This error is already known. I think it did work in 3.0.4, but in 3.1.0, it doesn't work anymore, but it will probably be fixed with the next release again.
danpost danpost

2018/3/22

#
For a transparent background, you can change the line to this:
1
GreenfootImage img = new GreenfootImage(text, 64, Color.WHITE, new Color(0, 0, 0, 0));
You need to login to post a reply.