null

What to do with null fields in compare()?

不羁的心 提交于 2020-01-01 07:32:00
问题 In Java, I use a class in which some fields can be null . For example: class Foo { String bar; //.... } I want to write a BarComparator for this class, private static class BarComparator implements Comparator<Foo> { public int compare( final Foo o1, final Foo o2 ) { // Implementation goes here } } Is there a standard way to deal with the fact that any of o1 , o2 , o1.bar , o2.bar can be null , without writing lots of nested if ... else ? Cheers! 回答1: I guess you could wrap the call to the

Moshi/Kotlin - How to serialize NULL JSON strings into empty strings instead?

怎甘沉沦 提交于 2020-01-01 05:29:10
问题 I'm trying to write a null-safe String adapter that will serialize this JSON {"nullString": null} into this: Model(nullString = "") so that any JSON with a 'null' value that I expect to be a String will be replaced with "" (assuming there exists a data class like this: data class Model(val nullString: String) ) I wrote a custom adapter to try and handle this: class NullStringAdapter: JsonAdapter<String>() { @FromJson override fun fromJson(reader: JsonReader?): String { if (reader == null) {

Best way to handle a NULL

[亡魂溺海] 提交于 2020-01-01 04:02:26
问题 At the top of my functions I'm trying the best way to handle a null coming into my procedures in C#. Which is the best way for checking and handling the null and why? I've added the complete code of what I'm using right now and Resharper is telling me to use Option #1. Normally I do what it says as I understand why it makes it more efficient. This time though I'm not sure so I must ask. Option #1 if (sender == null) return; // Code goes here or Option #2 if (sender != null) { // Code goes

Why does Go have typed nil?

时间秒杀一切 提交于 2020-01-01 03:52:34
问题 Why does Go have typed nil ? It throws an explicit interface conformation check for convenience. What's the problem of untyped nil and what did the designers want to solve with typed nil ? 回答1: It sounds like you're asking about this error message: http://play.golang.org/p/h80rmDYCTI package main import "fmt" type A struct {} type B struct {} func (a *A) Foo() { fmt.Println("A") } func (b *B) Foo() { fmt.Println("B") } func main() { n := nil n.Foo() } This prints: prog.go:17: use of untyped

pandas check if column is null with query function

℡╲_俬逩灬. 提交于 2020-01-01 02:30:10
问题 I have pandas dataframe that I want to execute on it query function with isnull() or not isnull() condition like that: In [67]: df_data = pd.DataFrame({'a':[1,20,None,40,50]}) In [68]: df_data Out[68]: a 0 1.0 1 20.0 2 NaN 3 40.0 4 50.0 if I use this command: df_data.query('a isnull', engine='python') or this command: df_data.query('a isnull()', engine='python') I get an error: In [75]: df_data.query('a isnull', engine='python') File "<unknown>", line 1 a isnull SyntaxError: invalid syntax In

What is the best way to convert an int or null to boolean value in an SQL query?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 01:29:05
问题 What is the best way to convert an int or null to boolean value in an SQL query, such that: Any non-null value is TRUE in the results Any null value is FALSE in the results 回答1: To my knowledge (correct me if I'm wrong), there is no concept of literal boolean values in SQL. You can have expressions evaluating to boolean values, but you cannot output them. This said, you can use CASE WHEN to produce a value you can use in a comparison: SELECT CASE WHEN ValueColumn IS NULL THEN 'FALSE' ELSE

@JsonInclude(Include.NON_NULL)

半世苍凉 提交于 2019-12-31 23:55:25
  前端的同事要求说尽量不要有null,可有为空串“” 或者 0 或者 [], 但尽量不要null。   所以@JsonInclude(Include.NON_NULL) 这个注解放在类头上就可以解决。 实体类与json互转的时候 属性值为null的不参与序列化    import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; @JsonInclude(Include.NON_NULL) public class WithdrawDetail implements Serializable { } 或者 WithdrawDetail wd = new WithdrawDetail(); wd.setSerializationInclusion(Include.NON_NULL);   实际效果 全局配置 springMVC.xml <!-- 默认的注解映射的支持 比如requestMapper之类的 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json

Either OR non-null constraints in MySQL

狂风中的少年 提交于 2019-12-31 21:35:08
问题 What's the best way to create a non-NULL constraint in MySQL such that fieldA and fieldB can't both be NULL. I don't care if either one is NULL by itself, just as long as the other field has a non-NULL value. And if they both have non-NULL values, then it's even better. 回答1: MySQL 5.5 introduced SIGNAL, so we don't need the extra column in Bill Karwin's answer any more. Bill pointed out you also need a trigger for update so I've included that too. CREATE TABLE foo ( FieldA INT, FieldB INT );

mSecurityInputMethodService is null in logcat

假装没事ソ 提交于 2019-12-31 13:05:24
问题 I wrote a little android app that should display the current location (last known location) of the smartphone. Although I copied the example code, and tried several other solutions, It seems to have the same error every time. My app consists of a button. Pressing the button should log the longitude and latitude, but only logs "mSecurityInputMethodService is null" . Here's the MainActivity.java : public class MainActivity extends Activity { int response; @Override public void onCreate(Bundle

fputs/puts dangerous (in C)?

心已入冬 提交于 2019-12-31 07:00:24
问题 I've been having trouble with fputs lately: when printing some strings in a text file with fputs, it happens I get other characters than A-Z, a-z, 0-9 in (chars that aren't part of the string). I made absolutely sure the strings all end with the null character. Unfortunately I can't give you more information, since I did not personally test the program, that was the feedback I received. But after I replaced fputs with fprintf it worked properly. So my question is: is fputs sort of dangerous?