You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用sbt加载Scala项目时抛出NullPointerException问题求助

sbt throws NullPointerException when loading Scala projects on Ubuntu 18.04

I cloned two separate Scala projects, navigated to their respective directories in the terminal, and ran the sbt command to load them. Both projects encountered identical-looking NullPointerException errors:

Error from first project:

java.lang.NullPointerException at java.base/java.util.regex.Matcher.getTextLength(Matcher.java:1769) at java.base/java.util.regex.Matcher.reset(Matcher.java:416) at java.base/java.util.regex.Matcher.<init>(Matcher.java:253) at java.base/java.util.regex.Pattern.matcher(Pattern.java:1130) at java.base/java.util.regex.Pattern.split(Pattern.java:1249) at java.base/java.util.regex.Pattern.split(Pattern.java:1322) at sbt.IO$.pathSplit(IO.scala:797) at sbt.IO$.parseClasspath(IO.scala:912) at sbt.compiler.CompilerArguments.extClasspath(CompilerArguments.scala:66) ... ...

Error from pdffigures2 project:

[info] Loading project definition from /path/to/project/pdffigures2/project java.lang.NullPointerException at java.base/java.util.regex.Matcher.getTextLength(Matcher.java:1769) at java.base/java.util.regex.Matcher.reset(Matcher.java:416) at java.base/java.util.regex.Matcher.<init>(Matcher.java:253) at java.base/java.util.regex.Pattern.matcher(Pattern.java:1130) at java.base/java.util.regex.Pattern.split(Pattern.java:1249) at java.base/java.util.regex.Pattern.split(Pattern.java:1322) at sbt.IO$.pathSplit(IO.scala:744) at sbt.IO$.parseClasspath(IO.scala:859) at sbt.compiler.CompilerArguments.extClasspath(CompilerArguments.scala:62) at sbt.compiler.MixedAnalyzingCompiler$.withBootclasspath(MixedAnalyzingCompiler.scala:189) ... ...

Retrying the operation didn't resolve the issue. My system is Ubuntu 18.04.


Fixing the sbt NullPointerException

Let's tackle this issue head-on—I've run into this exact problem before, and it almost always ties to classpath misconfiguration or compatibility gaps between sbt and your Java environment. Here's how to fix it step by step:

Root Cause Breakdown

The error stack points to sbt's pathSplit method failing while splitting classpath entries with regex. This usually happens when the CLASSPATH environment variable is empty, null, or contains invalid paths—sbt tries to parse it and hits a null pointer when the regex matcher gets no valid input.

Step 1: Fix Your CLASSPATH Variable (Most Common Fix)

  1. First, check what your current CLASSPATH is set to:
    echo $CLASSPATH
    
  2. If it returns an empty string or looks like invalid garbage, unset it temporarily to test:
    unset CLASSPATH
    
  3. Try running sbt again in your project directory. If this works, make the change permanent:
    • For bash users: Open ~/.bashrc in a text editor and add unset CLASSPATH at the end.
    • For zsh users: Edit ~/.zshrc instead with the same line.
    • Save the file and run source ~/.bashrc (or source ~/.zshrc) to apply the change immediately.

Step 2: Ensure sbt and Java Are Compatible

Ubuntu 18.04's default sbt or Java versions might be outdated and clash. Here's a stable setup:

  1. Check your current Java version:
    java -version
    
  2. If you're using Java 11 or newer, install sbt 1.3.x+ (the latest stable version) with these commands:
    echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list
    curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo apt-key add
    sudo apt update
    sudo apt install sbt
    
  3. For a reliable Java runtime, install OpenJDK 11 and set it as default:
    sudo apt install openjdk-11-jdk
    sudo update-alternatives --config java
    
    Select the OpenJDK 11 option from the prompt that appears.

Step 3: Clear Corrupted sbt Cache

Sometimes cached files get corrupted and cause weird errors. Wipe them out and rebuild:

rm -rf ~/.sbt ~/.ivy2/cache

Re-run sbt in your project—this will force sbt to re-download all dependencies and rebuild the cache from scratch.

Step 4: Verify Project Setup (Just in Case)

If none of the above works, double-check your project setup:

  • Make sure you cloned the projects correctly and didn't accidentally modify files in the project directory (like build.sbt or plugins.sbt).
  • Delete the target directory and try a clean compile:
    rm -rf target
    sbt clean compile
    

内容的提问来源于stack exchange,提问作者npk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:34:52