Java iterator null object

Русские Блоги

Attempt to invoke interface method ‘java.util.Iterator java.util.List.iterator()’ on a null object

Process: com.xxx.yyy Flags: 0xc8be45 Package: com.xxx.yyy v1000 (xxxxxxx-yyyyyyyy) Build: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz:eng/test-keys java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference at android.os.Parcel.readException(Parcel.java:1552) at android.os.Parcel.readException(Parcel.java:1499) at com.android.internal.view.IInputMethodManager$Stub$Proxy.showInputMethodPickerFromClient(IInputMethodManager.java:753) at android.view.inputmethod.InputMethodManager.showInputMethodPickerLocked(InputMethodManager.java:1898) at android.view.inputmethod.InputMethodManager.dispatchInputEvent(InputMethodManager.java:1755) at android.view.ViewRootImpl$ImeInputStage.onProcess(ViewRootImpl.java:3853) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:122) at android.app.ActivityThread.main(ActivityThread.java:5267) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Это очень рад видеть java.lang.nullpointerexception?

APOTTELT, чтобы вызвать интерфейс метод ‘java.util.iterator java.util.list.iterator ()’ на ссылке на нулевой объект

Но у вас нет хорошего кода Com.xxx.yyy?

Давайте посмотрим на последнюю строку стека:

1543 public final void readException(int code, String msg) < 1544 switch (code) < 1545 case EX_SECURITY: 1546 throw new SecurityException(msg); 1547 case EX_BAD_PARCELABLE: 1548 throw new BadParcelableException(msg); 1549 case EX_ILLEGAL_ARGUMENT: 1550 throw new IllegalArgumentException(msg); 1551 case EX_NULL_POINTER: 1552 throw new NullPointerException(msg); 1553 case EX_ILLEGAL_STATE: 1554 throw new IllegalStateException(msg); 1555 case EX_NETWORK_MAIN_THREAD: 1556 throw new NetworkOnMainThreadException(); 1557 case EX_UNSUPPORTED_OPERATION: 1558 throw new UnsupportedOperationException(msg); 1559 >1560 throw new RuntimeException("Unknown exception code: " + code 1561 + " msg " + msg); 1562 >

Пустой указатель ненормальный, а исходный код iinputmethodmanager.java не потому, что он автоматически сгенерирован iinputmethodmanager.aldl. В это время нет кодового стека приложения com.xxx.yy, но сочетает в себе ранее видел IXXX. Java Кодекс должен заключаться в том, что редкселирование брошено после завершения Troxy, и проблема объясняется в IncapeMethodManagerservice.

Читайте также:  Javascript process local file

Вновь воспроизведите ошибку, наблюдайте все выходы журнала системы, мы находим:

android.util.Log$TerribleFailure: Input Method Manager Crash at android.util.Log.wtf(Log.java:291) at android.util.Slog.wtf(Slog.java:116) at com.android.server.InputMethodManagerService.onTransact(InputMethodManagerService.java:879) at android.os.Binder.execTransact(Binder.java:446) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference at com.android.server.InputMethodManagerService.isServiceRunning(InputMethodManagerService.java:993) at com.android.server.InputMethodManagerService.showInputMethodPickerFromClient(InputMethodManagerService.java:2154) at com.android.internal.view.IInputMethodManager$Stub.onTransact(IInputMethodManager.java:237) at com.android.server.InputMethodManagerService.onTransact(InputMethodManagerService.java:874) . 1 more

Так что исправьте итератор пустой указатель вводаMethodmanagerservice.java, чтобы решить проблему.

Источник

Return empty iterator

Effective java Item 43 states return empty arrays or collections, not nulls. In practice you should return the same immutable empty collection every time you return a collection. There are a few ways to handle the exception to the rule when you encounter methods that should return a collection but instead return null. Check out return empty list, return empty map, return empty set, return empty enumeration,return empty sorted set, return empty sorted map and return empty list iterator when having to deal with other collection types.

Straight up Java

@Test public void return_empty_list_iterator_java ()  IteratorString> strings = Collections.emptyIterator(); assertFalse(strings.hasNext()); >

Google Guava

@Test public void return_empty_list_iterator_guava ()  IteratorString> strings = Iterators.emptyIterator(); assertFalse(strings.hasNext()); >

Apache Commons

@Test public void return_empty_iterator_java ()  @SuppressWarnings("unchecked") IteratorString> strings = IteratorUtils.emptyIterator(); assertFalse(strings.hasNext()); >

Exception to the rule

The goal is to handle the null to empty collection early in the chain. If you are coding the method that returns an iterator, there is no reason it should return null. When dealing with legacy code you have to deal with a null so here are a few options when that occurs:

Just make the null and empty check

private void return_empty_iterator_java_exception ()  DomainObject domain = null; // dao populate domain IteratorString> strings; if (domain != null && domain.getStrings() != null && domain.getStrings().hasNext())  strings = domain.getStrings(); > else  strings = Collections.emptyIterator(); > //. >

Use Guava Objects.firstNonNull

private void return_empty_iterator_guava_exception ()  DomainObject domain = null; // dao populate domain IteratorString> strings = Objects.firstNonNull( domain != null ? domain.getStrings() : null, Iterators.String>emptyIterator()); //. >

Apache commons ColletionUtils

private void return_empty_iterator_apache_commons_exception ()  DomainObject domain = null; // dao populate domain IteratorString> strings; if (domain != null && !CollectionUtils.sizeIsEmpty(domain.getStrings()))  strings = domain.getStrings(); > else  strings = EmptyIterator.INSTANCE; > //. >

Return empty iterator posted by Justin Musgrove on 24 January 2014

Tagged: java and java-collections

Источник

Java iterator null object

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Method Detail

hasNext

Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

next

remove

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next() . The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

forEachRemaining

Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.

 while (hasNext()) action.accept(next()); 

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.

Источник

Interface Iterator

This interface is a member of the Java Collections Framework.

Method Summary

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Method Details

hasNext

Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

next

remove

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next() . The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy. The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

forEachRemaining

Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller. The behavior of an iterator is unspecified if the action modifies the collection in any way (even by calling the remove method or other mutator methods of Iterator subtypes), unless an overriding class has specified a concurrent modification policy. Subsequent behavior of an iterator is unspecified if the action throws an exception.

 while (hasNext()) action.accept(next()); 

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. Other versions.
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.

Источник

Interface Iterator

This interface is a member of the Java Collections Framework.

Method Summary

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Method Details

hasNext

Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

next

remove

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next() . The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy. The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

forEachRemaining

Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller. The behavior of an iterator is unspecified if the action modifies the collection in any way (even by calling the remove method or other mutator methods of Iterator subtypes), unless an overriding class has specified a concurrent modification policy. Subsequent behavior of an iterator is unspecified if the action throws an exception.

 while (hasNext()) action.accept(next()); 

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. Other versions.
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.

Источник

Оцените статью