使用sbt加载Scala项目时抛出NullPointerException问题求助
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.
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)
- First, check what your current
CLASSPATHis set to:echo $CLASSPATH - If it returns an empty string or looks like invalid garbage, unset it temporarily to test:
unset CLASSPATH - Try running
sbtagain in your project directory. If this works, make the change permanent:- For bash users: Open
~/.bashrcin a text editor and addunset CLASSPATHat the end. - For zsh users: Edit
~/.zshrcinstead with the same line. - Save the file and run
source ~/.bashrc(orsource ~/.zshrc) to apply the change immediately.
- For bash users: Open
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:
- Check your current Java version:
java -version - 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 - For a reliable Java runtime, install OpenJDK 11 and set it as default:
Select the OpenJDK 11 option from the prompt that appears.sudo apt install openjdk-11-jdk sudo update-alternatives --config java
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
projectdirectory (likebuild.sbtorplugins.sbt). - Delete the
targetdirectory and try a clean compile:rm -rf target sbt clean compile
内容的提问来源于stack exchange,提问作者npk

