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

2017/4/19

nullPointer

Nosson1459 Nosson1459

2017/4/19

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ocean extends World
{
    private int counter=39;
    private int counter2=650;
    private int counter3=0;
    private int ident=0;
    private Oxygen[] oxygen=new Oxygen[50];
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Ocean()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(640, 600, 1,false); 
        getBackground().setColor(new Color(0,186,255));
        getBackground().fillRect(0,(getHeight()/3)+13,getWidth(),getHeight());
        getBackground().setColor(new Color(133,228,224));
        getBackground().fillRect(0,0,getWidth(),(getHeight()/3)-12);
        setPaintOrder(Dolphin.class);
        drawBackgroundPics();
        setup();
    }

    /**
     * The main method.
     */
    public void act()
    {
        oxygen();
        addWave();
        addCloud();
    }

    /**
     * Set up the beginning actors.
     */
    private void setup()
    {
        addObject(new Cloud(),150,120);
        addObject(new Cloud(),500,55);
        for(int i=40;i<getWidth();i+=80)
        {
            addObject(new Wave(-2),i,getHeight()/3);
        }
        for(int i=0;i<50;i++)
        {
            addObject(oxygen[i],501+(i*2),566);
        }
    }

    /**
     * Add a wave object in certain increments.
     */
    private void addWave()
    {
        counter++;
        if(counter==40)
        {
            counter=0;
            addObject(new Wave(-2),getWidth()+40,getHeight()/3);
        }
    }

    /**
     * Add a cloud object in certain increments.
     */
    private void addCloud()
    {
        counter2+=5;
        if(counter2==700)
        {
            addObject(new Cloud(),850,120);
            counter2=0;
        }
        if(counter2==350)addObject(new Cloud(),850,55);
    }

    /**
     * Draw backgound pictures. 
     */
    private void drawBackgroundPics()
    {
        for(int i=0;i<665;i+=166)
        {
            getBackground().drawImage(new GreenfootImage("Mud.png"),i,getHeight()-84);
        }
        getBackground().drawImage(new GreenfootImage("rock2.png"),75,getHeight()-100);
        getBackground().drawImage(new GreenfootImage("grass.png"),475,getHeight()-115);
        getBackground().setColor(new Color(255,255,28));
        getBackground().fillOval(-50,-50,100,100);
        getBackground().setColor(Color.black);
        getBackground().drawRect(500,550,101,31);
    }

    private void oxygen()
    {
        // counter3++;
        int num=49;
        List<Oxygen>oxygen=getObjects(Oxygen.class);
        Oxygen ox;
        for(int i=0;i<oxygen.size();i++)
        {
            if(oxygen.get(i).getIdentity()==num)
            {
                removeObject(oxygen.get(i));
                
                //break ;
            }
            num--;
        }

        /*if(counter3==2)
        {
        for(Oxygen o2:oxygen)
        {
        if(o2.getIdentity()==ident)
        {
        removeObject(o2);
        ident--;
        break;
        }
        }

        counter3=0;
        }*/
    }
}
that's my world class heres the oxygen class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Oxygen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Oxygen extends Actor
{
    private int identifier;
    public Oxygen(int num)
    {
        setImage("Oxygen-bar.png");
        identifier=num;
    }

    public Oxygen()
    {
        setImage("Oxygen-bar.png");
    }

    /**
     * Act - do whatever the Oxygen wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    

    public int getIdentity()
    {
        return identifier;
    }
}
and the terminal window
java.lang.NullPointerException
	at greenfoot.World.addObject(World.java:396)
	at Ocean.setup(Ocean.java:57)
	at Ocean.<init>(Ocean.java:31)
	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$3.run(WorldHandlerDelegateIDE.java:425)
	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)
the only reason as far as I can see for the nullPointer is the oxygen array not being initialized which it is I even tried initializing it manually and it didn't work
danpost danpost

2017/4/19

#
Line 16 only declares an array to hold up to 50 Oxygen objects. It does not assign any objects to any of the elements in the array. You will still need to create them and assign them to the elements of the array.
Nosson1459 Nosson1459

2017/4/19

#
is this good
for(Oxygen ox:oxygen)
{
    ox=new Oxygen();
}
danpost danpost

2017/4/19

#
Nosson1459 wrote...
is this good
for(Oxygen ox:oxygen)
{
    ox=new Oxygen();
}
It should work. As an alternative, you could just change line 57 to this:
addObject(oxygen[i] = new Oxygen(),501+(i*2),566);
Nosson1459 Nosson1459

2017/4/19

#
the loop didn't work but that last suggestion did thnx
Yehuda Yehuda

2017/4/19

#
Is there any Java Documentation/API for arrays, and where is it if there is?
danpost danpost

2017/4/19

#
Yehuda wrote...
Is there any Java Documentation/API for arrays, and where is it if there is?
It is located here. There is also the java.util.Arrays class, which is the wrapper class for arrays.
Yehuda Yehuda

2017/4/19

#
danpost wrote...
It is located here. There is also the java.util.Arrays class, which is the wrapper class for arrays.
I know how to use arrays, I was just wondering where I can see API for them, since they're not a class. The first link you gave me is in The Java™ Tutorials and the second doesn't really look like the array that I use when I place brackets after a variable type.
danpost danpost

2017/4/19

#
Yehuda wrote...
I was just wondering where I can see API for them, since they're not a class. The first link you gave me is in The Java™ Tutorials and the second doesn't really look like the array that I use when I place brackets after a variable type.
The second one is a close as you will get. It is similar to the way that the java.lang.Integer class and an int value are related. The Integer class is a wrapper class for int types.
Yehuda Yehuda

2017/4/19

#
danpost wrote...
The second one is a close as you will get. It is similar to the way that the java.lang.Integer class and an int value are related. The Integer class is a wrapper class for int types.
Thanks, now I see that the only thing that the arrays have that's not from Object is 'length'.
You need to login to post a reply.