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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:10:06+00:00 2026-05-14T14:10:06+00:00

I am writing a simple web server in java as an exercise. When I

  • 0

I am writing a simple web server in java as an exercise. When I run the program it immediately starts into an infinite loop outputting “Error: null” into the console. I have a few try/catch statements that do

System.out.println("Error: " + e.getMessage());

I tried using the eclipse debugger to figure out where it starts, but when I run it in the debugger, it acts normally. Heres the two relevant clips of code:

public void run() {
        // Declare server socket
        ServerSocket serversocket = null;

        try {
            // Initialize serversocket on the port passed to
            // the constructor 
            serversocket = new ServerSocket(port);
            System.out.println("Server started on port " 
                    + Integer.toString(port));
        } catch (Exception e) {
            System.out.println("Fatal Error: " + e.getMessage());
        }

        while (true) {
            try {
                // Wait for connections
                Socket connectionsocket = serversocket.accept();
                // Get client IP
                InetAddress client = connectionsocket.getInetAddress();
                System.out.println(client.getHostName() + " connected.");

                // Read the request into a buffer
                BufferedReader input = new BufferedReader(
                    new InputStreamReader(connectionsocket.getInputStream()));

                // Prepare the output stream for sending headers
                // and requested file to the client
                DataOutputStream output = 
                    new DataOutputStream(connectionsocket.getOutputStream());

                http_handler(input, output);
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }

and

private void http_handler(BufferedReader input, DataOutputStream output) {
    int method = 0; // 1 get, 2 head, 0 not supported
    String http = new String();
    String path = new String();
    String file = new String();
    String user_agent = new String();
    try {
        // Two types of requests we can handle:
        // Get /index.html HTTP/1.0
        // HEAD /index.html HTTP/1.0
        String tmp = input.readLine(); // Read from the stream
        String tmp2 = new String(tmp);
        tmp.toUpperCase();
        if (tmp.startsWith("GET")) {
            method = 1;
        }
        if (tmp.startsWith("HEAD")) {
            method = 2;
        }

        if (method == 0) {
            try {
                // If the request is unsupported, send the error
                output.writeBytes(construct_http_header(501, 0));
                output.close();
                return;
            } catch (Exception e2) {
                System.out.println("Error: " + e2.getMessage());
            }
        }

        // Get whats between the spaces in the request
        // without the beginning slash
        int start = 0;
        int end = 0;

        for (int a = 0; a < tmp2.length(); a++) {
            if (tmp2.charAt(a) == ' ' && start != 0) {
                end = a;
                break;
            }
            if (tmp2.charAt(a) == ' ' && start == 0) {
                start = a;
            }
        }
        path = tmp2.substring(start + 2, end);

    } catch(Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    FileInputStream requestedfile = null;

    try {
        // Try to open the file
        requestedfile = new FileInputStream(path);
    } catch (Exception e) {
        try {
            output.writeBytes(construct_http_header(404, 0));
            output.close();
        } catch (Exception e2) {}
        ;
        System.out.println("Error: " + e.getMessage());
    }

    try {
        int type_is = 0;
        // Find out what the filename ends with so we can
        // put the right MIME type in the header

        if (path.endsWith(".zip") || path.endsWith(".exe")
                || path.endsWith(".tar")) {
            type_is = 3;
        } else if (path.endsWith(".jpg") || path.endsWith(".jpeg")) {
            type_is = 1;
        } else if (path.endsWith(".gif")) {
            type_is = 2;
        } else {
            type_is = 5;
        }
        // write out the header, 200 -> OK
        output.writeBytes(construct_http_header(200, type_is));

        // if the request was HEAD, we don't print the body
        if (method == 1) {
            while (true) {
                // read the file from the filestream and ouput through
                // the client outpustream
                int b = requestedfile.read();
                if (b == -1) {
                    break; // end of file
                }
                output.write(b);
            }
        }
        // Clean up
        output.close();
        requestedfile.close();
    } catch (Exception e) {}
}

** EDIT **
I printed the stack trace, and it is giving a NullPointerException on

Socket connectionsocket = serversocket.accept();

I thought this method was supposed to wait for connections. How do i keep it from doing what it’s doing?

  • 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-14T14:10:07+00:00Added an answer on May 14, 2026 at 2:10 pm

    If the ServerSocket initialization fails, you end up with an infinite null pointer exception because nothing exists to accept the connection on.

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

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The easiest way to split an existing index (without reindexing… May 14, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer You could implement getName() as a pipelined function: CREATE OR… May 14, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer In python (and consequently in IronPython) you cannot change a… May 14, 2026 at 11:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.