title: Spring Gateway RouteLocator null Pointer exception
body: <pre><code>import java.net.URISyntaxException;
  
  import javax.validation.constraints.NotNull;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.cloud.gateway.route.Route;
  import org.springframework.cloud.gateway.route.RouteDefinition;
  import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
  import org.springframework.cloud.gateway.route.RouteLocator;
  import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
  import org.springframework.stereotype.Component;
  import org.springframework.util.StringUtils;
  import reactor.core.publisher.Flux;
  import reactor.core.publisher.Mono;
  
  @Component
  public class RefreshableRoutesLocator implements RouteLocator {
  
      private RouteLocatorBuilder builder;
      private RouteLocatorBuilder.Builder routesBuilder;
      private Flux&lt;Route&gt; route;
  
      @Autowired
      GatewayRoutesRefresher gatewayRoutesRefresher;
  
      @Autowired
      private RouteDefinitionWriter routeDefinitionWriter;
  
      public void RefreshRouteLocator(RouteLocatorBuilder builder) {
          this.builder = builder;
          clearRoutes();
      }
  
      public void clearRoutes() {
          routesBuilder = builder.routes();
      }
  
      public RefreshableRoutesLocator addRoute(@NotNull final String id, @NotNull final String path,
              @NotNull final URI uri) throws URISyntaxException {
          System.out.println("-----------");
          if (StringUtils.isEmpty(uri.getScheme())) {
              throw new URISyntaxException("Missing scheme in URI: {}", uri.toString());
          }
          routesBuilder.route(id, fn -&gt; fn.path(path + "/**").uri(uri));
          return this;
      }
  
      public RefreshableRoutesLocator addRoute(@NotNull RouteDefinition definition) {
          routeDefinitionWriter.save(Mono.just(definition)).subscribe();
          return this;
      }
  
      public void buildRoutes() {
          this.route = routesBuilder.build().getRoutes();
          gatewayRoutesRefresher.refreshRoutes();
      }
  
      @Override
      public Flux&lt;Route&gt; getRoutes() {
          return route;
      }
  } 
  </code></pre>
  
  <p>I am using RefreshableRoutesLocator to load routes on spring startup from database but while starting i am getting exception </p>
  
  <pre><code>reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.NullPointerException: The mapper returned a null Publisher
  Caused by: java.lang.NullPointerException: The mapper returned a null Publisher
  </code></pre>
  
  <p>On checking I found that route is null at start :</p>
  
  <pre><code>private Flux&lt;Route&gt; route;
  </code></pre>
  
  <p>Can someone help me to fix this, My Agenda is to load Routes dynamically from Database into spring gateway at startup event.</p>