d s
d s

Reputation: 387

vertx.createHttpServer() throws NullPointerException

I am playing a bit around with vertx.

Why does this code throws a NullPointerException?

import io.vertx.core.http.HttpServer;
import io.vertx.core.AbstractVerticle;

class vertxFacadeTest extends AbstractVerticle
{

    @Test
    void createServer()
    {
        HttpServer httpServer = vertx.createHttpServer();
    }
}


java.lang.NullPointerException
at com.webinterface.vertxFacadeTest.createServer(vertxFacadeTest.java:23) //line 23 is this here: HttpServer httpServer = vertx.createHttpServer();

Has anybody an idea why? Looking forward :-)

Upvotes: 0

Views: 389

Answers (1)

dano
dano

Reputation: 94881

Your Vertx instance is null. Right now your class extends AbstractVerticle, which means that it has a vertx instance variable, but that is not initialized in its constructor; it's only set to a real value in the AbstractVerticle.init() method, which is only called by Vert.x when you deploy the Verticle. So in your case, when JUnit instantiates the class, vertx is null.

You shouldn't have the test class extend AbstractVerticle. Instead, create the Vertx instance explicitly in the test class, probably using a method annotated with @Before. See the documentation on Vert'x integration with JUnit here.

Upvotes: 3

Related Questions