0

I am searching a way to catch an NullPointerException in a Runnable class. I have this method called by run :

private void cooccurrence(int contextIndex, int targetIndex) {
        try {
            Double old_value = X.get(contextIndex, targetIndex);
            X.set(contextIndex, targetIndex, old_value + 1);
        }catch (NullPointerException e) {
            X.growData(X.nz_length + 1);
            X.addItem(contextIndex, targetIndex,  1);
        }
}

and I have this code in another class which call a class ThreadForX implements Runnable :

final BlockingQueue<JSONObject> queue = new
ArrayBlockingQueue<>(5000000);
int json_object_in_queue = 0;

ExecutorService pool = Executors.newFixedThreadPool(2);

DMatrixSparseTriplet X = new DMatrixSparseTriplet(200000,200000,0);

JSONObject inQueue_jsonObject;
while ((inQueue_jsonObject = queue.poll()) != null) {
        Runnable readTweet = new ThreadForX(contextLenght, inQueue_jsonObject, X, final_vocabulary);
        pool.execute(readTweet);
}
pool.shutdown();
while (!pool.isTerminated()) { }

As you can see, the NullPointerException is really important to add an item so I need the values of contextIndex and targetIndex

  • 2
    You should never have to catch a `NullPointerException`. Just write code that ensures it never happens. – Michael Jul 29 '17 at 11:33
  • That's crazy, trying to catch a NullPointerException. Closing as a duplicate because your question just means that you don't yet understand this exception fully enough. – Hovercraft Full Of Eels Jul 29 '17 at 11:45
  • Let me tell you why I do this : EJML `DMatrixSparseTriplet` need to know the number of non-zero value at first. Then, you can grow the size of non value element and add an item. (Actually it is a `nz_data` is a `Element[]`. BUT i can't know the number of non-zero value at first. That's why I to get/set and if it's failed because it doesnt exist then I grow and add non-zero item. – Geoffrey Guettier Jul 29 '17 at 22:09

0 Answers0