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

2018/2/14

Calling an object from another object

1
2
danpost danpost

2018/2/15

#
Also, change the access modifier on lines 17 through 19 to 'protected' (on previous code post -- not what you just posted):
protected Color color1;
protected Color color2;
protected Color color3;

// or just
protected Color color1, color2, color3;
Xolkiyr Xolkiyr

2018/2/15

#
danpost wrote...
Also, change the access modifier on lines 17 through 19 to 'protected' (on previous code post -- not what you just posted):
protected Color color1;
protected Color color2;
protected Color color3;

// or just
protected Color color1, color2, color3;
Now I'm getting this:
java.lang.NullPointerException
	at greenfoot.GreenfootImage.setColorAt(GreenfootImage.java:525)
	at Ore.updateImage(Ore.java:135)
	at Ore.<init>(Ore.java:69)
	at Moon.<init>(Moon.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:607)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:441)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494)
	at greenfoot.core.Simulation.maybePause(Simulation.java:299)
	at greenfoot.core.Simulation.runContent(Simulation.java:212)
	at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2018/2/15

#
Did you adjust both of the Ore constructors to assign values to the fields while not creating local varaibles?
Xolkiyr Xolkiyr

2018/2/15

#
danpost wrote...
Did you adjust both of the Ore constructors to assign values to the fields while not creating local varaibles?
Yup, here's the code up to and including the constructor:
	private static final int SIZE = 50;
    private static final int HALFSIZE = SIZE / 2;
    protected Color color1;
    protected Color color2;
    protected Color color3;

	public String oreType;

	private static final Random randomizer = new Random();
    
    private float ore;
    
    private Label info;
    
    String oreDisp;

    /**
     * Create a vein of ore with an image depicting the amount.
     */
    public Ore(String oreType2, int oreUpdate)
    {
        ore = (Greenfoot.getRandomNumber(345)+oreUpdate)/100;  // number of chunks of ore in this vein.
		if("Iron".equals(oreType2)){
			Color color1 = new Color(160, 160, 160);
            Color color2 = new Color(80, 80, 80);
            Color color3 = new Color(10, 10, 10);
			oreType = "Iron";
		}else if("Copper".equals(oreType2)){
			Color color1 = new Color(124, 102, 22);
            Color color2 = new Color(62, 51, 11);
            Color color3 = new Color(10, 10, 10);
			oreType = "Copper";
		}
        updateImage();
    }
    
    /**
     * Create a vein of ore with an image depicting the amount. Default Amount
     */
    public Ore(String oreType2)
    {
        ore = (Greenfoot.getRandomNumber(345)+364789)/100;  // number of chunks of ore in this vein.
		if("Iron".equals(oreType2)){
			Color color1 = new Color(160, 160, 160);
            Color color2 = new Color(80, 80, 80);
            Color color3 = new Color(10, 10, 10);
			oreType = "Iron";
		}else if("Copper".equals(oreType2)){
			Color color1 = new Color(124, 102, 22);
            Color color2 = new Color(62, 51, 11);
            Color color3 = new Color(10, 10, 10);
			oreType = "Copper";
		}
        updateImage();
    }
danpost danpost

2018/2/15

#
Xolkiyr wrote...
danpost wrote...
Did you adjust both of the Ore constructors to assign values to the fields while not creating local varaibles?
Yup, here's the code up to and including the constructor: << Code Omitted >>
Sorry, you did not. No line between lines 20 and 55 should begin with 'Color'. Those lines should begin with 'color1', 'color2' or 'color3' so that you assign values to the fields declared on lines 3 through 5. 'Color' is a declaration of type for a new variable on those lines; but, you do not want new local variables -- you want to use the already declared instance fields. Btw, the 'protected' on lines 3 through 5 was just in case you were using subclasses. Since you are not, 'private' is okay.
Xolkiyr Xolkiyr

2018/2/15

#
danpost wrote...
Xolkiyr wrote...
danpost wrote...
Did you adjust both of the Ore constructors to assign values to the fields while not creating local varaibles?
Yup, here's the code up to and including the constructor: << Code Omitted >>
Sorry, you did not. No line between lines 20 and 55 should begin with 'Color'. Those lines should begin with 'color1', 'color2' or 'color3' so that you assign values to the fields declared on lines 3 through 5. 'Color' is a declaration of type for a new variable on those lines; but, you do not want new variables -- you want to use the already declared fields.
Alright, that fixed it. I just misunderstood whatcha meant. With all the help you've been giving me, I should be putting you under co-author.
You need to login to post a reply.
1
2