MovingBlocks/DestinationSol

java.lang.NullPointerException: ... because "environment" is null

jozefcifre opened this issue · 15 comments

What you were trying to do

I was trying to run the game as follows:

nix-shell
./gradlew
./gradlew run

What actually happened

java.lang.NullPointerException: Cannot invoke "org.terasology.gestalt.module.ModuleEnvironment.getSubtypesOf(java.lang.Class, java.util.function.Predicate)" because "environment" is null
...

How to reproduce

No response

Game version

2.1.0 (develop branch)

Log details

java.lang.NullPointerException: Cannot invoke "org.terasology.gestalt.module.ModuleEnvironment.getSubtypesOf(java.lang.Class, java.util.function.Predicate)" because "environment" is null
	at org.terasology.gestalt.entitysystem.component.management.ComponentTypeIndex.changeEnvironment(ComponentTypeIndex.java:84)
	at org.terasology.gestalt.entitysystem.component.management.ComponentTypeIndex.<init>(ComponentTypeIndex.java:67)
	at org.destinationsol.assets.AssetHelper.init(AssetHelper.java:82)
	at org.destinationsol.SolApplication.create(SolApplication.java:164)
	at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.initializeListener(Lwjgl3Window.java:416)
	at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:366)
	at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:193)
	at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:167)
	at org.destinationsol.desktop.SolDesktop.main(SolDesktop.java:138)

Operating System

Linux

Additional Info

I use NixOS.

Here is the shell.nix where I usually run DS:

let
  nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-unstable";
  pkgs = import nixpkgs { config = {}; overlays = []; };
in

pkgs.mkShell {
  packages = with pkgs; [
#    git # already installed
#    temurin-bin-11 # I installed temurin-bin-21 instead
    libglvnd
    libpulseaudio
    xorg.libXcursor
    xorg.libXrandr
    xorg.libXxf86vm
  ];
  LD_LIBRARY_PATH = "${pkgs.stdenv.cc.cc.lib}/lib:${pkgs.libglvnd}/lib:${pkgs.libpulseaudio}/lib:${pkgs.xorg.libXcursor}/lib:${pkgs.xorg.libXrandr}/lib:${pkgs.xorg.libXxf86vm}/lib";
}

I was able to run the game exactly as I tried today. It's a fresh clone, no changes, just the source code as it is in this repository; however, it didn't work this time.

$ ./gradlew run
Configuration on demand is an incubating feature.
Module modules:core has a build file so counting it complete and including it
> Task :build-logic:extractPluginRequests UP-TO-DATE
> Task :build-logic:generatePluginAdapters UP-TO-DATE
> Task :build-logic:compileJava UP-TO-DATE
> Task :build-logic:compileGroovy NO-SOURCE
> Task :build-logic:compileGroovyPlugins UP-TO-DATE
> Task :build-logic:pluginDescriptors UP-TO-DATE
> Task :build-logic:processResources UP-TO-DATE
> Task :build-logic:classes UP-TO-DATE
> Task :build-logic:jar UP-TO-DATE
> Task :engine:compileJava UP-TO-DATE
> Task :desktop:compileJava UP-TO-DATE
> Task :desktop:processResources UP-TO-DATE
> Task :desktop:classes UP-TO-DATE
> Task :engine:processResources UP-TO-DATE
> Task :engine:classes UP-TO-DATE
> Task :engine:jar UP-TO-DATE

> Task :desktop:run
 INFO [main] (ModulePathScanner.java:110) - Discovered module: core-2.1.0
java.lang.UnsupportedOperationException: The Security Manager is deprecated and will be removed in a future release
        at java.base/java.lang.System.setSecurityManager(System.java:425)
        at org.destinationsol.modules.ModuleManager.loadEnvironment(ModuleManager.java:281)
        at org.destinationsol.modules.ModuleManager.init(ModuleManager.java:248)
        at org.destinationsol.SolApplication.create(SolApplication.java:158)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.initializeListener(Lwjgl3Window.java:416)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:366)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:193)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:167)
        at org.destinationsol.desktop.SolDesktop.main(SolDesktop.java:138)
ERROR [main] (SolApplication.java:160) - Cannot initialize modules
ERROR [main] (SolDesktop.java:179) - This exception was not caught:
java.lang.NullPointerException: Cannot invoke "org.terasology.gestalt.module.ModuleEnvironment.getSubtypesOf(java.lang.Class, java.util.function.Predicate)" because "environment" is null
        at org.terasology.gestalt.entitysystem.component.management.ComponentTypeIndex.changeEnvironment(ComponentTypeIndex.java:84)
        at org.terasology.gestalt.entitysystem.component.management.ComponentTypeIndex.<init>(ComponentTypeIndex.java:67)
        at org.destinationsol.assets.AssetHelper.init(AssetHelper.java:82)
        at org.destinationsol.SolApplication.create(SolApplication.java:164)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.initializeListener(Lwjgl3Window.java:416)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:366)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:193)
        at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:167)
        at org.destinationsol.desktop.SolDesktop.main(SolDesktop.java:138)

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 17s
13 actionable tasks: 1 executed, 12 up-to-date
java.lang.UnsupportedOperationException: The Security Manager is deprecated and will be removed in a future release
       at java.base/java.lang.System.setSecurityManager(System.java:425)
       at org.destinationsol.modules.ModuleManager.loadEnvironment(ModuleManager.java:281)
       at org.destinationsol.modules.ModuleManager.init(ModuleManager.java:248)
       at org.destinationsol.SolApplication.create(SolApplication.java:158)
       at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.initializeListener(Lwjgl3Window.java:416)
       at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:366)
       at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:193)
       at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:167)
       at org.destinationsol.desktop.SolDesktop.main(SolDesktop.java:138)

