bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.46k stars 581 forks source link

NPE when loading a built library #584

Open bepopov opened 2 years ago

bepopov commented 2 years ago

Hello. I've found the following issue.

I built dll-library of generated JNI C++ integration code and tried to load it via Loader.loadLibrary but I got a NullPointerException. Here is the stacktrace:

java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at java.util.ArrayDeque.addFirst(ArrayDeque.java:233)
Caused by: java.lang.NullPointerException

    at java.util.ArrayDeque.push(ArrayDeque.java:508)
    at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1651)
    at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1611)
    at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1603)
    at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1597)
    at Test.<clinit>(Test.java:12)

I think it is related to method which puts cls to the related method as null:

public static String loadLibrary(URL[] urls, String libnameversion, String ... preloaded) {
    return loadLibrary(null, urls, libnameversion, preloaded);
}

public static synchronized String loadLibrary(Class<?> cls, URL[] urls, String libnameversion, String ... preloaded) {
    //...
    classStack.get().push(cls);
    //...
}

I would suggest to return some more understandable error message. The version of javacpp I've used is 1.5.7

saudet commented 2 years ago

Why/how are you making classStack null?

bepopov commented 2 years ago

Why/how are you making classStack null?

I don't make classStack null. It is because of the code of ArrayDeque:

package java.util;

public class ArrayDeque<E> extends AbstractCollection<E>
                           implements Deque<E>, Cloneable, Serializable
{
    //...
    public void push(E e) {
        addFirst(e);
    }

    public void addFirst(E e) {
        if (e == null)
            throw new NullPointerException();
        elements[head = (head - 1) & (elements.length - 1)] = e;
        if (head == tail)
            doubleCapacity();
    }
    //...
}
saudet commented 2 years ago

Oh, ok, that's a bug. We can just skip the calls to push() and pop() in that case. Mind opening a pull request with the changes?

bepopov commented 2 years ago

Oh, ok, that's a bug. We can just skip the calls to push() and pop() in that case. Mind opening a pull request with the changes?

Sure. I will create a pull request