- Saved searches
- Use saved searches to filter your results more quickly
- Request processing failed; nested exception is java.lang.NullPointerException] with root cause #18852
- Request processing failed; nested exception is java.lang.NullPointerException] with root cause #18852
- Comments
- java-exception-root-cause
- 2. An Age Calculator App
- 3. Find the Root Cause Using Plain Java
- 4. Find the Root Cause Using Apache Commons Lang
- 5. Find the Root Cause Using Guava
- 6. Conclusion
- 401 unauthorized : [no body] with root cause in JAVA Spring
- Solution
- 401 unauthorized : [no body] with root cause in JAVA Spring
- Solution
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
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
Request processing failed; nested exception is java.lang.NullPointerException] with root cause #18852
Request processing failed; nested exception is java.lang.NullPointerException] with root cause #18852
Comments
Seeing below exception in the production logs intermittently without any proper stack-traces.
This started after i upgraded to Spring boot 2.1.9.RELEASE from 2.1.7.RELEASE
10/31/2019 18:32:39 [http-nio-5000-exec-10] [] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] — Servlet.service() for servlet [dispatcherServlet] in context with path [/appcontext] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
The text was updated successfully, but these errors were encountered:
wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Nov 4, 2019
@wilkinsona thank you for your reply. Due to absence of the stack trace even i am not able to find the cause of the exception. My application is SB Rest Application.
spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 4, 2019
Unfortunately, knowing that it is a REST application doesn’t help us. Without some code that reproduces the problem, we have no way of know where to even start looking. If you want to track this down, you are either going to have to take the time to provide us with something that reproduces the problem, or you’re going to have to track it down yourself.
If you can reproduce it outside your production environment, you could try placing a breakpoint in the Tomcat code that’s logging the error. I believe that’s done in StandardWrapperValve . Alternatively, you could place a breakpoint in the constructor of NullPointerException so that you can see where it is being created.
You also said that the problem only started occurring when you upgraded from Spring Boot 2.1.7 to 2.1.9. That may be a coincidence or it may be a clue as to the cause. You could try comparing the dependency version of those to Spring Boot releases and seeing if you can identify an upgrade in 2.1.9 that is causing the problem.
wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Nov 4, 2019
java-exception-root-cause
It’s pretty common in Java to work with nested exceptions as they can help us track the source of an error.
When we deal with these kinds of exceptions, sometimes we may want to know the original problem that caused the exception so our application can respond differently for each case. This is especially useful when we work with frameworks that wrap the root exceptions into their own.
In this short article, we’ll show how to get the root cause exception using plain Java as well as external libraries such as Apache Commons Lang and Google Guava.
2. An Age Calculator App
Our application will be an age calculator that tells us how old a person is from a given date received as String in ISO format. We’ll handle 2 possible error cases when parsing the date: a poorly-formatted date and a date in the future.
Let’s first create exceptions for our error cases:
static class InvalidFormatException extends DateParseException < InvalidFormatException(String input, Throwable thr) < super("Invalid date format: " + input, thr); >> static class DateOutOfRangeException extends DateParseException < DateOutOfRangeException(String date) < super("Date out of range: " + date); >>
Both exceptions inherit from a common parent exception that will make our code a bit clearer:
static class DateParseException extends RuntimeException < DateParseException(String input) < super(input); >DateParseException(String input, Throwable thr) < super(input, thr); >>
After that, we can implement the AgeCalculator class with a method to parse the date:
static class AgeCalculator < private static LocalDate parseDate(String birthDateAsString) < LocalDate birthDate; try < birthDate = LocalDate.parse(birthDateAsString); >catch (DateTimeParseException ex) < throw new InvalidFormatException(birthDateAsString, ex); >if (birthDate.isAfter(LocalDate.now())) < throw new DateOutOfRangeException(birthDateAsString); >return birthDate; > >
As we can see, when the format is wrong we wrap the DateTimeParseException into our custom InvalidFormatException.
Finally, let’s add a public method to our class that receives the date, parses it and then calculates the age:
public static int calculateAge(String birthDate) < if (birthDate == null || birthDate.isEmpty()) < throw new IllegalArgumentException(); >try < return Period .between(parseDate(birthDate), LocalDate.now()) .getYears(); >catch (DateParseException ex) < throw new CalculationException(ex); >>
As shown, we’re wrapping the exceptions again. In this case, we wrap them into a CalculationException that we have to create:
static class CalculationException extends RuntimeException < CalculationException(DateParseException ex) < super(ex); >>
Now, we’re ready to use our calculator by passing it any date in ISO format:
AgeCalculator.calculateAge("2019-10-01");
And if the calculation fails, it would be useful to know what the problem was, wouldn’t it? Keep reading to find out how we can do that.
3. Find the Root Cause Using Plain Java
The first way we’ll use to find the root cause exception is by creating a custom method that loops through all the causes until it reaches the root:
public static Throwable findCauseUsingPlainJava(Throwable throwable) < Objects.requireNonNull(throwable); Throwable rootCause = throwable; while (rootCause.getCause() != null && rootCause.getCause() != rootCause) < rootCause = rootCause.getCause(); >return rootCause; >
Notice that we’ve added an extra condition in our loop to avoid infinite loops when handling recursive causes.
If we pass an invalid format to our AgeCalculator, we’ll get the DateTimeParseException as the root cause:
try < AgeCalculator.calculateAge("010102"); >catch (CalculationException ex)
However, if we use a future date we’ll get a DateOutOfRangeException:
try < AgeCalculator.calculateAge("2020-04-04"); >catch (CalculationException ex)
Furthermore, our method also works for non-nested exceptions:
In this case, we get an IllegalArgumentException since we passed in null.
4. Find the Root Cause Using Apache Commons Lang
We’ll now demonstrate finding the root cause using third-party libraries instead of writing our custom implementation.
Apache Commons Lang provides an ExceptionUtils class which provides some utility methods to work with exceptions.
We’ll use the getRootCause() method with our previous example:
try < AgeCalculator.calculateAge("010102"); >catch (CalculationException ex)
We get the same root cause as before. The same behavior applies to the other examples that we’ve listed above.
5. Find the Root Cause Using Guava
The last way we’re going to try is by using Guava. Similar to Apache Commons Lang, it provides a Throwables class with a getRootCause() utility method.
Let’s try it out with the same example:
try < AgeCalculator.calculateAge("010102"); >catch (CalculationException ex)
The behavior is exactly the same as with the other methods.
6. Conclusion
In this article, we’ve demonstrated how to use nested exceptions in our application and implemented a utility method to find the root cause exception.
We’ve also shown how to do the same by using third-party libraries like Apache Commons Lang and Google Guava.
As always, the full source code for the examples is available over on GitHub.
401 unauthorized : [no body] with root cause in JAVA Spring
I saw some questions related to this but none well able to solve my problem.
public String getAuth(String client_id, String app_secret) < String auth = client_id + ":" + app_secret; return Base64.getEncoder().encodeToString(auth.getBytes()); >@GetMapping(value = "/token") public Object generateAccessToken() < String auth = this.getAuth( "CLIENT_ID", "APP_SECRET" ); RestTemplate restTemplate = new RestTemplate(); String base = "https://external-api.com"; HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Basic " + auth); MultiValueMaprequestParams = new LinkedMultiValueMap<>(); requestParams.add("Grant_type", "client_credentials"); ResponseEntity response = restTemplate.postForEntity( base + "/v1/oauth2/token", requestParams, Object.class, headers ); return response.getBody(); >
Solution
Here’s the solution to my own question.
This is what I had to change;
MultiValueMap requestBody = new LinkedMultiValueMap<>(); requestBody.add("grant_type", "client_credentials"); HttpEntity request = new HttpEntity<>(requestBody, headers); ResponseEntity response = restTemplate.postForEntity( base +"/v1/oauth2/token", request, String.class ); // check response if (response.getStatusCode() == HttpStatus.OK) < System.out.println("Request Successful"); System.out.println(response.getBody()); >else < System.out.println("Request Failed"); System.out.println(response.getStatusCode()); >JSONObject object = new JSONObject(response.getBody()); return object.getString("access_token"); >
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0
401 unauthorized : [no body] with root cause in JAVA Spring
I saw some questions related to this but none well able to solve my problem.
public String getAuth(String client_id, String app_secret) < String auth = client_id + ":" + app_secret; return Base64.getEncoder().encodeToString(auth.getBytes()); >@GetMapping(value = "/token") public Object generateAccessToken() < String auth = this.getAuth( "CLIENT_ID", "APP_SECRET" ); RestTemplate restTemplate = new RestTemplate(); String base = "https://external-api.com"; HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Basic " + auth); MultiValueMaprequestParams = new LinkedMultiValueMap<>(); requestParams.add("Grant_type", "client_credentials"); ResponseEntity response = restTemplate.postForEntity( base + "/v1/oauth2/token", requestParams, Object.class, headers ); return response.getBody(); >
Solution
Here’s the solution to my own question.
This is what I had to change;
MultiValueMap requestBody = new LinkedMultiValueMap<>(); requestBody.add("grant_type", "client_credentials"); HttpEntity request = new HttpEntity<>(requestBody, headers); ResponseEntity response = restTemplate.postForEntity( base +"/v1/oauth2/token", request, String.class ); // check response if (response.getStatusCode() == HttpStatus.OK) < System.out.println("Request Successful"); System.out.println(response.getBody()); >else < System.out.println("Request Failed"); System.out.println(response.getStatusCode()); >JSONObject object = new JSONObject(response.getBody()); return object.getString("access_token"); >
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0