title: Android unit test throw null pointer exception
body: <p>I'm trying to test a validity function. My function is like this:</p>
  <pre><code>
  class InvalidCredentialException(message: String) : Exception(message)
  
  
  @Throws
  fun credentialValidityChecker(email: String, password: String, nameAndFamily: String? = null) {
      when {
          email.isBlank() -&gt; {
              throw InvalidCredentialException(&quot;Email address can't left blank.&quot;)
          }
          !Patterns.EMAIL_ADDRESS.matcher(email)
              .matches() -&gt; {
              throw InvalidCredentialException(&quot;Email address format is not correct.&quot;)
          }
          password.isBlank() -&gt; {
              throw InvalidCredentialException(&quot;Password can't left blank.&quot;)
          }
          password.length &lt; 5 -&gt; {
              throw InvalidCredentialException(&quot;Password should have at least 5 characters.&quot;)
          }
          nameAndFamily != null -&gt; {
              if (nameAndFamily.isBlank())
                  throw InvalidCredentialException(&quot;Name and family can't left blank.&quot;)
          }
      }
  }
  
  
  </code></pre>
  <p>I use this function to throw in case of any problem with the user credential. Otherwise, nothing happens, and the code continues. The exception is handled in other application layers.
  and here are my test cases:</p>
  <pre><code>
  class CredentialValidityTest {
  
      @Test
      fun emptyEmail_raiseEmptyEmailException() {
          try {
              credentialValidityChecker(email = &quot;&quot;, password = &quot;12345&quot;)
              fail(&quot;Empty email should raise exception.&quot;)
          } catch (e: InvalidCredentialException) {
              assertThat(e.message).isEqualTo(&quot;Email address can't left blank.&quot;)
          }
      }
  
      @Test
      fun wrongFormatEmail_raiseWrongEmailException() {
          val wrongFormatEmailList = listOf(
              &quot;test&quot;, &quot;test@&quot;, &quot;test@application&quot;,
              &quot;test@application.&quot;, &quot;test@.&quot;, &quot;test.application@com&quot;
          )
          for (email in wrongFormatEmailList)
              try {
                  credentialValidityChecker(email = email, password = &quot;12345&quot;)
                  fail(&quot;Wrong format email should raise exception.&quot;)
              } catch (e: InvalidCredentialException) {
                  assertThat(e.message).isEqualTo(&quot;Email address format is not correct.&quot;)
              }
      }
  
      @Test
      fun emptyPassword_raiseEmptyPasswordException() {
          try {
              credentialValidityChecker(email = &quot;test@application.com&quot;, password = &quot;&quot;)
              fail(&quot;Empty password should raise exception.&quot;)
          } catch (e: InvalidCredentialException) {
              assertThat(e.message).isEqualTo(&quot;Password can't left blank.&quot;)
          }
      }
  
      @Test
      fun weakPassword_raiseWeakPasswordException() {
          try {
              credentialValidityChecker(email = &quot;test@application.com&quot;, password = &quot;1234&quot;)
              fail(&quot;weak password should raise exception.&quot;)
          } catch (e: InvalidCredentialException) {
              assertThat(e.message).isEqualTo(&quot;Password should have at least 5 characters.&quot;)
          }
      }
  
      @Test
      fun emptyNameAndFamily_raiseEmptyNameAndFamilyException() {
          try {
              credentialValidityChecker(
                  email = &quot;test@application.com&quot;,
                  password = &quot;12345&quot;,
                  nameAndFamily = &quot;&quot;
              )
              fail(&quot;Empty name and family should raise exception.&quot;)
          } catch (e: InvalidCredentialException) {
              assertThat(e.message).isEqualTo(&quot;Name and family can't left blank.&quot;)
          }
      }
  
  }
  </code></pre>
  <p>The problem is:</p>
  <p>Only the first test case pass, which checks email not be blank. The other test cases fail with the <code>java.lang.NullPointerException</code> error.
  What is the problem?</p>