{"record":{"id":"e64f99da06ff85b3","repo":"apache/beam","slug":"geminimodelparameters-must-not-be-null","errorCode":null,"errorMessage":"GeminiModelParameters must not be null","messagePattern":"GeminiModelParameters must not be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"sdks/java/ml/inference/gemini/src/main/java/org/apache/beam/sdk/ml/inference/gemini/GeminiModelHandler.java","lineNumber":45,"sourceCode":"\n/**\n * Model handler for Google Gemini API inference requests.\n *\n * <p>This handler manages communication with Google's Gemini API, including client initialization,\n * request formatting, and response parsing. It allows executing a custom {@link\n * GeminiRequestFunction} against a batch of inputs.\n */\n@SuppressWarnings(\"nullness\")\npublic class GeminiModelHandler<InputT extends BaseInput, OutputT extends BaseResponse>\n    implements BaseModelHandler<GeminiModelParameters<InputT, OutputT>, InputT, OutputT> {\n\n  private transient Client client;\n  private GeminiModelParameters<InputT, OutputT> modelParameters;\n\n  @Override\n  public void createClient(GeminiModelParameters<InputT, OutputT> parameters) {\n    if (parameters == null) {\n      throw new NullPointerException(\"GeminiModelParameters must not be null\");\n    }\n    this.modelParameters = parameters;\n\n    // Configure client based on vertex or API key\n    if (parameters.getApiKey() != null) {\n      if (parameters.getProject() != null || parameters.getLocation() != null) {\n        throw new IllegalArgumentException(\"Project and location must be null if API key is set\");\n      }\n      this.client = Client.builder().apiKey(parameters.getApiKey()).build();\n    } else {\n      Client.Builder builder = Client.builder();\n      if (parameters.getProject() != null && parameters.getLocation() != null) {\n        builder.vertexAI(true).project(parameters.getProject()).location(parameters.getLocation());\n      } else if (parameters.getProject() != null || parameters.getLocation() != null) {\n        throw new IllegalArgumentException(\n            \"Project and location must both be provided if one is provided\");\n      }\n      this.client = builder.build();","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/ml/inference/gemini/src/main/java/org/apache/beam/sdk/ml/inference/gemini/GeminiModelHandler.java#L27-L63","documentation":"GeminiModelHandler.createClient requires a non-null GeminiModelParameters object to configure either an API-key client or a Vertex AI client. Passing null immediately throws this NullPointerException before any client setup. It is an explicit fail-fast guard rather than an incidental NPE.","triggerScenarios":"Calling createClient(null) directly, or configuring the handler such that its parameters field is never set (e.g. forgetting withModelParameters(...) during setup, which tests like testNullParameters exercise).","commonSituations":"Programmatic handler construction where the builder chain was interrupted or parameters were conditionally skipped; reflection-based or serialized handler instances missing parameters.","solutions":["Provide GeminiModelParameters via the builder (withModelParameters / equivalent) before applying the handler.","Ensure at least an apiKey or both project+location are present in the parameters object.","Add a null/emptiness check on your configuration object before instantiating the handler."],"exampleFix":"// before\nhandler.createClient(null);\n// after\nGeminiModelParameters<InputT, OutputT> p = GeminiModelParameters.<InputT, OutputT>builder().setApiKey(key).build();\nhandler.createClient(p);","handlingStrategy":"validation","validationCode":"checkNotNull(parameters, \"GeminiModelParameters must be configured before createClient\");","typeGuard":"boolean paramsReady(GeminiModelParameters<?,?> p) { return p != null && (p.getApiKey() != null || (p.getProject() != null && p.getLocation() != null)); }","tryCatchPattern":"try { handler.createClient(params); } catch (NullPointerException e) { throw new IllegalStateException(\"GeminiModelParameters missing\", e); }","preventionTips":["Build parameters via the builder pattern and assert non-null in tests.","Fail fast at config load time if no auth parameters were supplied.","Cover handler setup with a unit test (like testNullParameters) before deployment."],"tags":["java","apache-beam","gemini","null-check"],"backgroundTag":"null-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}