NullPointerException in 2 tests

Why am i getting null pointer expections in getStockQuoteSingle() in AlphavantageServiceTest as well as TiingoServiceTest?

Because there might be some values that are null and you are trying to reference it further in your program

Yes but the error is in test file and not in my code and I’m pretty sure we don’t change anything in test files

It may happen because this exception needs to be thrown if any situation arises. These tests might be checking some cases in which null value is generated for a variable and these values are then further used in program

Look for any such case where a null value is generated and try throwing nullpointerexception when it happens

Test file is just calling functions and checks for output of your functions with desired output. Thus you have to handle every exception in your code.
And if you’re getting any exception then there is the case which you’re missing to handle.

The error is shown as being in test file because you are running a test. If you use the debug mode instead on that test, you can see the full stacktrace in test report and that’ll help you better to pinpoint the origin of the exception.

Also, this exception means a value you have used is actually null. Maybe a null return from a method or an uninitialised array index. Or something else. You get the hint I think.

Hi i checked my whole code and there is no null pointer exception. I researched a bit and found out that Mockito tests have to be initialized using initMocks but they have not been initialized in the tests and thats why theres null pointer error.

I wont recommend modifying the test files in any manner. They work fine, regardless of the module. The error has to be somewhere in code.

I don’t think you are supposed to share the code on forum.

So i was finally able to solve the error. I was not creating instance properly

stuck on same issue, the instance of RestTemplate is it ?
or is it any other variable.

Rest template response should be in string form

yes I am doing that only result = restTemplate.getForObject(uri, String.class); but the result for this is null and hence getting null pointer exception.

Have you initialized resttemplate object?

I am using the one provided by the constructor

I mean in the PlatformManagerApplication, have you created some instance of restTemplate?

you mean as an instance variable in some method then yes.
if you mean as a global variable then no.
also no todo mentioned to make any changes to the PlatformManagerApplication class so it is the same as before.

Yes in the platform manager application, resttemplate should be a global object as it is used by various other methods.

public class PortfolioManagerApplication {
  private static RestTemplate restTemplate = new RestTemplate();
//code
}

like this ?
coz it didn’t work for me.

Yes like this and also can you show me what null pointer exception you’re getting