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

2017/1/28

I get a java.lang.NullPointerException even though the same code works elsewere

GreenDodo GreenDodo

2017/1/28

#
So I have this bit working
    public void maimult( int nr )
    {
         /**
         *Mai mult - creaza un submeniu
         */
         World lume = getWorld(); 
         options_rez[] rezol = new options_rez[nr];
         if( !exista )
          {
              int i;
              int x = this.getX();
              int y = this.getY();
              for (i=0 ; i < nr ; i++)
              {
                  rezol[i] = new options_rez();
                  lume.addObject( rezol[i] , x + 100 , y + 50 * i );
              }
              exista = true ;
            }
          else
          {
                int i;
                for (i=0 ; i < nr ; i++)
                {
                    lume.removeObject ( rezol[i] );
                }
          }
and works just fine but after that i'm doing another method
public void  buildunda( int nr , double lamda , double A , double frecventa , double unghi , boolean vert , int x, int y )
    {
        World lume = getWorld();
        Unda[] punctepeunda = new Unda[nr];
        int i;
        for( i=0 ; i<nr ; i++ )
        {
            punctepeunda[i] = new Unda();
            punctepeunda[i].A = A;
            punctepeunda[i].frecventa = frecventa;
            punctepeunda[i].unghi = unghi - 3 * i/lamda;
            lume.addObject( punctepeunda[i] , x + 3 * i , y  );
        }
    }
I am using this method in a superclass and referencing it in a world subclass
public class simulareUnde extends World
{

    /**
     * Constructor for objects of class simulareUnde.
     */
    public simulareUnde()
    {    
        super(global.rez[0], global.rez[1], 1);
        preg();
    }
    private void preg()
    {
        Oscilator undamea = new Oscilator();
        undamea.buildunda( 10 , 200 , 200 , 0.01 , 0 , true , 100 , 300 );
    }
}
It might be because I am referencing the method in a bat bay but i'm not sure how to fix it
Nosson1459 Nosson1459

2017/1/29

#
What did it say in the Terminal Window (it would help ME figure it out even if other people already know the solution, but it would probably only help me if I see the full class codes)?
GreenDodo GreenDodo

2017/1/29

#
Here is the error that i keep getting : java.lang.NullPointerException at Oscilator.buildunda(Oscilator.java:69) at simulareUnde.preg(simulareUnde.java:23) at simulareUnde.<init>(simulareUnde.java:18) at Unde.act(Unde.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) ( P.S. Sorry for the messy names but they make sense in my language)
danpost danpost

2017/1/29

#
In the 'preg' method above, line 14 creates an Oscilator object. You need to add it into the world immediately before calling the 'buildunda' method on it (line 15).
Super_Hippo Super_Hippo

2017/1/29

#
You call the buildunda method on the Oscilator object without adding it to the world. 'getWorld' returns 'null', so, 'lume' is 'null' and you can't call a method on 'null'. You could pass the world to the buildunda method:
public void buildunda(World world, int nr, double lamda, double A, double frecventa, double unghi, boolean vert, int x, int y)
{
    Unda[] punctepeunda = new Unda[nr];
    //....
    world.addObject( punctepeunda[i], x + 3 * i, y );
}
private void preg()
{
    Oscilator undamea = new Oscilator();
    undamea.buildunda(this, 10, 200, 200, 0.01, 0, true, 100, 300);
}
GreenDodo GreenDodo

2017/1/29

#
Thanks. It now works properly
You need to login to post a reply.