This error is likely the cause. It's a known issue and usually happens with Java versions above 17. The exception is thrown because SecurityManager is deprecated for removal. Since the game typically runs with Java 11 this is not usually encountered.

A workaround is something along the lines of the following in ModuleManager, which I will try and add soon:

if (Runtime.version().feature() < 18 || "allow".equals(System.getProperty("java.security.manager"))) {
    Policy.setPolicy(new ModuleSecurityPolicy());
    System.setSecurityManager(new ModuleSecurityManager());
} else {
    logger.warn("SecurityManager is disabled starting with Java 18 - module sandbox functionality is limited!");
    logger.warn("To enable SecurityManager, use the \"-Djava.security.manager=allow\" JVM option.");
}

Although with that nix-shell environment you should be using Java 11 already. What output do you see from running the java -version command in that shell?

Although with that nix-shell environment you should be using Java 11 already. What output do you see from running the java -version command in that shell?

Ah, I forgot to mention that I installed OpenJDK 19 instead, that's what I were running at that time.

if (Runtime.version().feature() < 18 || "allow".equals(System.getProperty("java.security.manager"))) {
    Policy.setPolicy(new ModuleSecurityPolicy());
    System.setSecurityManager(new ModuleSecurityManager());
} else {
    logger.warn("SecurityManager is disabled starting with Java 18 - module sandbox functionality is limited!");
    logger.warn("To enable SecurityManager, use the \"-Djava.security.manager=allow\" JVM option.");
}
DestinationSol/engine/src/main/java/org/destinationsol/modules/ModuleManager.java:284: error: cannot find symbol
        if (Runtime.version().feature() < 18 || "allow".equals(System.getProperty("java.security.manager"))) {
                   ^
  symbol:   method version()
  location: class Runtime
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
6 warnings

Where do you place it?

Did you find a solution for this? I had meant to get back to this but temporarily forgot for a time.

Did you find a solution for this? I had meant to get back to this but temporarily forgot for a time.

I tried pasting snippet code at ModuleManager.java:284. It failed to build.

I know that DS is supposed to be run in jdk11, but I wanted to test whether it runs faster in newer versions.

The changes in BenjaminAmos@fdeced3 should work. We are unable to use the Runtime class yet because the code is still compiled against Java 8 for Android runtime compatibility. This means that we are limited to the APIs available in Java 8, even if the game is unable to actually run on Java 8 anymore.

Thank you. fdeced3 didn't work on Java 21, but worked on Java 17.

Is it showing the same error as before on Java 21? I tested a distribution build under Java 21 and it appeared to work.

Running ./gradlew run might be more problematic though since the version of Gradle we currently use doesn't support Java 21 (it supports Java 19 at most). Upgrading Gradle is not currently planned since I need to co-ordinate it with also upgrading the Android gradle plugin at the same time, which would necessitate a move to Java 17.

Is it showing the same error as before on Java 21? I tested a distribution build under Java 21 and it appeared to work.

No, only happens on Java 21.

[info] Java Home: /nix/store/4v41d9wyc4n4idvcx5fa7vpbnqp14ccz-temurin-bin-21.0.1
[info] JVM Args: --add-opens=java.base/java.util=ALL-UNNAMED,--add-opens=java.base/java.lang=ALL-UNNAMED,--add-opens=java.base/java.lang.invoke=ALL-UNNAMED,--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens=java.base/java.nio.charset=ALL-UNNAMED,--add-opens=java.base/java.net=ALL-UNNAMED,--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xms128m,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=US,-Duser.language=en,-Duser.variant
[info] Gradle User Home: /home/user/.gradle
[info] Gradle Version: 7.6
[info] Module modules:core has a build file so counting it complete and including it
Module modules:pirates has a build file so counting it complete and including it
> Task :build-logic:extractPluginRequests UP-TO-DATE
> Task :build-logic:generatePluginAdapters UP-TO-DATE
> Task :build-logic:compileJava UP-TO-DATE
> Task :build-logic:compileGroovy NO-SOURCE
> Task :build-logic:compileGroovyPlugins FAILED
[error] FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':build-logic:compileGroovyPlugins'.
> BUG! exception in phase 'semantic analysis' in source unit 'precompiled_DestinationSolIde' Unsupported class file major version 65

If you change the gradle version used:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-all.zip

to

https\://services.gradle.org/distributions/gradle-8.4-all.zip

Does it help at all?

Hi. I have this problem now, it's making it difficult to keep developing.
redhat-developer/vscode-java#3449 (comment)
Please help.

If you change the gradle version used:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-all.zip

to

https\://services.gradle.org/distributions/gradle-8.4-all.zip

Does it help at all?

Yes, it seems so. I don't see the "Error: Gradle Build Failed" message again.

Hi. I have this problem now, it's making it difficult to keep developing. redhat-developer/vscode-java#3449 (comment) Please help.

I tend to use IntelliJ Idea personally, so the most I know is that your issue looks a bit like microsoft/vscode-gradle#1431 and it might be worth disabling the Gradle for Java extension. You might have to get used to running ./gradlew run whenever you want to test the game otherwise.

I tend to use IntelliJ Idea personally, so the most I know is that your issue looks a bit like microsoft/vscode-gradle#1431 and it might be worth disabling the Gradle for Java extension. You might have to get used to running ./gradlew run whenever you want to test the game otherwise.

It worked! Thank you!