{"record":{"id":"bbac3da3a0d75277","repo":"microg/GmsCore","slug":"task-must-not-be-null","errorCode":null,"errorMessage":"Task must not be null","messagePattern":"Task must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"play-services-tasks/src/main/java/com/google/android/gms/tasks/Tasks.java","lineNumber":49,"sourceCode":"import java.util.concurrent.atomic.AtomicInteger;\nimport java.util.concurrent.atomic.AtomicReference;\n\n/**\n * {@link Task} utility methods.\n */\n@PublicApi\npublic final class Tasks {\n\n    /**\n     * Blocks until the specified Task is complete.\n     *\n     * @return the Task's result\n     * @throws ExecutionException   if the Task fails\n     * @throws InterruptedException if an interrupt occurs while waiting for the Task to complete\n     * @throws TimeoutException     if the specified timeout is reached before the Task completes\n     */\n    public static <TResult> TResult await(Task<TResult> task, long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException {\n        if (task == null) throw new IllegalArgumentException(\"Task must not be null\");\n        if (timeout <= 0) throw new IllegalArgumentException(\"Timeout must be positive\");\n        if (unit == null) throw new IllegalArgumentException(\"TimeUnit must not be null\");\n        if (task.isComplete()) return handleCompletedTask(task);\n        CountDownLatch latch = new CountDownLatch(1);\n        task.addOnCompleteListener(Runnable::run, completedTask -> latch.countDown());\n        if (latch.await(timeout, unit)) {\n            return handleCompletedTask(task);\n        }\n        throw new TimeoutException(\"Timed out waiting for Task\");\n    }\n\n    /**\n     * Blocks until the specified Task is complete.\n     *\n     * @return the Task's result\n     * @throws ExecutionException   if the Task fails\n     * @throws InterruptedException if an interrupt occurs while waiting for the Task to complete\n     */","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/microg/GmsCore/blob/157c9d86ac46c195a86c2f15ab55c84036223f95/play-services-tasks/src/main/java/com/google/android/gms/tasks/Tasks.java#L31-L67","documentation":"Tasks.await(task, timeout, unit) validates its input and throws IllegalArgumentException when the task reference is null. A null Task carries no completion state, so blocking on it is meaningless and the method fails fast.","triggerScenarios":"Calling Tasks.await(null, timeout, unit) — typically when a preceding async call returned null instead of a Task.","commonSituations":"Misconfigured initialization of the underlying task source returning null; unchecked return value of a factory method; Kotlin/Java interop where a platform type hid the nullability.","solutions":["Check that the API producing the Task is configured and returning a non-null Task before calling await","Add an explicit null check on the task argument and handle the null path","Log/inspect why the producer returned null (init failure, wrong client)","Use Optional/Objects.requireNonNull around the producer call to surface the real bug"],"exampleFix":"// before\nTask<User> t = maybeGetUser(); // may return null\nUser u = Tasks.await(t, 5, TimeUnit.SECONDS);\n// after\nTask<User> t = maybeGetUser();\nif (t != null) {\n    User u = Tasks.await(t, 5, TimeUnit.SECONDS);\n}","handlingStrategy":"validation","validationCode":"if (task == null) { throw new IllegalStateException(\"Task producer returned null\"); }","typeGuard":"boolean hasTask(Task<?> t) { return t != null; }","tryCatchPattern":"try {\n    T r = Tasks.await(task, 5, TimeUnit.SECONDS);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"Task must not be null\")) { /* handle null producer */ }\n}","preventionTips":["Never allow task-producing APIs to return null; fail at the source","Objects.requireNonNull(task) immediately after obtaining it","Enable nullability annotations/lint to catch null Tasks at compile time","Log producer errors when a Task is unexpectedly absent"],"tags":["tasks","null","illegal-argument","await"],"backgroundTag":"null-argument","analyzedSha":"157c9d86ac46c195a86c2f15ab55c84036223f95","analyzedAt":"2026-09-06T17:27:33.892Z","contentChangedAt":"2026-09-06T17:27:33.892Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}