TheAlgorithms/Java · error · IllegalArgumentException
Input must be a non-empty binary string.
Error message
Input must be a non-empty binary string.
What it means
Thrown by OnesComplement.onesComplement(String) when the input is null or empty. The method inverts each bit of a binary string; with no characters there is nothing to invert, so null and empty strings are rejected before processing begins.
Source
Thrown at src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java:24
* The class OnesComplement computes the complement of binary number
* and returns
* the complemented binary string.
* @return the complimented binary string
*/
public final class OnesComplement {
private OnesComplement() {
}
/**
* Returns the 1's complement of a binary string.
*
* @param binary A string representing a binary number (e.g., "1010").
* @return A string representing the 1's complement.
* @throws IllegalArgumentException if the input is null or contains characters other than '0' or '1'.
*/
public static String onesComplement(String binary) {
if (binary == null || binary.isEmpty()) {
throw new IllegalArgumentException("Input must be a non-empty binary string.");
}
StringBuilder complement = new StringBuilder(binary.length());
for (char bit : binary.toCharArray()) {
switch (bit) {
case '0' -> complement.append('1');
case '1' -> complement.append('0');
default -> throw new IllegalArgumentException("Input must contain only '0' and '1'. Found: " + bit);
}
}
return complement.toString();
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate the string is non-null and non-empty before calling.
- Treat empty input as a no-op/empty result upstream rather than passing it through.
- Provide a default non-empty binary string when the source is missing.
Example fix
// before String out = OnesComplement.onesComplement(inputField.getText()); // after String raw = inputField.getText(); String out = (raw == null || raw.isEmpty()) ? "" : OnesComplement.onesComplement(raw);
Defensive patterns
Strategy: validation
Validate before calling
if (binary == null || binary.isEmpty()) {
return ""; // or throw your own error
}
String out = OnesComplement.onesComplement(binary); Type guard
static boolean isNonEmptyBinary(String s) { return s != null && !s.isEmpty(); } Try / catch
try {
String out = OnesComplement.onesComplement(binary);
} catch (IllegalArgumentException e) {
out = ""; // nothing to invert
} Prevention
- Normalize input upstream: treat null/empty as empty output.
- Avoid passing raw user fields without a null/empty check.
- Document that this method needs at least one bit.
When it happens
Trigger: Calling onesComplement(null) or onesComplement(""). Also triggered by a value that becomes empty after trim(), or a field/config that was never set.
Common situations: User input left blank; reading from a property file where the key is absent (null); trimming whitespace-padded input down to empty.
Related errors
- Input must contain only '0' and '1'. Found: {}
- Input must contain only '0' and '1'.
- Input cannot be negative
- The exponent must be positive
- Input data contains invalid characters. Only uppercase A-Z a
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/01b32d2ebb411ee5.
Report an issue: GitHub.