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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:48:49+00:00 2026-06-04T05:48:49+00:00

This is a client-server programm. For each client server has a method, which checks

  • 0

This is a client-server programm.
For each client server has a method, which checks if there are some messages to this client.

Code:

        while (bool) {
            for(int j = 0;j<Start.bases.size();j++){
                if(Start.bases.get(j).getId() == id){
                    if(!Start.bases.get(j).ifEmpty()){
                        String output = Start.bases.get(j).getMessage();
                        os.println(output);
                        System.out.println(output +" *FOT* "+ addr.getHostName());
                    }
                }

            }

Each thread has an id.
So everything seems to be OK, but I get strange null pointer Exception at this line

if(Start.bases.get(j).getId() == id){

id – integer.
It is really strange, because I have run in debug this part and checked that “bases” and “id” are not null and bases have apropriate fields.
bases is not empty.

By the way bases is static(because every thread can use it) and bases is declared before this method is used.

This line doesn’t cause problems

            for(int j = 0;j<Start.bases.size();j++){

May it is because of method getId() ?

public int getId(){
    return id;

}

What is the problem?

Edited.

  static ArrayList<Base> bases;
  bases = new ArrayList<Base>();

Class Base:

 public class Base {
private ServerThread st;
private int id;
private String name;
private ArrayList<String> messages;

public Base(String n, ServerThread s_t, int i_d){
    messages = new ArrayList<String>();
    st = s_t;
    name = n;
    id = i_d;
}

public String getName(){
    return name;
}
public int getId(){
    return id;
}
public ServerThread getThr(){
    return st;
}
public String getMessage(){
    String ret = "";

    if(!messages.isEmpty()){
        ret = messages.get(0);
        messages.remove(messages.get(0));
    }

    return ret;
}

public void addMessage(String m){
    messages.add(m);
}

public boolean ifEmpty(){
    return messages.isEmpty();
}
  }

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-04T05:48:51+00:00Added an answer on June 4, 2026 at 5:48 am

    Given this:

    “I have run in debug this part and checked that “bases” and “id” are not null and bases have apropriate fields”

    and this:

    bases is static(because every thread can use it)

    I think it’s pretty likely that you have a race condition. In a race condition, there are two threads simultaneously accessing the same data structure (in this case, Start.bases). Most of the time, one thread’s code completes faster, and everything goes the way you expect them to, but occasionally the other thread gets a head-start or goes a little faster than usual and things go “boom”.

    When you introduce a debugger with a break point, you pretty much guarantee that the code with the break point will execute last, because you’ve stopped it mid-execution while all your other threads are still going.

    I’d suggest that the size of your list is probably changing as you execute. When a user leaves, is their entry removed from the “base” list? Is there some other circumstance where the list can be changed from another thread during execution?

    The first thing I’ll suggest is that you switch your code to use iterators rather than straight “for” loops. It won’t make the problem go away (it might actually make it more visible), but it will make what’s happening a lot clearer. You’ll get a ConcurrentModificationException at the point where the modification happens, rather than the less helpful NullPointerException only when a certain combination of changes happens.):

            for(Base currentBase : Start.bases)
            {
                if(currentBase.getId() == id && !currentBase.ifEmpty())
                {
                    String output = currentBase.getMessage();
                    os.println(output);
                    System.out.println(output +" *FOT* "+ addr.getHostName());
                }
            }
    

    If you do get a concurrent modification exception with the above code, then you’re definitely dealing with a race condition. That means that you’ll have to synchronize your code.

    There are a couple of ways to do this, depending on how your application is structured.

    Assuming that the race is only between this bit of code and one other (the part doing the removing-from-the-list), you can probably solve this scenario by wrapping both chunks of code in

    synchronized(Start.bases)
    {
       [your for-loop/item removal code goes here]
    }
    

    This will acquire a lock on the list itself, so that those two pieces of code will not attempt to update the same list at the same time in different threads. (Note that it won’t stop concurrent modification to the Base objects themselves, but I doubt that’s the problem in this case).

    All of that said, any time you have a variable which is read/write accessed by multiple threads it really should be synchronized. That’s a fairly complicated job. It’s better to keep the synchronization inside the object you’re managing if you can. That way you can see all the synchronization code in one place, making you less likely to accidentally create deadlocks. (In your code above, you’d need to make the “for” loop a method inside your Start class, along with anything else which uses that list, then make “bases” private so that the rest of the application must use those methods).

    Without seeing all the other places in your code where this list is accessed, I can’t say exactly what changes you should make, but hopefully that’s enough to get you started. Remember that multi-threading in Java requires a very delicate hand!

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

Sidebar

Related Questions

I'm trying to make a multithreaded server/client app with java ! this code is
I've built apps that connect to a server. But this time a client has
I'm working on this project where the client has a virtual server setup. I
i have this client/server application, and the client application sometimes completely freezes when i
Basically my question is the exact same one as this: Simple client/server, TCP/IP encrypting
I'm programming a C/C++ client/server sockets application. At this point, the client connects itselfs
i'm studying this source base . Basically this is an Anim server client for
i have data structure i am passing this from server to client using data
Hi I have an application that operations like this.. Client <----> Server <----> Monitor
I got server-client application. On the client side, I am using this I/O stream

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.