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

[Bug]: NPE on ApiException.getMessage() #23

Open
1 task done
luis901101 opened this issue Jan 26, 2023 · 1 comment
Open
1 task done

[Bug]: NPE on ApiException.getMessage() #23

luis901101 opened this issue Jan 26, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@luis901101
Copy link

What happened?

When an ApiException is thrown and exception.printStackTrace() or just exception.getMessage() is called, then a NullPointerException is thrown due to this line in the getMessage() function implementation:

public String getMessage() {
        return String.format("Message: %s%nHTTP response code: %s%nHTTP response body: %s%nHTTP response headers: %s", super.getMessage(), this.getCode(), this.getResponseBody(), this.getResponseHeaders().toString());
    }

If this.getResponseHeaders() is null, then .toString() throws NPE.

Steps to reproduce?

When sending a notification fails:

apiClient.createNotificationAsync(
                notification,
                new ApiCallback<>() {
                    @Override
                    public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
                        logger.info("Notification send failed with status code: "+ statusCode);
                        if(e != null) e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(CreateNotificationSuccessResponse result, int statusCode, Map<String, List<String>> responseHeaders) {
                        System.out.println("Push notification sent: " + result.toJson());
                    }

                    @Override
                    public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {

                    }

                    @Override
                    public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {

                    }
                }
            );

What did you expect to happen?

exception.printStackTrace() or exception.getMessage() should not throw NPE.

Relevant log output

Exception in thread "OkHttp Dispatcher" java.lang.NullPointerException: Cannot invoke "java.util.Map.toString()" because the return value of "com.onesignal.client.ApiException.getResponseHeaders()" is null
	at com.onesignal.client.ApiException.getMessage(ApiException.java:164)
	at java.base/java.lang.Throwable.getLocalizedMessage(Throwable.java:397)
	at java.base/java.lang.Throwable.toString(Throwable.java:496)
	at java.base/java.lang.String.valueOf(String.java:4218)
	at java.base/java.io.PrintStream.println(PrintStream.java:1047)
	at java.base/java.lang.Throwable$WrappedPrintStream.println(Throwable.java:763)
	at java.base/java.lang.Throwable.printStackTrace(Throwable.java:670)
	at java.base/java.lang.Throwable.printStackTrace(Throwable.java:659)
	at java.base/java.lang.Throwable.printStackTrace(Throwable.java:650)
	at com.myapp.backend.notification.client.NotificationClient$2.onFailure(NotificationClient.java:97)
	at com.onesignal.client.ApiClient$1.onResponse(ApiClient.java:1038)
	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@luis901101 luis901101 added the bug Something isn't working label Jan 26, 2023
@carlos-contreras14
Copy link

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants