Как выловить java util nosuchelementexception
Constructs a NoSuchElementException , saving a reference to the error message string s for later retrieval by the getMessage method.
Method Summary
Methods inherited from class java.lang.Throwable
Methods inherited from class java.lang.Object
Constructor Detail
NoSuchElementException
public NoSuchElementException()
Constructs a NoSuchElementException with null as its error message string.
NoSuchElementException
Constructs a NoSuchElementException , saving a reference to the error message string s for later retrieval by the getMessage method.
Java™ Platform
Standard Ed. 8
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
How to solve java.util.NoSuchElementException in this code?
Try to use if(sc.hasNextLine()) before actually trying to read the next line. JavaDoc on hasNextLine() : «This method may block while waiting for input.».
Oct 13, 2022 at 6:52
1 Answer 1
At least something like this can be done:
String name = ""; if (sc.hasNextLine())
Or even better you can use optionals not to have empty line.
answered Oct 13, 2022 at 6:54
1 2 2 bronze badges
- java
- java.util.scanner
- nosuchelementexception
-
The Overflow Blog
Related
Hot Network Questions
Subscribe to RSS
Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.10.27.43697
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Как выловить java util nosuchelementexception
Constructs a NoSuchElementException , saving a reference to the error message string s for later retrieval by the getMessage method.
Method Summary
Methods declared in class java.lang.Throwable
Methods declared in class java.lang.Object
Constructor Detail
NoSuchElementException
public NoSuchElementException()
Constructs a NoSuchElementException with null as its error message string.
NoSuchElementException
Constructs a NoSuchElementException , saving a reference to the error message string s for later retrieval by the getMessage method.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Saved searches
Use saved searches to filter your results more quickly
Cancel Create saved search
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When using instrumented Maybe — getting java.util.NoSuchElementException #3265
philippreston opened this issue May 18, 2020 · 0 comments
When using instrumented Maybe — getting java.util.NoSuchElementException #3265
philippreston opened this issue May 18, 2020 · 0 comments
type: bug Something isn’t working
Comments
Contributor
philippreston commented May 18, 2020
Task List
- Steps to reproduce provided
- Stacktrace (if present) provided
- Example that reproduces the problem uploaded to Github
- Full description of the issue provided (see below)
Steps to Reproduce
- Add @NewSpan to a method that returns a Maybe from a method that returns a Publisher
Expected Behaviour
That a Maybe that wraps a Publisher which emits 0 items, returns an empty Maybe.
Actual Behaviour
The actual behaviour is that we get an exception:
Caused by: java.util.NoSuchElementException: The source Publisher is empty at io.reactivex.internal.operators.single.SingleFromPublisher$ToSingleObserver.onComplete(SingleFromPublisher.java:99) at io.micronaut.tracing.instrument.util.TracingPublisher$1.onComplete(TracingPublisher.java:182) at io.reactivex.internal.util.HalfSerializer.onComplete(HalfSerializer.java:90) at io.reactivex.internal.subscribers.StrictSubscriber.onComplete(StrictSubscriber.java:109) at io.reactivex.internal.operators.flowable.FlowableElementAt$ElementAtSubscriber.onComplete(FlowableElementAt.java:105) at io.reactivex.internal.operators.flowable.FlowableDoOnEach$DoOnEachSubscriber.onComplete(FlowableDoOnEach.java:135) at io.reactivex.internal.operators.flowable.FlowableDoOnLifecycle$SubscriptionLambdaSubscriber.onComplete(FlowableDoOnLifecycle.java:94) at io.reactivex.internal.operators.flowable.FlowableDoOnEach$DoOnEachSubscriber.onComplete(FlowableDoOnEach.java:135) at io.reactivex.internal.operators.flowable.FlowableDoOnEach$DoOnEachSubscriber.onComplete(FlowableDoOnEach.java:135) at com.mongodb.reactivestreams.client.internal.ObservableToPublisher$1.onComplete(ObservableToPublisher.java:76) at com.mongodb.async.client.AbstractSubscription.onComplete(AbstractSubscription.java:154) at com.mongodb.async.client.AbstractSubscription.processResultsQueue(AbstractSubscription.java:225) at com.mongodb.async.client.AbstractSubscription.tryProcessResultsQueue(AbstractSubscription.java:172) at com.mongodb.async.client.MongoIterableSubscription$2.onResult(MongoIterableSubscription.java:105) at com.mongodb.async.client.MongoIterableSubscription$2.onResult(MongoIterableSubscription.java:89) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:174) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:118) at com.mongodb.async.client.MongoIterableSubscription.requestMoreData(MongoIterableSubscription.java:89) at com.mongodb.async.client.MongoIterableSubscription$1.onResult(MongoIterableSubscription.java:53) at com.mongodb.async.client.MongoIterableSubscription$1.onResult(MongoIterableSubscription.java:46) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49) at com.mongodb.async.client.OperationExecutorImpl$1$1$1.onResult(OperationExecutorImpl.java:94) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49) at com.mongodb.operation.FindOperation$3.onResult(FindOperation.java:827) at com.mongodb.operation.OperationHelper$ReferenceCountedReleasingWrappedCallback.onResult(OperationHelper.java:412) at com.mongodb.operation.CommandOperationHelper$10.onResult(CommandOperationHelper.java:481) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49) at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor$2.onResult(DefaultServer.java:253) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49) at com.mongodb.internal.connection.CommandProtocolImpl$1.onResult(CommandProtocolImpl.java:85) at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection$1.onResult(DefaultConnectionPool.java:467) at com.mongodb.internal.connection.UsageTrackingInternalConnection$2.onResult(UsageTrackingInternalConnection.java:111) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49) at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:399) at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:376) at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:677) at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:644) at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:514) at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:511) at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:220) at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:203) at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:127) at java.base/sun.nio.ch.Invoker.invokeDirect(Invoker.java:158) at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.implRead(UnixAsynchronousSocketChannelImpl.java:560) at java.base/sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:277) at java.base/sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:298) at com.mongodb.internal.connection.AsynchronousSocketChannelStream$AsynchronousSocketChannelAdapter.read(AsynchronousSocketChannelStream.java:137) at com.mongodb.internal.connection.AsynchronousChannelStream.readAsync(AsynchronousChannelStream.java:105) at com.mongodb.internal.connection.InternalStreamConnection.readAsync(InternalStreamConnection.java:511) at com.mongodb.internal.connection.InternalStreamConnection.access$1000(InternalStreamConnection.java:76) at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:634) at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:619) at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:514) at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:511) at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:220) at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.completed(AsynchronousChannelStream.java:203) at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:127) at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishRead(UnixAsynchronousSocketChannelImpl.java:437) at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:191) at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213) at java.base/sun.nio.ch.KQueuePort$EventHandlerTask.run(KQueuePort.java:312) at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834)
The use case we have is using the reactive Mongo library. When doing a find — and no results are returned, the Publisher completes without emitting an item. Our method looks like:
public MaybeRole> findById(String id) < return Flowable.fromPublisher(collection().find(eq("_id", new ObjectId(id)))) .firstElement(); >
@NewSpan Maybe findById(String id);
The stack trace shows that in the Publisher to Maybe conversion — the Publisher is being wrapped by a Single which does not allow for empty, hence the exception.