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 8102829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:20:32+00:00 2026-06-05T23:20:32+00:00

I’m using AndEngine to make a game for the Android platform. I need to

  • 0

I’m using AndEngine to make a game for the Android platform. I need to detect if a circular body contacts a circular sprite. To do this, I’m calculating the centers and radii of both and using pythag to determine if they are indeed touching. However, I keep getting a nullpointerexception when I try to get the x coordinate of my body. spriteNum increases every time the screen it touched and a sprite is created. Any ideas why? My code:

public class grow implements Runnable{
    Sprite sp;
    Body body[] = new Body[100];
    public grow(Sprite sprite){
        sp = sprite;
    }
    @Override
    public void run() {
        float radf, rads; //fill radius/stationary radius
        float[] fill; //fillx/stationaryx
        float fx=0, fy=0, sx, sy;

        while(down){
            //Log.e("Tag","Running thread. Down = "+Boolean.toString(down));
             yourSprite[spriteNum].setScale(scale += 0.1);
             fill = yourSprite[spriteNum].getSceneCenterCoordinates();
             fill[0]=fx;
             fill[1]=fy;
             radf=yourSprite[spriteNum].getHeightScaled()/2;    

             Log.e("Fill X before for",Float.toString(fx));

            if(spriteNum>0)
                for(int x=0;x<spriteNum;x++){               
                    rads=yourSprite[x].getHeightScaled()/2;
                    sx = body[x].getWorldCenter().x; //Null pointer exception
                    sy = body[x].getWorldCenter().y;

                    if(Math.sqrt(Math.pow((fx-sx),2)+Math.pow((fy-sy),2))<(radf+rads))
                            down = false;
                    Log.e("Fill x",Float.toString(fx));
                    Log.e("Stat x",Float.toString(sy));
                }
             try {
                Thread.sleep(75);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        body[spriteNum] = PhysicsFactory.createCircleBody(mPhysicsWorld, yourSprite[spriteNum], BodyType.DynamicBody, FIXTURE_DEF);
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(yourSprite[spriteNum], body[spriteNum], true, true));
        if(body[0]!=null)
            Log.e("Body created","not null"); //This says it's not null, but when I did the same thing just inside of my for() loop and it did say it was null
    }
  • 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-05T23:20:33+00:00Added an answer on June 5, 2026 at 11:20 pm

    I guess it’s because you have initialized body array Body body[] = new Body[100] but not individual Body elements in it. So when you access body[x] it returns null. You should also initialize body elements in array. Something like:

    for(int i=0; i<body.length; i++) {
       // probably some cod here..
       body[i] = new Body(..);
       // probably some cod here..
    }
    

    Another cause that came into my mind for your problem is, you said:

    spriteNum increases every time the screen it touched and a sprite is
    created.

    So it can be possible that you have initialized some body elements on startup and then when sprite count increases you do not create new elements in body array, so when you try body[0] it is not null but for new sprites for which body elements may not have been initialized you get null and thus NPE.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.