0

I am practicing JUnit testing and I am not sure what to do when NullPointerExceptions occurs.

I am currently creating my own indexOf() method and I am comparing it with the original indexOf(). I am not sure how to create a similar output from my custom indexOf() to have the same output for when the original indexOf() has a NullPointerException.

I thought about simply giving the output of the error that NullPointerException gives me but i am not sure how to do that as well

Here is my code:

public static int myIndexOf(char[] str, int ch, int index) {
    if (str == null) {
        // output NullPointerException
    }
    if (str.length <= index || index < 0) {
        return -1;
    }
    for (int i = index; i < str.length; i++) {
        if (index == str[i]) {
            return i;
        }
    }
    // if not found
    return -1;
}
8
  • Correct your code so that it doesn't throw a NullPointerException. Commented Mar 24, 2014 at 3:48
  • indexOf() doesn't throw a NullPointerException. Commented Mar 24, 2014 at 3:48
  • @Christian hmm maybe I should have reworded the question a bit better. When ex: str.indexOf(3) and str is null Commented Mar 24, 2014 at 3:50
  • So make sure you don't call any methods on null references. Commented Mar 24, 2014 at 3:53
  • 1
    @Liondancer: Java will always throw an exception in that scenario. You can't even modify this. Its "hardcoded" in every Java virtual machine. Commented Mar 24, 2014 at 3:53

3 Answers 3

2

indexOf() never throws a NullPointerException, except for var.indexOf with var == null. And that type of exception is handled automatically by Java.

Your code is however confusing. In most cases indexOf is an instance-method. In case of a static method it is of course possible to circumvent the problem. For instance:

public static void Foo (Bar b) {
    if(b != null) {
        //do something
    }
}

This is called a total method: in case b is null, nothing happens.

However in most cases you write instance methods. When such method is called and the instance is null, the Java Virtual Machine will throw a NullPointerException itself.

Furthermore in the static method you provide, you call str.length, since str can be null, the Java virtual machine will check this and throw a NullPointerException out of the method itself as well.


You can however test if an exception is thrown in JUnit using the following pattern:

@Test(expected = ExpectedException.class)
public void test () throws ExpectedException {
    Foo f = new Foo();
    f.methodThatThrowsException();
}

JUnit will consider this test to have failed if it does not throw an exception of type ExpectedException.

Sign up to request clarification or add additional context in comments.

5 Comments

This is what I meant sorry for the confusion
@Liondancer: When a null-instance calls a method, the Java runtime environment will throw a NullPointerException itself. You don't have to do anything for this. That makes NullPointerExceptions one of the most annoying things.
I appreciate this answer and I seem ot understand what youre saying. I have one last question. I understand that str.length will throw a NullPointerException when str is null. But I was wondering if you knew why I get a false when var.indexOf throws a NullPointerExcpetion when var is null (original) and when str.length is called (cusomt)?
var.indexOf will throw a NullPointerException as well. Can you provide some code (in your question) to illustrate the problem?
If you dont mind. Could you take a look at this? I thought because both methods output a NullPointerException the test case will return true. But I am assuming it will not due to the error output. pastebin.com/tFiKZWhn
1

If you are using Java 8 you can use a fancy little method to handle this.

public static int myIndexOf(char[] str, int ch, int index) {
    Objects.requireNonNull(str);
    if (str.length <= index || index < 0) {

This method will throw a NullPointerException if the given argument is null. You can also use it like so:

Objects.requireNonNull(str, "The given string cannot be null");

To provide your own error message.

Comments

1
throw new NullPointerException();

will do it. More "organically":

Object c = null; c.toString();

will do it.

But note that indexOf does not throw NPEs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.