问题1 有没有办法注解一个参数,使其不会被传null,或传null会警告,我指的不是值为null的String,@nonnull没用
问题2
当this.getON_title()+this.getON_content()+this.getON_source()加起来是null,
给 public static boolean isNotNullNorEmpty( String string)传的是null还是值为null的String
后面debug调试用logcat测出2021-11-01 01:38:17.414 16142-16142/com.example.myapplication D/boolean_test: Ordinary_Note_isNotNullNorEmpty: title null content null source null
//getON_title()、getON_content()、getON_source()获取的是String或null
if(StringFunction.isNotNullNorEmpty(this.getON_title()+this.getON_content()+this.getON_source()));
问题3
为什么黄背景提示string==null永远是false
find cause为什么是“string”was dereferenced
public static boolean isNotNullNorEmpty( String string) {
boolean b1 = string==null;
boolean b2 = string.length()==0;
boolean b3 = (string==null || string.length()==0);
Log.d("boolean_test", "b1 "+b1+" b2 "+b2+" b3 "+b3);
if(string==null || string.length()==0){
Log.d("boolean_test", "StringFunction_isNotNullNorEmpty: "+"false");
return false;
}
Log.d("boolean_test", "StringFunction_isNotNullNorEmpty: "+"true");
return true;
}
问题4
上面boolean b2 = string.length()==0;
为什么string是null时,length方法没有抛出异常
问题5
logcat为什么会得出这样的结果
2021-11-01 01:38:17.414 16142-16142/com.example.myapplication D/boolean_test: b1 false b2 false b3 false
2021-11-01 01:38:17.414 16142-16142/com.example.myapplication D/boolean_test: StringFunction_isNotNullNorEmpty: true
2021-11-01 01:38:17.414 16142-16142/com.example.myapplication D/boolean_test: Ordinary_Note_isNotNullNorEmpty: true
2021-11-01 01:38:17.414 16142-16142/com.example.myapplication D/boolean_test: Ordinary_Note_isNotNullNorEmpty: title null content null source null
下面是相关代码
package com.example.idea.models_class;
import android.util.Log;
import com.example.idea.Function.StringFunction;
import org.litepal.crud.LitePalSupport;
public class Ordinary_Note extends LitePalSupport {
private int id;
private String ON_title;
private String ON_content;
private String ON_source;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getON_title() {
return ON_title;
}
public void setON_title(String ON_title) {
this.ON_title = ON_title;
}
public String getON_content() {
return ON_content;
}
public void setON_content(String ON_content) {
this.ON_content = ON_content;
}
public String getON_source() {
return ON_source;
}
public void setON_source(String ON_source) {
this.ON_source = ON_source;
}
public boolean isNotNullNorEmpty(){
String string = this.getON_title()+this.getON_content()+this.getON_source();
if(StringFunction.isNotNullNorEmpty(string)){
Log.d("boolean_test", "Ordinary_Note_isNotNullNorEmpty: "+"true");
Log.d("boolean_test","Ordinary_Note_isNotNullNorEmpty: "+"title "+this.getON_title()+" content "+this.getON_content()+" source "+this.getON_source());
return true;
}
Log.d("boolean_test", "Ordinary_Note_isNotNullNorEmpty: "+"false");
return false;
}
}
public class StringFunction {
public static boolean isNullOrEmpty(String string) {
if(string==null || string.length()==0){
return true;
}
return false;
}
public static boolean isNotNullNorEmpty( String string) {
boolean b1 = string==null;
boolean b2 = string.length()==0;
boolean b3 = (string==null || string.length()==0);
Log.d("boolean_test", "b1 "+b1+" b2 "+b2+" b3 "+b3);
if(string==null || string.length()==0){
Log.d("boolean_test", "StringFunction_isNotNullNorEmpty: "+"false");
return false;
}
Log.d("boolean_test", "StringFunction_isNotNullNorEmpty: "+"true");
return true;
}