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;
}
NullPointerException.indexOf()doesn't throw aNullPointerException.