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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:05:43+00:00 2026-06-06T21:05:43+00:00

I have a simple example of a socket server where the user connects to

  • 0

I have a simple example of a socket server where the user connects to it and may or may not immediately send input (in this case it does not). Java throws an exception and I don’t understand why.

Here’s the code:

public static void main(String[] args) {
    org.apache.log4j.PropertyConfigurator.configure("conf/log4j.properties");
    log.debug("Application started");

    try {
        // Setup socket server
        int port = 9999;
        sv = new ServerSocket(port);

        // Accept incoming sockets or block
        Socket s = sv.accept();
        log.warn("Accepted new client connection!");

        InputStreamReader reader = new InputStreamReader(s.getInputStream(), "UTF-8");

        while(!reader.ready()) {}

        char[] b = null;
        reader.read(b);
        log.warn(new String(b));

    } catch (Exception e) {
        System.out.print(e.getMessage() + "\n");
        e.printStackTrace();
        System.exit(1);
    }
}

And the stack trace:

DEBUG [main] (Main.java:38) - Application started
WARN [main] (Main.java:47) - Accepted new client connection!
null
java.lang.NullPointerException
    at java.io.Reader.read(Reader.java:140)
    at com.cinefyapp.Main.main(Main.java:51)

Thanks for helping.

  • 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-06T21:05:44+00:00Added an answer on June 6, 2026 at 9:05 pm

    Initialize your char array

    char[] b = new char[256];
    

    and use read(char[] cbuf, int offset, int length).

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

Sidebar

Related Questions

I have this code, simple socket C example code, it works fine for an
Have been struggling all day trying to make this simple example work using socket.io.
I have this simple example I can't seems to get working : MERGE INTO
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
Does anyone have a simple example of implementing an async validation rule in csla?
I have this very simple example that I am using to learn structs in
Have a look a this simple example. I don't quite understand why o1 prints
What's an example of a simple handshake between a php socket server and flash?
I am trying to adapt my simple socket server so that it can have
I have a simple (very simple:) ) client and server which can send text

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.