Java 将Iterable与列表进行比较会引发NullPointerException
我想将Java 将Iterable与列表进行比较会引发NullPointerException,java,spring-boot,unit-testing,Java,Spring Boot,Unit Testing,我想将Iterable与列表进行比较 我的方法是: public Iterable<Class> findAll(final List<Long> idList) { final List<Class> result = new LinkedList<Class>(); final List<List<Long>> partitions = ListUtils.partition(idList
Iterable
与列表进行比较
我的方法是:
public Iterable<Class> findAll(final List<Long> idList) {
final List<Class> result = new LinkedList<Class>();
final List<List<Long>> partitions = ListUtils.partition(idList, 999);
partitions.forEach(partition -> Iterables.addAll(result, this.repository.findAll(partition)));
return result;
}
当我将它们与asserEquals(预期的,实际的)
进行比较时,我得到一个空点异常。
我知道它们有不同的类型,但它们有相同的值。为什么我不能比较它们?
我使用偶数.equals()只是为了检查,但我仍然得到一个NPE
然后我试图得到第一个值
assertEquals(expected.get(0), result.iterator().next());
我还是有一个NPE
我检查了Iterable文档,并编写了iterator()抛出和NPE的代码
Iterator<T> iterator();
/**
* Performs the given action for each element of the {@code Iterable}
* until all elements have been processed or the action throws an
* exception. Unless otherwise specified by the implementing class,
* actions are performed in the order of iteration (if an iteration order
* is specified). Exceptions thrown by the action are relayed to the
* caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
它们(其中一个)是空的吗?为什么要使用Iterable
而不是List
,这是这里常见的类型?@UlanMukhamedyanov不,它们不是空的。我猜您得到的是actual
@dernor00返回的null。问题在于类中的equals实现错误。如果需要更多帮助,请再次分享。可能您以错误的方式比较了两个空字段
Iterator<T> iterator();
/**
* Performs the given action for each element of the {@code Iterable}
* until all elements have been processed or the action throws an
* exception. Unless otherwise specified by the implementing class,
* actions are performed in the order of iteration (if an iteration order
* is specified). Exceptions thrown by the action are relayed to the
* caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
java.lang.NullPointerException
at ch.sbb.project.model.Class.equals(Class.java:224)
at java.util.AbstractList.equals(AbstractList.java:523)
at org.junit.Assert.isEquals(Assert.java:131)
at org.junit.Assert.equalsRegardingNull(Assert.java:127)
at org.junit.Assert.assertEquals(Assert.java:111)
at org.junit.Assert.assertEquals(Assert.java:144)
at ch.sbb.project.service.ServiceTest.findAll(ServiceTest.java:54)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)