HomeEducationThe right way to Remedy Java NullPointerException | Harness Acquire US

The right way to Remedy Java NullPointerException | Harness Acquire US

On this publish, we spotlight the shortcomings of counting on stack traces alone for investigating Java NullPointerExceptions.

For those who’ve ever been annoyed with an exception, you’ve reached the proper place.

On this publish, we spotlight the shortcomings of counting on stack traces alone for investigating Java NullPointerExceptions. Though you get the road from which the exception was thrown, understanding if it’s new, why it occurred, and who launched the change that triggered it’s a complete completely different ball sport.

Let’s roll.

Contents hide
1 The Typical NullPointerException Decision Workflow
1.1 java.lang.NullPointerException: null at [GetUserBillingServlet.class:na] at [GetUserBillingServlet.class:na] at [AppServicesProtoServlet.class:na] at [AppServicesProtoServlet.class:na] at com.sparktale.bugtale.server.widespread.servlet.CommonServlet.handleRequest( [CommonServlet.class:na] at com.sparktale.bugtale.server.widespread.servlet.CommonServlet.doPost( [CommonServlet.class:na] at javax.servlet.http.HttpServlet.service( [servlet-api.jar:na] at javax.servlet.http.HttpServlet.service( [servlet-api.jar:na] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter( [catalina.jar:7.0.42] at org.apache.catalina.filters.ExpiresFilter.doFilter( [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter( [catalina.jar:7.0.42] at org.apache.catalina.filters.AddDefaultCharsetFilter.doFilter( [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [catalina.jar:7.0.42] at org.apache.catalina.core.ApplicationFilterChain.doFilter( [catalina.jar:7.0.42] at org.apache.catalina.core.StandardWrapperValve.invoke( [catalina.jar:7.0.42] at org.apache.catalina.core.StandardContextValve.invoke( [catalina.jar:7.0.42] at org.apache.catalina.authenticator.AuthenticatorBase.invoke( [catalina.jar:7.0.42] at com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve.invoke( [tomcat-redis-session-manager-1.2.jar:na] at org.apache.catalina.core.StandardHostValve.invoke( [catalina.jar:7.0.42] at org.apache.catalina.valves.ErrorReportValve.invoke( [catalina.jar:7.0.42] at ch.qos.logback.entry.tomcat.LogbackValve.invoke( [logback-access-1.1.2.jar:na] at org.apache.catalina.core.StandardEngineValve.invoke( [catalina.jar:7.0.42] at org.apache.catalina.connector.CoyoteAdapter.service( [catalina.jar:7.0.42] at org.apache.coyote.http11.AbstractHttp11Processor.course of( [tomcat-coyote.jar:7.0.42] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.course of( [tomcat-coyote.jar:7.0.42] at org.apache.tomcat.util.internet.NioEndpoint$ [tomcat-coyote.jar:7.0.42] at java.util.concurrent.ThreadPoolExecutor.runWorker( [na:1.7.0_65] at java.util.concurrent.ThreadPoolExecutor$ [na:1.7.0_65] at [na:1.7.0_65]

The Typical NullPointerException Decision Workflow

Whereas the problem we’re protecting right here isn’t unique to NullPointerExceptions, it makes easy instance. In spite of everything, they’re the most typical exception in Java manufacturing environments.

Let’s assume a NullPointerException simply occurred, how are you made conscious of it?

  • Worst case – your clients are negatively impacted and your crew is made conscious of it by means of an offended stream of tweets.
  • Finest case – it fails considered one of your checks and also you’re in a position to cease if from reaching manufacturing.
  • The widespread case – exceptions occur left and proper however you don’t know in the event that they’re new or vital.

For the aim of this train, let’s assume that we have now an exception in our fingers that we’re tasked with fixing so identification is out of the way in which (for now). The start line of the investigation part would typically be your software logs and the the exception’s corresponding stack hint. There’s additionally the likelihood that the exception wasn’t logged – we prefer to name these the silent killers of Java functions.

Let’s work with the perfect case state of affairs, assuming the exception was certainly logged:

java.lang.NullPointerException: null
        at [GetUserBillingServlet.class:na]
        at [GetUserBillingServlet.class:na]
        at [AppServicesProtoServlet.class:na]
        at [AppServicesProtoServlet.class:na]
        at com.sparktale.bugtale.server.widespread.servlet.CommonServlet.handleRequest( [CommonServlet.class:na]
        at com.sparktale.bugtale.server.widespread.servlet.CommonServlet.doPost( [CommonServlet.class:na]
        at javax.servlet.http.HttpServlet.service( [servlet-api.jar:na]
        at javax.servlet.http.HttpServlet.service( [servlet-api.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.filters.ExpiresFilter.doFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.filters.AddDefaultCharsetFilter.doFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter( [catalina.jar:7.0.42]
        at org.apache.catalina.core.StandardWrapperValve.invoke( [catalina.jar:7.0.42]
        at org.apache.catalina.core.StandardContextValve.invoke( [catalina.jar:7.0.42]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke( [catalina.jar:7.0.42]
        at com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve.invoke( [tomcat-redis-session-manager-1.2.jar:na]
        at org.apache.catalina.core.StandardHostValve.invoke( [catalina.jar:7.0.42]
        at org.apache.catalina.valves.ErrorReportValve.invoke( [catalina.jar:7.0.42]
        at ch.qos.logback.entry.tomcat.LogbackValve.invoke( [logback-access-1.1.2.jar:na]
        at org.apache.catalina.core.StandardEngineValve.invoke( [catalina.jar:7.0.42]
        at org.apache.catalina.connector.CoyoteAdapter.service( [catalina.jar:7.0.42]
        at org.apache.coyote.http11.AbstractHttp11Processor.course of( [tomcat-coyote.jar:7.0.42]
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.course of( [tomcat-coyote.jar:7.0.42]
        at org.apache.tomcat.util.internet.NioEndpoint$ [tomcat-coyote.jar:7.0.42]
        at java.util.concurrent.ThreadPoolExecutor.runWorker( [na:1.7.0_65]
        at java.util.concurrent.ThreadPoolExecutor$ [na:1.7.0_65]
        at [na:1.7.0_65]

Now, let’s clear the noise and strip down the third occasion code to stick with probably the most related info:

java.lang.NullPointerException: null
        at [GetUserBillingServlet.class:na]
        at [GetUserBillingServlet.class:na]
        at [AppServicesProtoServlet.class:na]
        at [AppServicesProtoServlet.class:na]
        at com.sparktale.bugtale.server.widespread.servlet.CommonServlet.handleRequest( [CommonServlet.class:na]
        at com.sparktale.bugtale.server.widespread.servlet.CommonServlet.doPost( [CommonServlet.class:na]

We see there’s a NullPointerException on line quantity 64 within the GetUserBillingServlet class.

Once we observe by means of and study the code, there are two attainable eventualities. The snakes and ladders of debugging:

1. We’re in luck, there’s just one worth that would’ve been null on that line and possibly we additionally logged it in a number of completely different spots within the code so we will slender down on the problematic step. One thing like:

if( person.isCustomer() )

The “person” object is unquestionably the supply of bother.

2. Murphy’s legislation. If one thing can go mistaken, it can go mistaken. Think about the next if assertion:

if( person.isCustomer() && account.equals(id) )

Now we’re undecided if it’s the “person” or “account” who’re null and we’re caught.

Let’s look into some attainable options that may assist us advance the investigation.

Resolution #1: Breaking Down Advanced Strains of Code

Within the above instance, the if assertion might have been damaged all the way down to:

if ( person.isCustomer() &&
     account.equals(id) )

The stack hint would come with the suitable line quantity and allow us to transfer ahead sooner. That is additionally why splitting combination operations on streams is an efficient follow.

In actual fact, some fashion guides insist on the identical precept additionally for readability points. Take a look at the publish the place we in contrast Java fashion guides from firms like Google, Twitter and Mozilla (and Pied Piper).

Resolution #2: Extra Null Checks

That is most likely the obvious answer, conserving the nulls at verify and ensuring no rogue values go to vital areas. Code full of null checks is just not fairly, however generally it’s a essential evil.

In a earlier publish about JVM JIT optimization strategies we elaborated on how the JVM makes use of the widespread lure mechanism to work round presumably redundant null checks that have an effect on efficiency.

Resolution #3: Increased Verbosity Logging

If there’s an exception, there’s often a log message which accommodates extra hints. Whether or not it can comprise helpful info or not is a special story.

The subsequent step could possibly be so as to add info to the message or add extra log statements that may shine some gentle on the trail to the… explosion. Which creates the debugging paradox – hoping the error would occur once more to make it cease from occurring once more.

For extra strategies to debug manufacturing servers at scale, take a look at this publish on the Excessive Scalability weblog (which is a superb useful resource for something associated to excessive scale techniques).

Resolution #4: Undertake a Steady Reliability mindset

Bettering code high quality and making certain software reliability are robust issues to unravel. We made a number of assumptions on this publish to make issues simpler however as you already know, software errors are a way more advanced downside in actuality.

Steady Reliability (CR) helps outline a brand new method for making certain software program high quality in Steady Integration (CI) and Steady Supply (CD) pipelines. Serving to promote “Shift Left”, “Shift Proper” and Developer Productiveness initiatives by introducing structured practices for figuring out and resolving vital software program points, based mostly on high quality gates, software observability, and contextual suggestions loops.

At OverOps, we’re laser targeted on making the imaginative and prescient of Steady Reliability a actuality throughout the scope of mission-critical Java and .NET based mostly functions. Each time an exception, logged error or warning happens, OverOps captures and analyzes it, serving to prioritize it and offering a snapshot with the entire variable state from the second of error with the code that triggered it.

This fashion, irrespective of the problem, figuring out, prioritizing and resolving it solely takes minutes:

Java NullPointerException Evaluation

NullPointerExceptions aren’t going wherever anytime quickly. That’s why it’s vital to have technique in place to determine, prioritize and resolve them!

#Remedy #Java #NullPointerException #Harness

Continue to the category


Please enter your comment!
Please enter your name here

- Advertisment -spot_img

Most Popular

Recent Comments