omer
omer

Reputation: 69

App crashes.... NullPointerException

The app is crashing the second i open it... it says it crashes due to NullPointerException but i cant figure out why.

this is the logCat:

12-06 08:37:58.217: D/AndroidRuntime(1138): Shutting down VM
12-06 08:37:58.217: W/dalvikvm(1138): threadid=1: thread exiting with uncaught exception (   (group=0xb2aeaba8)
12-06 08:37:58.247: E/AndroidRuntime(1138): FATAL EXCEPTION: main
12-06 08:37:58.247: E/AndroidRuntime(1138): Process: com.example.batandball, PID: 1138
12-06 08:37:58.247: E/AndroidRuntime(1138): java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.batandball/com.example.batandball.MainActivity}:    java.lang.NullPointerException
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.os.Looper.loop(Looper.java:136)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at java.lang.reflect.Method.invoke(Method.java:515)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at dalvik.system.NativeStart.main(Native Method)
12-06 08:37:58.247: E/AndroidRuntime(1138): Caused by: java.lang.NullPointerException
12-06 08:37:58.247: E/AndroidRuntime(1138):     at com.example.batandball.brickArr.duplicate(brickArr.java:15)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at com.example.batandball.brickArr.<init>(brickArr.java:9)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at com.example.batandball.MainActivity.runable(MainActivity.java:29)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at com.example.batandball.MainActivity.onCreate(MainActivity.java:17)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.Activity.performCreate(Activity.java:5231)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-06 08:37:58.247: E/AndroidRuntime(1138):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-06 08:37:58.247: E/AndroidRuntime(1138):     ... 11 more
12-06 08:38:03.467: I/Process(1138): Sending signal. PID: 1138 SIG: 9

this is the mainActivity:

public class MainActivity extends Activity {
private Paddle paddle;

@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    runable();
}

public void runable()
{
   Point p=new Point();
   this.getWindowManager().getDefaultDisplay().getSize(p);
   int screenW=p.x;
   int screenH=p.y;
   paddle=new Paddle(screenW/2,(float)(0.85*screenH),screenW, 30,135);
   Ball ball=new Ball(screenW/2,screenH/3,20,screenW,screenH,paddle);
   Brick brick=new Brick((float)0,(float)0,(float)(screenH/16),(float)(screenW/7));
   brickArr m=new brickArr(brick);
   ballView bv=new ballView(this,ball);
   PaddleView pv=new PaddleView(this,paddle);
   BrickView brv=new BrickView(this,m.getB());
   gameView gv=new gameView(this);
   gv.setbv(bv,pv,brv);
   setContentView(gv);
   ballMover ballmove=new ballMover(gv,ball,paddle);
   PaddleMover paddlemove=new PaddleMover(gv,paddle);
   brickMover brickmover=new brickMover(gv,m.getB(),ball);
   brickmover.start();
   ballmove.start();
   paddlemove.start();

}
public boolean onTouchEvent(MotionEvent event)
{
    if (event.getX()>=this.paddle.left()-this.paddle.screenW/16&&event.getX()   <=this.paddle.right()+this.paddle.screenW/16)
        this.paddle.setdest(event.getX());
    return true;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

this is the brickArr method:

public class brickArr {
private Brick brick;
private Brick[] b;
public brickArr(Brick brick) {
    this.brick=brick;
    this.b=new Brick[36];
    duplicate(b);
    }
public void duplicate(Brick[] b)
{
    for(int i=0;i<b.length-1;i++)
    {
        b[i].leftx=brick.weidth*(i%7);
        b[i].topy=brick.height*(i/7);
        b[i].height=brick.height;
        b[i].weidth=brick.weidth;
    }
    b[35].leftx=0;
    b[35].topy=0;
    b[35].height=0;
    b[35].weidth=0;
    }
public Brick[] getB()
{
    return b;
}


}

sorry if its a dumb question but i really cant understand what's the problem.

Upvotes: 0

Views: 81

Answers (1)

SMA
SMA

Reputation: 37023

You brick array element's are uninitialize i.e. bricks[i]

Change your for loop to initialise it like:

for(int i=0;i<b.length-1;i++)
{
    b[i] = new Brick();//call appropriate c'tor what is defined by you.
    b[i].leftx=brick.weidth*(i%7);
    b[i].topy=brick.height*(i/7);
    b[i].height=brick.height;
    b[i].weidth=brick.weidth;
}

Upvotes: 1

Related Questions