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

2012/5/25

Getting an error and i don't know how to fix it

JMoller JMoller

2012/5/25

#
hey guys im getting an error when im trying to run my program, tis
java.lang.NullPointerException
	at Gravity.pBar(Gravity.java:30)
	at Projectile.<init>(Projectile.java:15)
	at TrebArm.act(TrebArm.java:29)
	at greenfoot.core.Simulation.actActor(Simulation.java:507)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
	at greenfoot.core.Simulation.runContent(Simulation.java:204)
	at greenfoot.core.Simulation.run(Simulation.java:194)
could somebody help me? cheers jason
JMoller JMoller

2012/5/25

#
and this is when i try to start the program
darkmist255 darkmist255

2012/5/25

#
Well the error says that the problem is at Gravity() line 30. What is your code at line 30 of the Gravity class (and some surrounding code)?
erdelf erdelf

2012/5/25

#
without code I can say that you want to use something (maybe an object) which is not there.
JMoller JMoller

2012/5/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Gravity here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gravity extends Actor
{
   int gravityeffect = 1;
    public void act() 
    {
        
    }
    public void actGravity(int weight)
    {
        int x = getX();
        int y = getY();
        gravityeffect += weight;
        if (weight >= 2)
            gravityeffect--;
        if (gravityeffect >= weight);
            setLocation(x, y + gravityeffect);
    }
    
    public Bar pBar()
    {
        Earth world = (Earth) getWorld();
        return world.pBar;
    }
    public Bar cBar()
    {
        Earth world = (Earth) getWorld();
        return world.cBar;
    }
}

thats all my code for the gravity class
erdelf erdelf

2012/5/25

#
pls post Earth class too. I think i have the error, but I need to be sure
JMoller JMoller

2012/5/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
import java.lang.String;
/**
 * Write a description of class Earth here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Earth extends World
{
    private double gravity = 2;
    //static boolean transitioning = false;
    static boolean change = false;
    static final String[] barText = { "Projectile", "CounterWieght"};
    //static final int[][] barCols = { { 128,   0,   0, 255 },
    // {   0, 128,   0, 255 }};
    Bar pBar = new Bar("Projectile", "", 20, 100);  
    Bar cBar = new Bar("Counterweight", "", 200, 1000);
    //TrebBase base = new TrebBase();
    
    Bar[] bars = new Bar[2];
    public Earth()
    {
        super(1024, 768, 1);
        //for (int i = 0; i < 2; i++) 
        //{
        //  bars[i] = new Bar(barText[i ], "", 63 + 64 * (i), 255);
        //bars[i].setSafeColor(new Color(barCols[i ][0], barCols[i ][1], barCols[i][2], barCols[i][3]));
        //}
        //for (int i = 0; i < 2; i++) addObject(bars[i], 750, 75 + 25 * i);
        //addObject(new Button(), 750, 200);
        //addObject(new Pad(), 300, 200);
        //updateBackground();

        addObject(pBar, 900, 50);  
        addObject(cBar, 900,65);
        addObject(new TrebBase(), 220, 660);
        //Greenfoot.start();

    
        prepare();
    }

    //private Color BackgroundColor = Color.BLACK;

    public void act()
    {
        //     if (!change) return;
        // change = false;
        //updateBackground();
        //if (transitioning && getObjects(Bar.class).size() < 4) for (int i = 0; i < 2; i++) addObject(bars[2 + i], 750, 275 + 25 * i);
        //  if (!transitioning && getObjects(Bar.class).size() > 4) for (int i = 0; i < 2; i++) removeObject(bars[ + i]);
    }

    public double getGravity()
    {
        return gravity;
    }

    //public static boolean isKeyDown ("1");
      
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
      

        TrebArm trebarm = new TrebArm();
        TrebBase trebbase = new TrebBase();
        addObject(trebbase, 239, 668);
        trebbase.setLocation(227, 663);
        trebbase.setLocation(232, 663);
        trebbase.setLocation(238, 664);

        trebbase.setLocation(220, 660);
        trebbase.setLocation(229, 660);
        trebbase.setLocation(473, 722);
        trebbase.setLocation(234, 659);
        trebbase.setLocation(457, 678);
        trebbase.setLocation(228, 661);
        trebbase.setLocation(405, 686);
        trebbase.setLocation(408, 684);
        trebbase.setLocation(526, 683);
        trebbase.setLocation(231, 660);
        addObject(trebarm, 377, 589);
        trebarm.setLocation(168, 654);
        trebarm.setLocation(547, 429);
        trebbase.setLocation(712, 624);
        trebarm.setLocation(177, 652);
        trebbase.setLocation(226, 663);
        trebarm.setLocation(155, 614);
        trebarm.setLocation(158, 612);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(668, 555);
        trebbase.setLocation(671, 455);
        removeObject(trebbase);
        trebarm.setLocation(186, 675);

        trebarm.setLocation(217, 672);
        trebarm.setLocation(214, 676);
        trebarm.setLocation(211, 678);
    }
}
just ignor the last part it just locations that i haven't deleted yet
davmac davmac

2012/5/25

#
The problem is that you are calling the pBar() method when the Gravity object is not in the world, so getWorld() on line 29 returns null.
JMoller JMoller

2012/5/26

#
ah thank you
You need to login to post a reply.