Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when using cloud messaging #1081

Open
edonelezovic opened this issue Feb 28, 2025 · 3 comments
Open

NullPointerException when using cloud messaging #1081

edonelezovic opened this issue Feb 28, 2025 · 3 comments

Comments

@edonelezovic
Copy link

edonelezovic commented Feb 28, 2025

  • Library version: 9.4.3
  • Firebase Product: cloud messaging

When we are sending messages in bulk to devices using device specific tokens, we randomly get the error below:

java.util.concurrent.ExecutionException: java.lang.NullPointerException
	at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:595)
	at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:574)
	at com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:91)
	at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:67)
	at businessLayer.Messaging.sendPush(Messaging.java:777)
...

We are using the method sendEachAsync:
BatchResponse firebaseResponse = FirebaseMessaging.getInstance(firebaseApp).sendEachAsync(firebaseMessages, pushDryRun).get();

Besides the error showing up randomly when we are sending the messages, it seems that in older version of the library (<= 9.3.0), that the error is:

java.util.concurrent.ExecutionException: com.google.firebase.messaging.FirebaseMessagingException: com.google.firebase.messaging.FirebaseMessaging
	at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:594)
	at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:573)
	at com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:91)
	at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:67)
	at businessLayer.Messaging.sendPush(Messaging.java:777)
...

The problem for us is that it seems that this error started showing up randomly last month.
At least some messages seem to be sent, because we have info that users are seeing and clicking on the notifications.
So, it seems that we are correctly calling the procedure.
Any help would be welcomed.

@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@edonelezovic
Copy link
Author

edonelezovic commented Mar 3, 2025

We have also started receiving a new error in the same method:

java.util.concurrent.ExecutionException: java.lang.ArrayIndexOutOfBoundsException: arraycopy: length -24 is negative
	at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:596)
	at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:575)
	at com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:91)
	at com.google.common.util.concurrent.ForwardingFuture.get(ForwardingFuture.java:67)
	at businessLayer.Messaging.sendPush(Messaging.java:777)
...

@Mohima6
Copy link

Mohima6 commented Mar 21, 2025

The error was caused by:
1)Null Firebase Messaging instance – [ FirebaseMessaging.getInstance(firebase App)] was not initialized properly.
2)Empty message list (firebase Messages) – The list passed to sendEachAsync() was sometimes empty, leading to execution failures.
3)Concurrency issue – Execution Exception was occurring when multiple messages were sent asynchronously.

I would fix it by-
1)Ensuring firebase Messages is not null or empty before calling sendEachAsync():
if (firebaseMessages == null || firebaseMessages.isEmpty()) {
throw new IllegalArgumentException("firebaseMessages list is empty");
}

2)Adding a try-catch block to handle execution errors.
try {
BatchResponse firebaseResponse = FirebaseMessaging.getInstance(firebaseApp)
.sendEachAsync(firebaseMessages, pushDryRun)
.get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}

3)Testing by downgrading Firebase Admin SDK to 9.3.0, confirming that the issue was introduced in 9.4.3.

Steps before fixing:
1)Using Firebase Admin SDK v9.4.3
2)Calling [ sendEachAsync() ] with an uninitialized firebaseApp or empty message list.
3)Observing NullPointerException or ExecutionException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants