2

I am new to java, i am running a local server (jar) and trying to hit an API endpoint. All data is correct, and I am getting below : java.util.NoSuchElementException: No value present The execption seems to be occuring on the below line in the code snippet .get()

any help is greatly appreciated!!

Using java8

final List<BundleValue> existingBundleValues = bundles.stream()
                                                              .filter(bundle -> bundle.type().equals(bundleType))
                                                              .findFirst()
                                                              .get()
                                                              .values()
                                                              .stream()
                                                              .map(BundleValue::convertFromAbstractBundleValue)
                                                              .collect(Collectors.toList());



java.util.NoSuchElementException: No value present
        at java.util.Optional.get(Optional.java:135)
        at com.groupon.goods.productbundlingservice.service.BundlingService.isNewBundle(BundlingService.java:105)
        at com.groupon.goods.productbundlingservice.resources.ProductBundlingServiceResource.createBundle(ProductBundlingServiceResource.java:119)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
        at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
        at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650)
        at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:34)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
        at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:45)
        at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:39)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
        at com.groupon.jtier.bundles.ctx.CtxFilter.doFilter(CtxFilter.java:56)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
        at com.groupon.jtier.bundles.debug.StenoLoggerFilter.doFilter(StenoLoggerFilter.java:118)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
        at com.groupon.jtier.bundles.debug.RequestServletFilter.doFilter(RequestServletFilter.java:46)
        at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
        at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
        at com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:241)
        at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:52)
        at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:455)
        at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:68)
        at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:169)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
        at org.eclipse.jetty.server.Server.handle(Server.java:561)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:334)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104)
        at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:679)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:597)
        at java.lang.Thread.run(Thread.java:748)
user3903418
  • 143
  • 1
  • 1
  • 11

2 Answers2

3

firstFirst returns an Optional, that you call get on, if that is an empty Optional - you will get that Exception.

You could write it as follows, where if it is present (that Optional) you would map it to Map::values (I assume that your findFirst returns some kind of a Map), otherwise get a Collections.emptyList().

bundles.stream()
       .filter(bundle -> bundle.type().equals(bundleType))
       .findFirst()
       .map(Map::values)
       .orElse(Collections.emptyList())
       .stream()
       .map(BundleValue::convertFromAbstractBundleValue)
       .collect(Collectors.toList());
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Thanks Eugene. But, I get error -" A Non-static method cannot be referenced from a static context" when I try to do that – user3903418 Jul 16 '19 at 11:48
  • @user3903418 where exactly do you get that? also are you using `Eclipse` or `javac`? – Eugene Jul 16 '19 at 11:50
  • I am using Intellij dea (similar to Eclipse). I am getting it at line :.map(Map::values) – user3903418 Jul 16 '19 at 11:53
  • @user3903418 I guess it's `.map(Bundle::values)`, as I said in the answer _I assume that your findFirst returns some kind of a Map_ – Eugene Jul 16 '19 at 11:58
1

Optional#get() throws NoSuchElementException if no value is present means value is null.

To avoid exception you can use Optional#orElse(T other) which returns the value if present, otherwise returns other value passed in the method.

Optional<Integer> d1  =  Optional.of(10);
Optional<Integer> d2  =  Optional.empty();
Integer res1 = d1.orElse(0);  // return 10 since value is present
Integer res2 = d2.orElse(0);  // return 0 since value is not present

Or you want to throw an exception when if no value is present then use orElseThrow

optionalObj.orElseThrow(ArithmeticException::new)

Then you can do further operation based on value.

Eklavya
  • 17,618
  • 4
  • 28
  • 57