0


Currently I have a need in local http file server in Android app and I decided to use NanoHttpd SimpleWebServer. But I encountered such exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference
        at fi.iki.elonen.SimpleWebServer.<clinit>(SimpleWebServer.java:83)

And the crashing part is actually static constructor

static {
    mimeTypes();
    InputStream stream = SimpleWebServer.class.getResourceAsStream("/LICENSE.txt");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int count;
    String text;
    try {
        while ((count = stream.read(buffer)) >= 0) {
            bytes.write(buffer, 0, count);
        }
        text = bytes.toString("UTF-8");
    } catch (IOException e) {
        text = "unknown";
    }
    LICENCE = text;
}

Apparently, library is trying to load some license file but fails. So, the server is not created and not started.
Does anybody know how to overcome this issue? Thanks in advance!

(I use Android Studio 1.5.1 and included library via gradle dependency 'org.nanohttpd:nanohttpd-webserver:2.2.0')

Alviere
  • 403
  • 1
  • 4
  • 14

1 Answers1

1

Seams InputStream wasn't created. It can be because of class loader can't find LICENSE.txt or do not have enough rights on it.

VDanyliuk
  • 1,049
  • 8
  • 23
  • Thanks for the answer! I know that already. My question is: can I do something about it? I can't edit this class, because it's dependency. – Alviere Dec 09 '15 at 08:01
  • 1
    Try to put LICENSE.txt to proper plase – VDanyliuk Dec 09 '15 at 08:04
  • Where to put it? I can't edit library's sources and can't include this file to android resources due to it's name (resource files must have only a-z,0-9 and _, no uppercase allowed). – Alviere Dec 09 '15 at 08:38