-1

I wrote this simple function:

private String getOperatorForCardinality(String op)
{
    String operator ="";
    if(op!=null)
    {
        if(op.equals(">="))
        {
            operator = ">=";
        }
        else if (op.equals("<="))
        {
            operator = "<=";
        }
    }
    else
    {
        operator = "empty";
    }

    return operator;
}

which returns a string.

In the main program I call this function, when the argument is null the compiler displays the error of NullPointerException. The reason is pretty clear, but I do not know how to deal with the null value when is passed by argument.

user840718
  • 1,563
  • 6
  • 29
  • 54
  • You may want to use `null`-safe string comparison: `"string_literal".equals(string_variable);` – PM 77-1 Jan 03 '17 at 19:21
  • Another option is to not allow `null` to be passed as a parameter, and just let it throw an exception because that's a mistake in the calling code. – 4castle Jan 03 '17 at 19:25
  • sometimes throwing an exception **is** the correct behavior. it depends on your design and expectations. – Patrick Parker Jan 03 '17 at 19:26
  • 1
    Check the error message again, it should mention the line of code where the nullpointerexception shows up. It can't be in the code you showed. The function is correct. – aniri Jan 03 '17 at 19:33

4 Answers4

5

It is impossible for the code you posted to throw a NPE. The error is somewhere else, or you are not running the code you think you are (ie haven't recompiled etc).

That said, your method can be simplified to:

private static List<String> OPS = Arrays.asList("<=", ">=");  // can add more valid ops

private static String getOperatorForCardinality(String op) {
    if (op == null)
        return "empty";
    return OPS.contains(op) ? op : "";
}

Or if you don't mind nested ternaries:

private static String getOperatorForCardinality(String op) {
    return OPS.contains(op) ? op : op == null ? "empty" : "";
}

Less code is usually clearer code, and leaves less places for bugs to lurk.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

It is called defensive programming and you should do something like:

private String getOperatorForCardinality(String op) {
    if(null == op) {
        //return null;
        //throw new NullPointerException("...");
    }
    ....
}

You should think about how your method should react, need to return null if parameter is null or throw an exception? Generally you cant be sure a parameter will never be null so you have always to check and take action.

ddarellis
  • 3,912
  • 3
  • 25
  • 53
1

This can't throw a null pointer exception:

private String getOperatorForCardinality(String op)
{
    String operator = "";
    if(">=".equals(op))
    {
        operator = ">=";
    }
    else if ("<=".equals(op))
    {
        operator = "<=";
    } else {
        operator = "empty";
    }

    return operator;
}
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
0

try opposite

if (op==null)
operator = "empty";
Khalil M
  • 1,788
  • 2
  • 22
  • 36