{"record":{"id":"4415b3fb042432fb","repo":"stanfordnlp/CoreNLP","slug":"need-an-even-number-of-arguments-but-there-were","errorCode":null,"errorMessage":"Need an even number of arguments but there were \" + args.length","messagePattern":"Need an even number of arguments but there were \" \\+ args\\.length","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/PropertiesUtils.java","lineNumber":54,"sourceCode":"    return ! (value.equals(\"false\") || value.equals(\"no\") || value.equals(\"off\"));\n  }\n\n  public static boolean hasPropertyPrefix(Properties props, String prefix) {\n    for (Object o : props.keySet()) {\n      if (o instanceof String && ((String) o).startsWith(prefix)) return true;\n    }\n    return false;\n  }\n\n  /** Create a Properties object from the passed in String arguments.\n   *  The odd numbered arguments are the names of keys, and the even\n   *  numbered arguments are the value of the preceding key\n   *\n   *  @param args An even-length list of alternately key and value\n   */\n  public static Properties asProperties(String... args) {\n    if (args.length % 2 != 0) {\n      throw new IllegalArgumentException(\"Need an even number of arguments but there were \" + args.length);\n    }\n    Properties properties = new Properties();\n    for (int i = 0; i < args.length; i += 2) {\n      properties.setProperty(args[i], args[i + 1]);\n    }\n    return properties;\n  }\n\n  /** Convert from Properties to String. */\n  public static String asString(Properties props) {\n    try {\n      StringWriter sw = new StringWriter();\n      props.store(sw, null);\n      return sw.toString();\n    } catch (IOException ex) {\n      throw new RuntimeException(ex);\n    }\n  }","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/PropertiesUtils.java#L36-L72","documentation":"PropertiesUtils.asProperties(String... args) builds a Properties object from alternating key/value strings, so it requires an even number of arguments. If an odd count is passed it throws IllegalArgumentException immediately, since the last key would have no value. This is a fail-fast guard on command-line style property arguments.","triggerScenarios":"Calling PropertiesUtils.asProperties(\"key1\", \"val1\", \"key2\") — any invocation where args.length is odd, typically from a main() passing through raw command-line arguments with a missing value.","commonSituations":"User forgets a value on the command line (e.g. '-output' with no filename); a value containing spaces was not quoted so it shifted the argument positions; shell stripping an empty quoted argument.","solutions":["Count and pair your arguments; add the missing value for the last key","Quote argument values containing spaces so they stay a single token","Validate args.length % 2 == 0 in your main before calling asProperties","Print usage/help when the argument count is wrong"],"exampleFix":"// before\nProperties p = PropertiesUtils.asProperties(\"-filelist\"); // odd\n// after\nProperties p = PropertiesUtils.asProperties(\"-filelist\", \"input.txt\");","handlingStrategy":"validation","validationCode":"if (args.length % 2 != 0) {\n  throw new IllegalArgumentException(\"Expected key/value pairs; got \" + args.length\n      + \" args (dangling key: \" + args[args.length - 1] + \")\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  Properties p = PropertiesUtils.asProperties(args);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Need an even number\")) {\n    printUsage(\"Each key needs a value; missing value for last argument.\");\n    System.exit(2);\n  }\n  throw e;\n}","preventionTips":["Quote argument values containing spaces in shell scripts","Validate raw command-line args before passing them through to asProperties","Prefer explicit Properties files or builders over varargs key/value pairs for many options","Print usage text when argument count is odd"],"tags":["properties","commandline","arguments","validation"],"backgroundTag":"missing-required-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}