空参数 Java 空指针

huangapple go评论68阅读模式
英文:

null argument java null pointer

问题

这是我的代码,我不知道为什么"one == null"和"two == null"总是为False,并且我不能向我的方法传递任何Null参数!请帮助我。

public class RepeatInString {
    public int StringInString(String one, String two) {
        if (one.equals("") || two.equals("") || one == null || two == null){
            return 0;
        }
        else{
            char[] oneChar = one.toCharArray();
            char[] twoChar = two.toCharArray();
            int repeatCounter = 0;
            int matchChars = 0;
            for (int i=0 ; i < (one.length()-two.length()+1) ;i++ ){
                matchChars = 0;
                for (int j=0; j<two.length() ;j++) {
                    if (oneChar[i + j] == twoChar[j])
                        matchChars += 1;
                    else
                        break;
                }
                if (matchChars == two.length())
                    repeatCounter += 1;
            }
            return repeatCounter;
        }
    }
}
英文:

this is my code and I don't know why "one == null" and "two == null" are always False and I can't send any Null argument to my method!
please help me

public class RepeatInString {
	public int StringInString(String one, String two) {
		if (one.equals(&quot;&quot;) || two.equals(&quot;&quot;) || one == null || two == null){
			return 0;
		}
		else{
			char[] oneChar = one.toCharArray();
			char[] twoChar = two.toCharArray();
			int repeatCounter = 0;
			int matchChars = 0;
			for (int i=0 ; i &lt; (one.length()-two.length()+1) ;i++ ){
				matchChars = 0;
				for (int j=0; j&lt;two.length() ;j++) {
					if (oneChar[i + j] == twoChar[j])
						matchChars += 1;
					else
						break;
				}
				if (matchChars == two.length())
					repeatCounter += 1;
			}
			return repeatCounter;
		}
	}

答案1

得分: 0

如果传递 null 参数,您将获得 NullPointerException,因为您正在执行 one.equals("")。任何 null 都会引发 NullPointerException。您可以使用以下方式改进:

if (one == null || two == null || one.equals("") || two.equals("")) {

如果第一个条件为 true,则将忽略其余条件。

英文:

If you pass null argument, you will get NullPointerException because you are doing one.equals(""). Anything null. will throw NullPointerException. You are better with:

 if (one == null || two == null || one.equals(&quot;&quot;) || two.equals(&quot;&quot;)){

if first condition is true, rest of the conditions will be ignored.

答案2

得分: 0

'dot'是'dereference'运算符。它的意思是:取点号左边的东西,跟随引用,在找到的对象上执行此操作。

因此,如果你写 x.foo()x 为null,你会得到一个 NullPointerException

表达式 a || b || c 从左到右解析。首先计算 a。如果它为真,评估结束,结果已经明确。如果为假,那么解析 b,依此类推。

所以,假设 one 为null。

首先,会评估 one.equals("")(注意,你应该使用 one.isEmpty(),更符合惯用法)。one 为null,因此,此评估导致抛出 NullPointerException,然后执行在那里终止。

你可能想要的是 if (one == null || two == null || one.equals("") || two.equals("")) {

除此之外,你也不想要那个

null 不是代表 '空'。null 最好理解为:未知或无值。

因此,如果我问你:'这个未知值在另一个未知值中吗',唯一可能的答案是:“我不知道”。

如果你用 truefalse 回答该问题,你在撒谎,或者至少是在猜测。在这种情况下不要那样做。在此场景中,如果提供了null,抛出 nullpointerexception 是完全合理的。

有时你会从外部系统读取输入,并且明确选择将null输入视为空字符串或其他标志值是完全可以接受的。如果是这种情况,在系统边界上明确地进行,就在那里处理null输入。

英文:

The 'dot' is the 'dereference' operator. It means: Take the thingie on the left of the dot, follow the reference, and on the object you find there, do this operation.

Hence, if you write x.foo() and x is null, you get a NullPointerException.

The expression a || b || c is resolved left-to-right. First a is calculated. If it's true, evaluation ends, the result is already clear. If it is false, then b is resolved, and so on and so forth.

So, let's say one is null.

First, one.equals(&quot;&quot;) is evaluated (note, you should use one.isEmpty(), that's more idiomatic). one is null, so, this evaluation results in a thrown NullPointerException, and execution ends then and there.

You presumably want if (one == null || two == null || one.equals(&quot;&quot;) || two.equals(&quot;&quot;)) {.

Except, you don't want that either.

null isn't a standin for 'empty'. null is best taken as meaning: Unknown or no value.

So, if I ask you: is 'this unknown value in this other unknown value', the only possible answer is: "I don't know".

If you answer that question with either true or false, you're lying, or at least guessing. Don't do that. It is perfectly sensible in this scenario to throw a nullpointerexception if null is provided.

Sometimes you read input from external systems and it is perfectly acceptable to explicitly choose to treat null input as empty strings or some other sentinel value. If that's the case, do that, explicitly, right there, on the system border.

huangapple
  • 本文由 发表于 2020年8月10日 01:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定