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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:40:21+00:00 2026-05-31T11:40:21+00:00

I have a Java game that uses networking, and I have a client (using

  • 0

I have a Java game that uses networking, and I have a client (using a Socket) fetching objects from an ObjectInputStream, running in its own thread.

From Client.java:

        Object input = null;
        while(true) {
            input = in.readObject();
            if(input != null) {
                listener.gotObject(input);
            }
        }

This works pretty well. The object is gotten and is passed to the listener, which is a class linked to a my main GameApp class.

From the listener (NetControl.java):

public void gotObject(Object o) {
    System.out.println(o);
    app.gotObject(o);
}

“app” is the instance that handles all new objects received and deals with them.

From the app (GameApp.java) (edit: the non-abstract CardGameApp.java gives greater context):

public void gotObject(Object o) {
    // select instance:
    if(o instanceof GameList) {
        GameList gameList = (GameList) o;
        System.out.println("gamelist: " + gameList);
        this.lobbyControl.gotGameList(gameList);
    }
}

I’ve run this code in the debugger, one step at a time, and it works perfectly. When I run it normally though, I get a null pointer (output is as follows:)

Game ID: 0. Name: game1. Players: 1 / 1. // the object, as it is printed in Client.java

gamelist: Game ID: 0. Name: game1. Players: 1 / 1. // the object, as it is printed again in GameApp.java

Exception in thread "Thread-1" java.lang.NullPointerException
at com.lgposse.game.app.GameApp.gotObject(GameApp.java:61)
at com.lgposse.game.net.NetControl.gotObject(NetControl.java:47)
at com.lgposse.net.client.Client.run(Client.java:49)

Now, I see the object being printed twice, so I know it has been received… but I get a null pointer.

I added a sleep function in the middle of the function:

    else if(o instanceof GameList) {
        GameList gameList = (GameList) o;
        System.out.println("gamelist: " + gameList);
        try {
            Thread.sleep(1000); // sleep 100 still gave null pointer
        } catch (InterruptedException e) {}
        this.lobbyControl.gotGameList(gameList);
    }

And setting it to sleep for a while, it all finally worked.

Any idea why I need to sleep the thread like this? Is there something I should do differently? I’m not sure why I was able to print the object while it was still considered null.

Edit: added some more context.

  • 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-31T11:40:22+00:00Added an answer on May 31, 2026 at 11:40 am

    It looks like lobbyControl is null, not gameList. If gameList were null, the top of the stack would be the gotGameList() method, not gotObject().

    If sleeping helps the problem, then you must be manipulating the lobbyControl member without proper concurrency safeguards. An ObjectInputStream won’t return an object until it’s been fully read from the stream, so your problem has nothing to do with not having completely read the object.


    Update: I can’t follow all the code, but it appears that a reference to the object being constructed is leaked to a thread (the client in the NetControl), which is started before the constructor completes. If that is the case, that’s very, very bad. You should never allow a partially constructed object to become visible to another thread.

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

Sidebar

Related Questions

I have a game that uses a callback to Java from C++ to force
i have a java game app that uses sockets to communicate with each other.
I have Java Map (out of Strings and Ints) objects that I want to
I have a more or less completed Java game. Enemies spawn from the top
I have a 2D game that uses two integer arrays to track x and
I have a java game using lwjgl and slick-util. It works perfectly on my
I have to write a multiplayer pacman game in Java for a university assignment
I am making a text based game in Java. I have a text field,
I am writing a game in Java, and need to have mouse interaction. I
I have a game I'm writing in java, it has the functionality to save

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.