Skip to content

Client constructor discards the boolean results of its initialize* helpers #11

Description

@dmccoystephenson

Problem

Client's constructor calls three helpers that each return boolean, and discards all three results:

public Client(String host, int port) {
    hostName = host;
    portNumber = port;
    initializeSocket();
    initializeReader();
    initializeWriter();
}

When initializeSocket() fails, socket stays null. initializeReader() and initializeWriter() then hit checkIfSocketIsNull(), print Socket is null., and return false — which is also discarded. Construction therefore "succeeds" with out and in both null, and the first call to sendStringToServer dereferences out.

Evidence

Read from src/SimpleServer/client/Client.java:

  • lines 19-25 — the constructor, with all three return values unused
  • lines 32-34 — sendStringToServer calls out.println(s) with no null guard
  • lines 49-57, 59-70, 72-83 — the three initialize* helpers, each returning boolean
  • lines 85-91 — checkIfSocketIsNull

The consequence described above (a NullPointerException on first send) is UNVERIFIED — no compile or run was performed in the session that filed this issue. The claim follows from out being null at that call site, which is source-verifiable; the thrown exception itself was not observed.

Proposed change

The constructor should honour the results it is already being given, and the object should report or refuse rather than proceeding half-constructed. The existing helper shape (private, boolean-returning, printing on failure) is the established pattern in this codebase and should be kept — only what the constructor does with the result needs to change.

Related: ClientApp hardcodes a hostname that resolves on one machine, which is the most likely way to reach this path. See the separate issue on that.


This issue was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).

drafted by Claude on behalf of Daniel Stephenson

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions