Skip to content

Commit 4b3a693

Browse files
authored
Check If Command Line Arguments Are Null in Java (#13246)
1 parent 629582c commit 4b3a693

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.commandline;
2+
3+
public class CommandLineWithErrorHandling {
4+
5+
public static void main(String[] args) {
6+
if (args.length > 0) {
7+
System.out.println(args[0]);
8+
} else {
9+
System.out.println("No command line arguments were provided.");
10+
}
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.commandline;
2+
3+
public class CommandLineWithoutErrorHandling {
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println(args[0]);
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.commandline;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.fail;
5+
6+
public class CommandLineWithoutErrorHandlingUnitTest {
7+
8+
@Test(expected = NullPointerException.class)
9+
public void givenNullCommandLineArgument_whenPassedToMainFunction_thenExpectNullPointerException() {
10+
CommandLineWithoutErrorHandling.main(null);
11+
}
12+
}

0 commit comments

Comments
 (0)