Check If Command Line Arguments Are Null in Java (#13246)

This commit is contained in:
hajarrs 2023-01-23 14:34:39 +01:00 committed by GitHub
parent 629582c27b
commit 4b3a693263
3 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.commandline;
public class CommandLineWithErrorHandling {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println(args[0]);
} else {
System.out.println("No command line arguments were provided.");
}
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.commandline;
public class CommandLineWithoutErrorHandling {
public static void main(String[] args) {
System.out.println(args[0]);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.commandline;
import org.junit.Test;
import static org.junit.Assert.fail;
public class CommandLineWithoutErrorHandlingUnitTest {
@Test(expected = NullPointerException.class)
public void givenNullCommandLineArgument_whenPassedToMainFunction_thenExpectNullPointerException() {
CommandLineWithoutErrorHandling.main(null);
}
}