Check If Command Line Arguments Are Null in Java (#13246)
This commit is contained in:
parent
629582c27b
commit
4b3a693263
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.commandline;
|
||||
|
||||
public class CommandLineWithoutErrorHandling {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println(args[0]);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue