Facing problem while using Event API

Hi All,

I am using event api with java platform and capturing all event for a project. But after 10 -20 minutes later it always shows me error

Exception in thread “Thread-7” java.util.NoSuchElementException
at com.asana.iterator.PageIterator.next(PageIterator.java:54)
at com.asana.iterator.EventsPageIterator.next(EventsPageIterator.java:42)
at com.asana.iterator.ItemIterator.next(ItemIterator.java:25)

or sometime it shows

Exception in thread “Thread-11” com.google.gson.JsonIOException: java.net.SocketTimeoutException: Read timed out
at com.google.gson.Gson.assertFullConsumption(Gson.java:788)
at com.google.gson.Gson.fromJson(Gson.java:776)
at com.asana.requests.ItemRequest.executeRaw(ItemRequest.java:39)
at com.asana.requests.ItemRequest.execute(ItemRequest.java:27)

here is my code:

EventsRequest<Event> request = client.events.get(projectId);
for (Iterator<Event> iterator = request.iterator(); iterator.hasNext();) {
    Event e = (Event) iterator.next();
    String action = e.action;
    String type   = e.type;
}

Can anyone tell me the solution?

Hey there @Kuldeep_Poonia,

The second error is simply a timeout error - trying to read the response, but the request took too long. I believe we’re supposed to retry in this case, but it doesn’t appear that we do. I’ll file an enhancement request in Asana.

I’'m not sure about the first error - calling next() will throw NoSuchElementException if there isn’t another element, but it should be checking with every loop. One brute force method might be to simply break out of the loop as if hasNext() were false in this case, but without being able to sit down and reproduce/debug, I think this will be tricky to track down. Is your app multi-threaded? Is it possible that there’s a race condition where you’re calling next() in a separate thread (or otherwise consuming the event from Asana with a different event stream) that might set up a race condition where hasNext() is true, but the event is consumed somewhere else becore next() is called? :thinking:

The NoSuchElementException in Java is thrown when one tries to access an iterable beyond its maximum limit. This means that, this exception is thrown by various accessor methods to indicate that the element being requested does not exist . The next() method in Java returns the next element in the iteration or NoSuchElementException if the iteration has no more elements.

As with most programming languages, the Iterator class includes a hasNext() method that returns a boolean indicating if the iteration has anymore elements. If hasNext() returns true, then the next() method will return the next element in the iteration otherwise raise exceptions if the iteration has no more elements.

if(input.hasNextInt() )
     number1 = input.nextInt(); // if there is another number  
else 
     number1 = 0; // nothing added in the input