NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.9k stars 1.69k forks source link

NPE with simple text response in RouterNanoHTTPD #543

Open cmarchand opened 5 years ago

cmarchand commented 5 years ago

This is probably just a simple question, but I can't find any solution? Here me simple code, using 2.3.1 :

public class RestServer extends RouterNanoHTTPD {
    private final Notifiee notifiee;
    private RestServer(int port, Notifiee notifiee) {
        super(port);
        this.notifiee = notifiee;
        addRoute("/ping", PingHandler.class);
        addRoute("/stop", StopHandler.class);
    }
    public static class StopHandler extends DefaultHandler {
        public StopHandler() {
            super();
        }
        @Override
        public String getText() { 
            RestServer.getInstance().notifiee.askStop();
            return "Stop ask";
        }
        @Override
        public Response.IStatus getStatus() { 
            return Response.Status.OK;
        }
        @Override
        public String getMimeType() { 
            return MIME_PLAINTEXT;
        }
    }
}

When I call http://localhost:port/stop, I get this :

java.lang.NullPointerException
    at fi.iki.elonen.router.RouterNanoHTTPD$UriRouter.process(RouterNanoHTTPD.java:596)
    at fi.iki.elonen.router.RouterNanoHTTPD.serve(RouterNanoHTTPD.java:671)
    at fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoHTTPD.java:945)
    at fi.iki.elonen.NanoHTTPD$ClientHandler.run(NanoHTTPD.java:192)
    at java.base/java.lang.Thread.run(Thread.java:834)

But notifiee.stop() is correctly call, I get response in browser. Just the NPE is thrown. Any help will be much appreciated.

sandrock commented 4 years ago

Same here. Cant' find what that is about.