Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:57:06+00:00 2026-06-12T17:57:06+00:00

I am trying to do a basic game.I have classes named Ball.java , Scratch.java

  • 0

I am trying to do a basic game.I have classes named Ball.java , Scratch.java and Panel.java

In Panel,I am trying to get information from Ball.java class but I am getting NullPointerException.Where is the mistake in my code?

Getting exceiption in this code in Panel.java

public Rect getBoundsBall(){
    return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
}

Error Log

10-13 12:57:28.746: E/AndroidRuntime(367): FATAL EXCEPTION: Thread-10
10-13 12:57:28.746: E/AndroidRuntime(367): java.lang.NullPointerException
10-13 12:57:28.746: E/AndroidRuntime(367):  at com.emredavarci.denemeler.Panel.getBoundsBall(Panel.java:44)
10-13 12:57:28.746: E/AndroidRuntime(367):  at com.emredavarci.denemeler.Panel.update(Panel.java:70)
10-13 12:57:28.746: E/AndroidRuntime(367):  at com.emredavarci.denemeler.TutorialThread.run(TutorialThread.java:30)

Panel.java

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

    private TutorialThread _thread;

    Scratch raket=new Scratch();
    Ball top=new Ball();
    Bitmap ball;
    Bitmap _scratch;

    public Panel(Context context) {
        super(context);
        getHolder().addCallback(this);
        _thread = new TutorialThread(getHolder(), this); /
        setFocusable(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) { // to
        switch (event.getAction()){
           case MotionEvent.ACTION_MOVE: // 
           raket.setX((int)(event.getX()));
           break;
       }

       return true;

    }

    public Rect getBoundsBall(){
        return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight() );
    }

    public Rect getBoundsScratch(){
        return new Rect (raket.getX(), raket.getY(),raket.getX()+_scratch.getWidth() ,raket.getY()+_scratch.getHeight() );
    }

    public void update(){ /


        if((top.getX()<480) && (top.getX()>0)){
            top.setX(top.getX()+top.getxdirection());
            }
        if((top.getX()==480) || (top.getX()==0)){
            top.setxdirection(-1);
            top.setX(top.getX()+top.getxdirection());
        }
        if((top.getY()<780) && (top.getY()>0)){
            top.setY(top.getY()+top.getydirection());
            }
        if((top.getY()==780) || (top.getY()==0)){
            top.setydirection(-1);
            top.setY(top.getY()+top.getydirection());
        }


        Rect BallBounds = getBoundsBall();
        Rect ScratchBounds = getBoundsScratch();

        if( BallBounds.intersect(ScratchBounds)     ){              
            top.setydirection(-1); 
            //top.setY(top.getY()); !!!!
        }   

    }





    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    public void surfaceCreated(SurfaceHolder holder) {
         _thread.setRunning(true);
         _thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

    @Override
    public void onDraw(Canvas canvas) {  
    _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.red2);
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(_scratch, raket.getX() - (_scratch.getWidth() / 2), raket.getY() - (_scratch.getHeight() / 2), null);
    canvas.drawBitmap(ball, top.getX() - (ball.getWidth() / 2), top.getY() - (ball.getHeight() / 2), null);
    }

}

Ball.java

public class Ball {

    private int x=100;          // the X coordinate
    private int y=100;          // the Y coordinate
    int xdirection=10;
    int ydirection=10;


    public Ball() {
        // TODO Auto-generated constructor stub
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public void setX(int a){
        x=a;
    }

    public void setY(int b){
        y=b;
    }

    public int getxdirection(){
        return xdirection;
    }

    public int getydirection(){
        return ydirection;
    }

    public void setxdirection(int a){
         xdirection=xdirection*a;
    }

    public void setydirection(int b){
        ydirection=ydirection*b;
    }

    //while ile ball u hareket ettir
    //ball un koordinatlarını sakla


}

Scratch.java

public class Scratch {

    private int x = 250; 
    private int y = 600;

    public Scratch() {
        // TODO Auto-generated constructor stub
    }

    public void setX(int a){
        x=a;
    }

    public void setY(int b){
        y=b;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:57:07+00:00Added an answer on June 12, 2026 at 5:57 pm

    You are missing something like:

    Ball top = new Ball();
    

    i.e. the variable top is not initialized, but used in the update method.

    Also, initialize the ball object outsite of onDraw. Who knows if update will be called before onDraw?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a basic falling sand game in java, I have
I am trying to make a very basic game with Java and I am
I'm trying the basic jMonkey but I get this exception jun 27, 2012 9:25:08
I have a bunch of classes in a basic (and badly coded!) game engine
Im trying to get into some basic JavaFX game development and I'm getting confused
I'm trying to create a basic Pacman game in C++ (I'll use Java syntax
Hey, I'm trying to make a basic hockey style game. I have the basic
I am trying to make a Chess multiplayer game in Visual Basic. Its a
So I'm trying to achieve something seemingly very basic in jQuery. I have a
I am trying to create a very basic flash game, using Tom Krcha's P2P

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.