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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:05:20+00:00 2026-06-04T10:05:20+00:00

In this game, I am trying to have multiple lasers spawn at given coordinates,

  • 0

In this game, I am trying to have multiple lasers spawn at given coordinates, but for some reason, the app crashes every time the numLasers is greater than 1. I have tried everything, and I could really use some help.

Here is my code:

public class LaserSpawn {

    private int amountOfVisibleLasers;
    private Context context;
    private long timeLastCreatedLaser;
    private Laser[] holderLaser;

    public LaserSpawn(Context context, int numLasers){
        this.context = context;
        holderLaser = new Laser[numLasers];

        this.amountOfVisibleLasers = numLasers;
        for(int i = 0; i< numLasers; i++){
            holderLaser[0] = new Laser(context, -10, -10);
        }
    }

    public void updatePlayerLaser(boolean shootLaser, float x, float y) {
    // Check if a new Laser should be created

    if(shootLaser == true) {    
        if(timeLastCreatedLaser + 100 < System.currentTimeMillis()) {
            timeLastCreatedLaser = System.currentTimeMillis();
            boolean createdNewLaser = false;

            for(int i = 0; i < this.amountOfVisibleLasers; i++) {
                if(createdNewLaser == false) {
                    if(holderLaser[i].isDisposed()) {
                        this.generateNewLaser(i,x,y);
                        createdNewLaser = true;

                    }
                }
            }
        }
    }

    // Update all the other Lasers
    for(int i = 0; i < this.amountOfVisibleLasers; i++) {
        if(holderLaser[i].isDisposed() == false) {
            holderLaser[i].update();
        }

    }

}

private void generateNewLaser(int i, float x, float y) {
    holderLaser[i].setY(y);
    holderLaser[i].setX(x);
}

Thanks!

  • 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-04T10:05:22+00:00Added an answer on June 4, 2026 at 10:05 am

    I’m pretty sure this is your problem:

    holderLaser[0] = new Laser(context, -10, -10);
    

    You only ever create a new Laser in the first position of your array. If you try access a second one you’ll get a null pointer exception.

    It should be something like

    holderLaser[i] = new Laser(context, -10, -10);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making this game where I'm trying to pair people. So I have this
All, I am trying to code a Connect4 game. For this, I have created
Right, I'm trying to implement this game, http://www.helicoptergame.net/ , but I'm struggling with the
I have this game with some variety of textures. Whenever I draw a texture
I have been trying to draw multiple balls for a game I am making,
I've been trying to get this Sudoku game working, and I am still failing
This is a problem I hit when trying to implement a game using the
I have this game where balloons are coming from bottom and player has to
Simple question but I got a headache to solve this game. Example regex. [a-zA-Z0-9\s]
I'm currently trying to write a multiple-file Python (2.6.5) game using PyGame. The problem

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.