Skip to content

Unirest.config() is null #540

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

Closed
jmechamFD opened this issue Mar 3, 2025 · 3 comments
Closed

Unirest.config() is null #540

jmechamFD opened this issue Mar 3, 2025 · 3 comments

Comments

@jmechamFD
Copy link

When trying to set the cookie specs with the line: Unirest.config().cookieSpec(CookieSpecs.STANDARD) I receive the error: java.lang.NullPointerException: Cannot invoke "kong.unirest.core.Config.cookieSpec(java.lang.String)" because the return value of "kong.unirest.core.Unirest.config()" is null.

I can't find any mention online of anyone else having this error and the docs show this is how you edit the config so I'm not sure what I'm doing wrong in this case.

  • My build.gradle.kts file has this import although I've gotten this error with the most recent version v4.4.5 as well : implementation("com.konghq:unirest-java:3.14.5")
  • I just import Unirest in my class using import kong.unirest.Unirest
  • Then the call Unirest.config() will return null.
@ryber
Copy link
Collaborator

ryber commented Mar 4, 2025

There are a few strange things here.

  1. You say you are using Unirest 3.14.5 but then you also say you are using kong.unirest.core.Unirest which is not a package that exists in the 3 line. Unirest there is just kong.unirest.Unirest. Do you maybe have Unirest 4 as a transient dependency? Unirest 4 was a major overhaul so it was moved to not conflict with Unirest 3.
  2. Per the Unirest code it's mostly not possible to have a null config unless you explicitly created that state in some way. There are only two uses of the Unirest constructor in the library itself and they both provide a config object

Image

@jmechamFD
Copy link
Author

jmechamFD commented Mar 4, 2025

I apologize for the confusion. I originally was using version 3.14.5 and was getting this error. I upgraded to the latest version v4.4.5 to try and fix this error and I still got the same error and that's when I copied the Null Pointer Exception. I have verified there is only one version of Unirest being imported by my application. I currently have reverted it back to version 3.14.5.

I did some more debugging and figured out this behavior occurs exclusively in my unit tests in the methods I'm calling Unirest.config().cookieSpec(CookieSpecs.STANDARD). However this method works as expected when I run my application normally. It also appears the .config() method is not the only method returning null.

Image

@ryber
Copy link
Collaborator

ryber commented Mar 5, 2025

are you using a mocking framework? Including the Unirest mocking library?

@ryber ryber closed this as completed Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants