Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: NPE when directly launching an application #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/com/pi4j/crowpi/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public Launcher(List<Application> applications) {
// Initialize PicoCLI instance
this.cmdLine = new CommandLine(this);

// Initialize Pi4J context
this.pi4j = CrowPiPlatform.buildNewContext();

// Register application runners as subcommands
this.applications = applications;
this.registerApplicationRunners();
Expand All @@ -113,12 +116,17 @@ public void run() {
// Interactively ask the user for a desired target and run it
// This loop will either run only once or forever, depending on the state of `demoMode`
do {
// Initialize Pi4J context
pi4j = CrowPiPlatform.buildNewContext();
// Re-initialize Pi4J context if needed
if (pi4j == null) {
pi4j = CrowPiPlatform.buildNewContext();
}

// Run the application
getTargetInteractively(targets).run();
// Clean up

// Cleanup Pi4J context
pi4j.shutdown();
pi4j = null;
} while (demoMode);
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/pi4j/crowpi/LauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ public void shouldNotStartTwice() throws ExecutionException, InterruptedExceptio
private static final class AppA implements Application {
@Override
public void execute(Context pi4j) {
assertNotNull(pi4j);
LauncherTest.EXECUTED_APP_A = true;
}
}

private static final class AppB implements Application {
@Override
public void execute(Context pi4j) {
assertNotNull(pi4j);
LauncherTest.EXECUTED_APP_B = true;
}
}
Expand Down