null-check

Why does null check fail even though object is explicitly initialized as null

强颜欢笑 提交于 2021-02-08 10:14:53
问题 I have created a custom abstract class, which in turn of course derived classes were also created. public abstract class AbstractBaseClass ... public class ChildClass1 : AbstractBaseClass ... Now, whenever I declare for example AbstractBaseClass baseClass = null , and wherever null checks follow after this initialization, it always fails. if (baseClass == null) { // this block is never reached - condition always evaluates to false // let's say AbstractBaseClass baseClass = null is at line 10

Java string null check by != null or !str.equals(null)? [duplicate]

和自甴很熟 提交于 2021-02-07 18:21:59
问题 This question already has answers here : Java null check why use == instead of .equals() (16 answers) Closed 7 years ago . What's the best way to check for not null values in java. String query ="abcd"; query != null vs !query.equals(null). which is better?why? 回答1: 1st one is better (and the only option ), because 2nd one will throw NPE , when your value is actually null . As simple as that. Try this out: String str = null; str.equals(null); // will throw `NPE`. So basically, the test which

Java string null check by != null or !str.equals(null)? [duplicate]

久未见 提交于 2021-02-07 18:20:50
问题 This question already has answers here : Java null check why use == instead of .equals() (16 answers) Closed 7 years ago . What's the best way to check for not null values in java. String query ="abcd"; query != null vs !query.equals(null). which is better?why? 回答1: 1st one is better (and the only option ), because 2nd one will throw NPE , when your value is actually null . As simple as that. Try this out: String str = null; str.equals(null); // will throw `NPE`. So basically, the test which

Java 8 nested null check for a string in a map in a list

℡╲_俬逩灬. 提交于 2021-01-27 11:50:47
问题 I need to do a series of null checks ( nested null-checks ) to get an array of strings like below String[] test; if(CollectionUtils.isNotEmpty(checkList)){ if(MapUtils.isNotEmpty(checkList.get(0))){ if(StringUtils.isNotBlank(checkList.get(0).get("filename"))){ test = checkList.get(0).get("filename").split("_"); } } } Is there a better way, maybe using Java8 Optional, to perform these kind of nested checks? I unsuccessfully tried to use Optional with flatmap / map. 回答1: You could use a long

Java 8 nested null check for a string in a map in a list

谁说胖子不能爱 提交于 2021-01-27 11:46:27
问题 I need to do a series of null checks ( nested null-checks ) to get an array of strings like below String[] test; if(CollectionUtils.isNotEmpty(checkList)){ if(MapUtils.isNotEmpty(checkList.get(0))){ if(StringUtils.isNotBlank(checkList.get(0).get("filename"))){ test = checkList.get(0).get("filename").split("_"); } } } Is there a better way, maybe using Java8 Optional, to perform these kind of nested checks? I unsuccessfully tried to use Optional with flatmap / map. 回答1: You could use a long

Use Java 8 Optional in existing Java 7 code

会有一股神秘感。 提交于 2020-12-25 04:39:45
问题 I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List<VehicleMake> loadMatching(Region region, String nameStartsWith, VehicleLoader loader) { if ((nameStartsWith == null) || (region == null) || (loader == null)) { throw new IllegalArgumentException("The VehicleLoader and both region and nameStartsWith are required when loading VehicleMake matches"); } List<VehicleMake

Use Java 8 Optional in existing Java 7 code

十年热恋 提交于 2020-12-25 04:39:05
问题 I have an assignment in which I need to convert the following pre-Java 8 code to Java 8 code. Below is just one method which is giving me hard time to finish up: public static List<VehicleMake> loadMatching(Region region, String nameStartsWith, VehicleLoader loader) { if ((nameStartsWith == null) || (region == null) || (loader == null)) { throw new IllegalArgumentException("The VehicleLoader and both region and nameStartsWith are required when loading VehicleMake matches"); } List<VehicleMake

Is there a ?. operator for Java to perform null pointer checking?

孤街浪徒 提交于 2020-05-29 03:30:11
问题 When I code I am frustrated with null checks. Especially while coding data structures! I have to do it as String val; if(a != null && a.child != null) val = a.child.getValue(); else val = null; Something sort of this... Is there a operator like ?. (which is a part of C#) for Java to shorten the burden ? С# example: String val = a?.child?.getValue(); 回答1: There is no ?. operator in Java, so, as Hari notes, you have to do things the "long winded" way. However, one could argue that this is a

Short cut for a check for null and assigning a value to it if it is?

≯℡__Kan透↙ 提交于 2020-02-25 13:15:53
问题 With the new C# 8 capabilities is there a short cut now for this code structure: if (App.selectedPhrases == null) App.selectedPhrases = App.DB.GetSelectedPhrases(); 回答1: Yes, it is called Null-coalescing assignment: App.selectedPhrases ??= App.DB.GetSelectedPhrases(); C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. 回答2: App

Short cut for a check for null and assigning a value to it if it is?

爱⌒轻易说出口 提交于 2020-02-25 13:15:15
问题 With the new C# 8 capabilities is there a short cut now for this code structure: if (App.selectedPhrases == null) App.selectedPhrases = App.DB.GetSelectedPhrases(); 回答1: Yes, it is called Null-coalescing assignment: App.selectedPhrases ??= App.DB.GetSelectedPhrases(); C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. 回答2: App