I have a little problem with a NullPointerException that I can't really understand.
My code runs 24/7 and works pretty well but I have that exception that pops randomly from 1 day to 1 week after the application is started.
Here is the stacktrace :
java.lang.NullPointerException
at java.util.LinkedList.get(LinkedList.java:477)
at com.ch4process.acquisition.ScenarioWorker.eventHandling(ScenarioWorker.java:97)
at com.ch4process.acquisition.ScenarioWorker.call(ScenarioWorker.java:79)
at com.ch4process.acquisition.ScenarioWorker.call(ScenarioWorker.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
As you can see this exception is rised in a thread.
Here is the code (simplified a bit) :
public class ScenarioWorker implement Callable<Integer>
{
List<SignalValueEvent> eventList = new LinkedList<>();
boolean busy = false;
@Override
public Integer call() throws Exception
{
try
{
while (true)
{
eventHandling();
Thread.sleep(1000);
}
}
catch (Exception ex)
{
// Redirects the exception to a custom class
}
}
private void eventHandling()
{
if (! busy)
{
while (eventList.size() > 0)
{
SignalValueEvent event = eventList.get(0); // NullPointerException here according to stacktrace
if(event.isTriggered()))
{
busy = true;
doScenario(event);
}
deleteEvent();
}
}
}
private void deleteEvent()
{
try
{
eventList.remove(0);
}
catch (Exception ex)
{
// Redirects the exception to custom class
}
finally
{
busy = false;
}
}
@Override
public void SignalValueChanged(SignalValueEvent event)
{
if (event.isValid())
{
eventList.add(event);
}
}
}
[Edit : the line 97 in the stacktrace is the line saying SignalValueEvent event = eventList.get(0); ]
This class implements an interface which allows another class to notify it by calling the SignalValueChanged method.
So basically my LinkedList is created at the initialization of the class, filled by an external class whenever an event needs to be put in the list, and the call method loops on the list to see if there's anything in it. If there is, it's treated and the event is deleted.
I've tested this and the only reason I should have a NPException is if my list is equal to null... but I don't do that anywhere in my code...
As I said this code is working 24/7 and I had this bug almost one week and a half after I started the application. Is it something obvious I am missing ?
Thank you so much for reading this and if you can help me I'll be glad :)
ScenarioWorker.java:972) Is the code failing at every run now, or intermittently? 3) If intermittently, then this suggests a concurrency problem. Are you sure that your code is thread safe?