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

  • Home
  • SEARCH
  • 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 3722242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:49:59+00:00 2026-05-19T05:49:59+00:00

I try hard to find the problem in this Java code, but I can’t

  • 0

I try hard to find the problem in this Java code, but I can’t find it – can you help me?

I hope the code I provide is enough, but I will post more if necessary.

Further I apologize, I didn’t make a minimal example.

game.getGroupPlayers().list();
MoverThread[] playerThread = game.getPlayers();
System.out.println(playerThread.length);
for (int i = 0; i < playerThread.length; i++) {
    try {
        System.out.println(i + " -> " +playerThread[i].toString());
        returnString += playerThread[i].toString() + "\n";
    } catch(NullPointerException e) {
        System.out.println("Problem at i = " + i);
        e.printStackTrace();
    }
    game.getGroupPlayers().list();
}

sometimes gives me the following output:

java.lang.ThreadGroup[name=Players,maxpri=10]
Player-0: 113
Player-1: 277
Player-2: 0
3
0 -> Player-0: 113
1 -> Player-1: 277
Problem at i = 2
java.lang.NullPointerException
at Referee.goalFound(Referee.java:70)
at DebugTestReferee.goalFound(DebugTestReferee.java:42)
at Player.checkGoal(Player.java:61)
at Player.run(Player.java:94)
at java.lang.Thread.run(Thread.java:636)
java.lang.ThreadGroup[name=Players,maxpri=10]
Player-0: 113
Player-1: 277
Player-2: 0

[edit]
here’s the source of getPlayers()

/*
 * post returns the games players as an array
 */
public MoverThread[] getPlayers() {
synchronized(movers) {
    MoverThread[] playerList = new MoverThread[players.activeCount()];
    players.enumerate(playerList);
    return playerList;
}
}

[edit]
here’s how players is generated

private ThreadGroup movers;
private ThreadGroup players;
private ThreadGroup ghosts;

private Observer observer;

/*
 * constructor
 */
 public Game(Maze maze, Referee referee) {
this.maze = maze;
this.referee = referee;
threadList = new ArrayList<MoverThread>();
movers = new ThreadGroup("Movers");
players = new ThreadGroup(movers, "Players");
ghosts = new ThreadGroup(movers, "Ghosts");
observer = null;
}

[edit]

Here’s how I call the method that generates the problem:

/*
 * post checks if the players thread was interrupted - if not if hostfield pretends to be a goal the game gets stopped and referee is called to perform "goal-found-actions"
 */
private void checkGoal() {
if (!getThread().isInterrupted()) {
    synchronized(getGame().getMovers()) {
        if (!getThread().isInterrupted()) {
            if (getHostField().isGoal()) {
                Field goal = getHostField();
                getGame().getReferee().goalFound(this, goal);
                getGame().setGameOver();
            }
        }
    }
}
}

and here’s the whole goalFound()

/*
 * post action to be performed if a player finds a goal
 * print some information
*/
public void goalFound(Player player, Field at) {
//FIXME get the Bug!!!
String returnString = "Game over - player " + player.getName() + " found a goal on (" + at.getPos()[0] + ", "  + at.getPos()[1] + ")!\n";
game.getGroupPlayers().list();
MoverThread[] playerThread = game.getPlayers();
System.out.println(playerThread.length);
for (int i = 0; i < playerThread.length; i++) {
    try {
        System.out.println(i + " -> " +playerThread[i].toString());
        returnString += playerThread[i].toString() + "\n";
    } catch(NullPointerException e) {
        System.out.println("Problem at i = " + i);
        e.printStackTrace();
    }
}
game.getGroupPlayers().list();
returnString += game.mazeString();
System.out.println(returnString);
}
  • 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-05-19T05:50:00+00:00Added an answer on May 19, 2026 at 5:50 am

    I found a solution – or maybe more a workaround…

    Beside using ThreadGroups I store my threads in an ArrayList aswell (maybe a Vector would be even better, but I’m fine with the ArrayList).

    I don’t know why, but when I try to call all Threads in the ThreadGroup it often happens that some Threads are left out. However, with the ArrayList it works fine.

    Would be interesting why ThreadGroups don’t work as supposed and what we need them for in this case.

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

Sidebar

Related Questions

This is very hard to explain but I'm going to try. We run a
This a little hard for me to explain, but I'll try my best. I'm
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
I've been looking on google about this but I can't find a clear answer.
It is hard to put this problem into words, but a real world example
First, I searched long and hard to try to find the answer to this.
Its hard to explain this so I'll try my best. Is it possible to
Kinda hard to explain, but i'll try. I have a datalist that is populated
I want to just hard code these values into a table. when I try
Edit: it's hard to describe what I'm trying to do, but here's a try

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.