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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:11:19+00:00 2026-06-05T13:11:19+00:00

I am using the acm library(acm.jar), same library Stanford students used for their Java

  • 0

I am using the acm library(acm.jar), same library Stanford students used for their Java course CS106A to create graphical application easier.

acm.jar can be found here: http://jtf.acm.org/

The following code adds a character sprite to a graphics window. If I press z , the character animates itself moving his bow and arrows starts to spawn moving vertically through the use of Java Threads. There is no errors so far.

Now I want to be able to able to perform collision detection on a threaded GImage object(linkArrow). An error happened in my program when I tried doing this:

arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
arrowObject = getElementAt(arrowPoint);

I get this error:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException

You can ctrl+f and type bug to see the error location of my program.

I have done collision detection before using graphical rectangles and oval using the GPoint and getElementAt for my game “BreakOut” and never had a problem using GPoint and getElementAt.

Here is my code: first class is main program that runs the thread. The second class is the thread.

        import java.awt.event.KeyEvent;
        import acm.graphics.GImage;
        import acm.program.GraphicsProgram;
        import acm.util.RandomGenerator;

        public class Link extends GraphicsProgram {


            private static final double GRAVITY = 1;
            public void init(){
                setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
                addLink();

                addKeyListeners();
        }


            private void addLink(){
                linkCharacter = new GImage("link sprites/linkFaceRight/link_frame_1_face_right.png");
                add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
            }

          public void keyPressed(KeyEvent e){ 
                /* Link's Movement
                 * 
                 */
              char linkMoveRightKey = e.getKeyChar();
            if(linkMoveRightKey == 'z')
                    {

                    xSpeed = 0;
                    ySpeed = 0;
                    pause(arrowDELAY);
                    linkCharacter.move(xSpeed, ySpeed);
                    linkCharacter.setImage(linkAttackWithBow[linkFrame]);

                    linkFrame++;
                    callArrow();

 // BUG 
           arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
           arrowObject = getElementAt(arrowPoint);

           }
  //                  


                   /*
                    * summon link's arrow
                    */
                    }

                    if(linkFrame>=linkAttackWithBow.length){
                        linkFrame = 0;
                    }


            }



            private void callArrow(){
                if(linkFrame == 2){
                 linkArrow = new ArrowThread(SIZE, rgen.nextColor());
                add(linkArrow,linkCharacter.getX(),linkCharacter.getY());

                Thread arrowThread = new Thread(linkArrow);
                arrowThread.start();




                }
            }

        private ArrowThread linkArrow;
            private int SIZE = 400;

            private RandomGenerator rgen = RandomGenerator.getInstance();
            private GImage gameBackgroundImage;

            private GImage linkCharacter;

            private double arrowDELAY = 28;


            private  int link_Location_XCoord = 50;
            private  int link_Location_YCoord = 50 ;
            private final int APPLICATION_WIDTH = 1200;
            private final int APPLICATION_HEIGHT = 800;
         private String[] linkAttackWithBow = {"link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png"};

            private int linkFrame = 0;
            private int xSpeed =5; //the number of pixels to move in x
            private int ySpeed = 0; //0 so you only move horizontally
        }

Here is my Thread class to spawn the arrows

import java.awt.Color;

import acm.graphics.GImage;


public class ArrowThread extends GImage implements Runnable{

    public ArrowThread(int SIZE,Color color){
        super("link sprites/linkAttackWithBow/arrow.png", SIZE,SIZE);


    }

    public void run(){
        for(int i = 0; i < 1000/STEP;i++){
            pause(60);

            move(arrowSpeedX,arrowSpeedY);

        }

    }






    private static final int arrowSpeedX = 0;
    private static final int arrowSpeedY = -5;

    private static final double STEP  = 5; 


}
  • 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-05T13:11:22+00:00Added an answer on June 5, 2026 at 1:11 pm

    It looks like the problem is that the member linkArrow is null on the first frame. See the check:

    if(linkFrame == 2){
        linkArrow = new ArrowThread(SIZE, rgen.nextColor());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
I have started taking the course Programming methodology(CS106A) available at stanford website. But I
I am writing a paper using the ACM template which capitalizes every letter in
I'm working on a GUI for a card game and am using the ACM's
I am trying to create three concentric circles of different color using GraphicsProgram. However
Using Android TelephonyManager an application can obtain the state of data activity over the
I am using the acm LaTeX template and I have trouble making my paper
Using Entity Framework CodeFirst, how do I create a created datetime column that gets
I am a high school student taking cs106a at Stanford via video. For my
Using boost python I need create nested namespace. Assume I have following cpp class

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.