diff --git a/core-java-modules/core-java-exceptions-3/pom.xml b/core-java-modules/core-java-exceptions-3/pom.xml index 32c522dab5..b909572afe 100644 --- a/core-java-modules/core-java-exceptions-3/pom.xml +++ b/core-java-modules/core-java-exceptions-3/pom.xml @@ -30,5 +30,4 @@ 3.10.0 - - \ No newline at end of file + diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/nullmethodparameter/NullParameterExample.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/nullmethodparameter/NullParameterExample.java new file mode 100644 index 0000000000..c2c76bdc46 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/nullmethodparameter/NullParameterExample.java @@ -0,0 +1,17 @@ +package com.baeldung.nullmethodparameter; + +public class NullParameterExample { + public void processSomethingNotNull(Object myParameter) { + if (myParameter == null) { + throw new IllegalArgumentException("Parameter 'myParameter' cannot be null"); + } + //Do something with the parameter + } + + public void processSomethingElseNotNull(Object myParameter) { + if (myParameter == null) { + throw new NullPointerException("Parameter 'myParameter' cannot be null"); + } + //Do something with the parameter + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/nullmethodparameter/NullParameterExampleUnitTest.java b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/nullmethodparameter/NullParameterExampleUnitTest.java new file mode 100644 index 0000000000..aa4b332e52 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/nullmethodparameter/NullParameterExampleUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.nullmethodparameter; + +import org.junit.Test; + +public class NullParameterExampleUnitTest { + @Test(expected = IllegalArgumentException.class) + public void givenNullParameter_whenProcessSomethingNotNull_thenIllegalArgumentException() { + NullParameterExample example = new NullParameterExample(); + example.processSomethingNotNull(null); + } + + @Test(expected = NullPointerException.class) + public void givenNullParameter_whenProcessSomethingElseNotNull_thenNullPointerException() { + NullParameterExample example = new NullParameterExample(); + example.processSomethingElseNotNull(null); + } +}