【java – 在Stream中过滤Null项】教程文章相关的互联网学习教程文章

java-如何避免嵌套null检查【代码】

我想从存在于类内部的API中检索字段.是的,我知道这违反了得墨meter耳法则,但我别无选择. 例getClassA().getClassB().getClassC().getClassD().getAccountId();因此,添加空检查作为其不良的代码气味,所以我附带以下代码:try{ getClassA().getClassB().getClassC().getClassD().getAccountId(); }catch(NullPointerException ex){S.O.P("Null Found"); }要么ClassA a = getClassA(); if(a!=null){ ClassB b = a.getClassB(); So on....

java-在Robolectric @config中指定清单路径时,它将引发Null指针异常【代码】

我第一次尝试与Robolectric合作.我阅读了互联网上的所有许多文档,并准备了以下测试代码以开始使用.当我准备发布代码时,我利用了这些资源=> https://github.com/codepath/android_guides/wiki/Robolectric-Installation-for-Unit-Testing https://github.com/codepath/android_guides/wiki/Android-Unit-and-Integration-testing http://www.vogella.com/tutorials/Robolectric/article.html@RunWith( RobolectricTestRunner.class ...

java-道奇-已知为null的值的冗余nullcheck【代码】

下面是我得到声纳违反的代码public static Connection getConnection(String db)throws SQLException {Connection connection = null;try {Driver dbRriver = (Driver) Class.forName(driver).newInstance();Properties props = new Properties();props.put("user", PropertyLoader.get("user123"));props.put("password", PropertyLoader.get("pass"));connection = dbRriver.connect(PropertyLoader.get("URL"), props);} catch ...

java-是否需要在Streams中使用orElse(null)?【代码】

我使用此代码段在字符串列表中找到包含“ o”字符的第一项:List<String> words = Arrays.asList("hi", "hello", "bye", "goodbye"); String s1 = words.stream().filter(word -> word.contains("o")).findFirst().orElse(null);因此,这将找到第一个具有“ o”(hello)的项目,如果未找到任何内容,它将返回orElse(null)指定的null.所以我的问题是: 是否真的需要使用orElse(null),如果我没有指定它,如果找不到任何匹配项,它将默认返回...

java-Spring / Thymeleaf:无法在null上找到属性或字段,但仍在渲染【代码】

我有一个Spring / Thymeleaf应用程序org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'projectName' cannot be found on null但是,页面看起来正常.所有变量都与数据一起呈现.我只是担心每个请求都会引发异常. 这是控制器:@Controller @RequestMapping("/download") public class AppDownloaderController {@AutowiredInstallLinkJoinedService installLinkJoinedService;@Requ...

java-具有Spring Data JPA存储库的@NamedStoredProcedureQuery-类型不能为null错误【代码】

我有一个JPA实体类RequestSummary,该类最初是从数据库视图填充的.我出于性能原因试图将其重构为使用存储过程,但是在正确设置它方面遇到了问题. 我有一个存储过程’sp_bsc_request_summary’,它将staffId作为参数. 我的spring数据仓库看起来像这样public interface RequestSummaryRepository extends JpaRepository<RequestSummary, Long> {@Procedure("procedureFindAll") public List<RequestSummary> procedureFindAll(@Param("s...

java-在SecretKey上调用.getEncoded()返回null【代码】

我使用以下代码生成AES密钥:KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder("db_enc_key", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT);KeyGenParameterSpec keySpec = builder.setKeySize(256).setBlockModes("CBC").setEncryptionPaddings("PKCS7Padding").setRandomizedEncryptionRequired(true).setUserAuthenticationRequired(true).setUserAuthenticationValidityDurationSe...

java-Assertj验证集合中每个项目的字段始终为null【代码】

我正在寻找一种解决方案,以检查集合中的每个项目都具有字段ExpectedNullField null. 以下内容不起作用: assertThat(aCollection).extracting(“ expectedNullField”).isNull(); 请注意以下工作正常:assertThat(aCollection).extracting(“ expectedNotNullField”).isNotNull(); 有人帮我吗? 谢谢.解决方法:如果您知道尺寸(假设是3),则可以使用assertThat(aCollection).extracting("expectedNullField").containsOnly(null, nu...

java-Retrofit对象返回为null【代码】

我已经建立了RetroFitService以返回一些对象.在MainActivity中,我只需单击一下按钮即可调用该服务.我似乎正在获取某种对象,但是我不觉得它实际上是从我指定的REST API返回的.它显示在调试器中,但其属性为null:bFetch.setOnClickListener(v -> {v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.image_click));RetrofitService service = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitServic...

java-在Filter中具有@Value属性的SpringBoot为null【代码】

我正在使用SpringBoot的@Value注释从默认的application.properties文件填充对象属性,但是在Filter覆盖中看到一些奇怪的行为. 我的调试器中有两个断点:@Componentpublic class BasicFilter implements Filter {@Value("${aad.client_id}")private String clientId;@Beanpublic FilterRegistrationBean registerFilter() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new BasicFil...

java-位置返回NULL【代码】

我正在编写一个返回经度和纬度的Android应用,但是location保留Null值. 请看看您是否能了解原因,整天困扰着我.代码如下:public class example extends Activity {public static double latitude;public static double longitude;LocationManager lm;LocationListener ll;Location location;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInst...

java-TreeMap.get()返回Null,即使键存在【代码】

我试图从TreeMap获取,但即使键存在,它也会返回null.HashCode和等式仅基于单词.可比性基于频率.public static void main(){TreeMap<Word,Integer> test = new TreeMap<>();test.put(new Word("pqr",12),1);test.put(new Word("abc",2),1);Integer prq = test.get(new Word("pqr",1));System.out.println(prq);prq = test.get(new Word("pqr",12));System.out.println(prq);}public class Word implements Comparable<Word>{String w...

java-为什么我将null作为数组的值?【代码】

这个问题已经在这里有了答案: > Why are my fields initialized to null or to the default value of zero when I’ve declared and initialized them in my class’ constructor? 4个我有一个类Hra1,它定义了游戏规则(game = hra).问题是,我得到一个空值,例如尽管在构造函数中创建了arraypoleMinci,但poleMinci == null.换句话说,玩家的move方法始终返回false. 构造函...

java-getIntent()始终返回null【代码】

此代码启动活动:Intent intent = new Intent(context, GameActivity.class);intent.putExtra("load", true);startActivity(intent);这是我尝试获得新活动的意图的方法:private Intent intent = this.getIntent();有人告诉我尝试包括:@Override protected void onNewIntent(Intent intent) {super.onNewIntent(intent);setIntent(intent); }但这没有帮助.在几次崩溃之后,我添加了:if (intent == null) Log.w("Intent", "Intent ...

java-Spring-注入null而不是在找不到bean时抛出异常【代码】

我上课要打针@Inject IMyInterface controller;但是没有实现IMyInterface的类.如何使spring注入为null而不是抛出NoSuchBeanDefinitionException,因为我不一定需要它出现?解决方法:使用@Inject(假设它是javax.inject.Inject),我认为没有直接的方法可以使绑定成为可选.但是,如果使用@Autowired,则可以将其required属性设置为false:@Autowired(required = false) IMyInterface controller;