Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.concurrent.ExecutionException: java.lang.NullPointerException Error

The following code snippet doesnt throw any error when executed in a standalone mode. When I deploy this into a web server [implementing a server's interface and added as JAR into classpath], I get

 java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.nbis.process.JSON_2_File_Split_V004.fn_1_parserEntity(JSON_2_File_Split_V004.java:256)
at com.nbis.process.JSON_2_File_Split_V004.fn_0_primaryCaller(JSON_2_File_Split_V004.java:177)
at com.nbis.process.JSON_2_File_Split_V004.execute(JSON_2_File_Split_V004.java:151)

Code Snippet:

this.callable = new JSON_3_File_Process_V005(this.originalFileName, this.inProgressDirLoc, this.processedDirLoc, "[" + jSONRecord.toString() + "]", this.dataMDHolder, this.dataAccIDValueMap, this.dataCountryNameValueMap);
String[] fullContent = null;    
try {
    fullContent = executor.submit(this.callable).get();
} catch (ExecutionException e) {
    StringWriter errors = new StringWriter();
    e.printStackTrace(new PrintWriter(errors));
    log.info("Srii: " + errors.toString());
    executor.shutdown();
    return 7;
    } 

Adding the get's return value to an ExecutorCompletionService would be an option, but would that not kill the concept of asynchronous processing? In other words, I collect in StringBuilder the output string from callable get and store to a disk when the get count reaches a specific number. Once data is emitted to disk, I refresh the StringBuilder. This way I push data to a disk at regular intervals without having to keep them in memory.

Any suggestions as to what wrong am I doing here? Appreciate any inputs. Thank you.

like image 734
Srii Avatar asked Oct 07 '13 16:10

Srii


People also ask

How do I fix Java Lang NullPointerException in Java?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

How do I get rid of NullPointerException?

How to avoid the NullPointerException? To avoid the NullPointerException, we must ensure that all the objects are initialized properly, before you use them. When we declare a reference variable, we must verify that object is not null, before we request a method or a field from the objects.

What does it mean when it says Java Lang NullPointerException?

The java. lang. NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.

What is an execution exception?

Overview. ExecutionException is a checked exception that extends the Exception class. This exception is thrown by an instance of FutureTask that encounters an Exception or Error (both derivates of Throwable ) that remain unhandled by user code and, subsequently an attempt is made to retrieve the result of such a task.


2 Answers

This is fixed. If it could useful:

The problem was with the way the variables were declared. I had to declare a class-level variable as static so the any changes applied to this one started reflecting everywhere else. Strangely enough, I dint see the problem when it was executed stand-alone.

EDIT on 13112019: Moving my comment to the answer section, on request:

As its quite long time back, I dont recollect exactly the variable details. But I believe it is one of the following: this.originalFileName, this.inProgressDirLoc, this.processedDirLoc , this.dataMDHolder, this.dataAccIDValueMap, this.dataCountryNameValueMap I had to set them as static as values assigned [or modified] by any of the member was not reflecting during references of the variable within the class.

like image 161
Srii Avatar answered Nov 07 '22 02:11

Srii


Similar issue I faced in spring rest project. I was using @Autowired annotation to inject an object inside Callable Thread class.

That object was causing Null Pointer exception sometimes. Then removing the @Autowired annotation and passing that object in constructor while creating callable thread class object fixed my issue.

like image 45
Abhishek Singh Avatar answered Nov 07 '22 02:11

Abhishek Singh