dominion-dev/dominion-ecs-java

NullPointerException occurs

cnwillz opened this issue · 3 comments

I'm tring to test a situation when i add entities and remove all after that, it occurs exception,
I fount when outer loop time less than 3 will not occurs, is this some kind of thread problem?

        // creates your world
        Dominion hello = Dominion.create();

        // creates a scheduler
        Scheduler scheduler = hello.createScheduler();

        // a system removes all entities with component Position
        Runnable s = () -> {
            hello.findEntitiesWith(Position.class).stream().forEach(e -> {
                hello.deleteEntity(e.entity());
            });
        };
        scheduler.schedule(s);

        for (int i = 0; i < 10; i++) {
            // add entities
            for (int j = 0; j < 1000; j++) {
                var e = hello.createEntity(
                        "my-entity",
                        new Position(0, 0),
                        new Velocity(1, 1)
                );
            }
            // tick to remove all entities
            scheduler.tick();
        }

Exception stacktrace:

dominion.SystemScheduler invoke 
java.lang.NullPointerException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at java.base/java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:564)
	at java.base/java.util.concurrent.ForkJoinTask.reportException(ForkJoinTask.java:591)
	at java.base/java.util.concurrent.ForkJoinTask.joinForPoolInvoke(ForkJoinTask.java:1042)
	at java.base/java.util.concurrent.ForkJoinPool.invoke(ForkJoinPool.java:2639)
	at dev.dominion.ecs.engine.SystemScheduler.forkAndJoin(SystemScheduler.java:113)
	at dev.dominion.ecs.engine.SystemScheduler$Single.call(SystemScheduler.java:262)
	at dev.dominion.ecs.engine.SystemScheduler$Single.call(SystemScheduler.java:239)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "dev.dominion.ecs.engine.IntEntity$Data.composition()" because the return value of "dev.dominion.ecs.engine.IntEntity.getData()" is null
	at dev.dominion.ecs.engine.Composition$IteratorWith1.next(Composition.java:352)
	at dev.dominion.ecs.engine.Composition$IteratorWith1.next(Composition.java:340)
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1845)
	at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
	at com.example.demo1.Demo1Application.lambda$main$1(Demo1Application.java:24)
	at dev.dominion.ecs.engine.SystemScheduler$2.compute(SystemScheduler.java:116)
	at java.base/java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:194)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
 

Thanks for posting the case, I think that a check in the code is still missing here, I will check very soon!

Fixed in 0.7.0-SNAPSHOT, please update your dependency version.
Let me know, thanks.

It works now! Thanks!