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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:12:59+00:00 2026-06-17T15:12:59+00:00

I’m using socket to writing multiple clients – server application in java. I wrote

  • 0

I’m using socket to writing multiple clients – server application in java. I wrote simple client – server application and everything was ok but when i tried change it to multi clients app I got the exception when i started client:

Exception in thread "pool-1-thread-1" java.lang.NullPointerException
    at MiniSerwer.run(Serwer.java:110)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)

I have two more classes (threads – InWorker and OutWorker to input and output).

Serwer.java (without InWorker and OutWorker) :

public class Serwer {
Serwer(int port) {
ServerSocket serversocket=null;
ExecutorService exec= Executors.newCachedThreadPool();

    try {
        serversocket=new ServerSocket(port);
    } catch (IOException e) {
        e.printStackTrace();
}
    while(true) {
    Socket socket=null;
    try {
        socket = serversocket.accept();
    } catch (IOException e) {
        e.printStackTrace();
    }
    exec.execute(new MiniSerwer(socket)); // create new thread
        }
} }

MiniSerwer – helper class to create owns thread to everyone client

class MiniSerwer implements Runnable{

    Socket socket=null;
    ExecutorService exec=null;
    ObjectOutputStream oos=null;
    ObjectInputStream ois=null;

    MiniSerwer(Socket socket) {
        this.socket=socket;
        try {
            oos=new ObjectOutputStream(socket.getOutputStream());
            oos.flush();
            ois=new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while(true) {
            exec.execute(new InWorker(socket, ois)); // input stream
            exec.execute(new OutWorker(socket, oos)); //output stream
            Thread.yield();
        }
    }
}

I change my program but it still doesn’t work. Any other suggestion ?

Server:

public class Serwer implements Runnable{

ServerSocket serversocket=null;
ExecutorService exec= Executors.newCachedThreadPool();
int port;

Serwer(int port) {
    this.port=port;
}

public void run() {
    try {
        serversocket=new ServerSocket(port);
        while(true) {
            Socket socket=null;
            try {
                socket = serversocket.accept();
                exec.execute(new MiniSerwer(socket)); // create new thread
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
}
}

public static void main(String[] args) {
    int port;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the port:");
    port = in.nextInt();
    ExecutorService exec=Executors.newCachedThreadPool();
    exec.execute(new Serwer(port));
}   
 }

MiniSerwer:

class MiniSerwer implements Runnable{

    Socket socket=null;
    ExecutorService exec=Executors.newCachedThreadPool();
    ObjectOutputStream oos=null;
    ObjectInputStream ois=null;

    MiniSerwer(Socket socket) {
        this.socket=socket;
    }

    public void run() {
        try {
            oos=new ObjectOutputStream(socket.getOutputStream());
            oos.flush();
            ois=new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(true) {
            exec.execute(new InWorker(socket, ois)); // input stream
            exec.execute(new OutWorker(socket, oos)); //output stream
            Thread.yield();
        }
    }
}

I have a lot of exceptions when I’m trying send message from client to server.

2 sec. after connecting on the server side (i don’t send anything) :

Exception in thread "pool-2-thread-1" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:657)
    at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:943)
    at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:992)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)
  • 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-17T15:13:00+00:00Added an answer on June 17, 2026 at 3:13 pm

    Your executor service, in your MiniSerwer class, is never initialized. That’s the root cause of your NPE.

    But like I mentioned in my comment, you should not be doing all the Serwer logic in it’s constructor. The object never gets fully initialized because you never exit the constructor. Make the entire class Runnable and move that logic to the overridden run method. Then add a main method to be used in actually instantiation/running the server.

    Also, in your MiniSerwer, your stream initialization can fail. You need to validate the streams aren’t null before using them in the run method. Or just move the initialization logic for them to the beginning of your run method.

    EDIT

    You also have a bug in your MiniSerwer implementation, you spawn an infinite number of threads to process the object input and output streams:

    while(true) {
        exec.execute(new InWorker(socket, ois)); // input stream
        exec.execute(new OutWorker(socket, oos)); //output stream
        Thread.yield();
    }
    

    You will either run out of threads, run out of memory, or both. To be honest, your solution is over-engineered, I doubt the In and Out workers really need to be separated the way you have them now.

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

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I am using JSon response to parse title,date content and thumbnail images and place

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.