我想用oracle jdk 1.8编译简单的代码:
class Foo {
public static void test(@NotNull String a) {}
}
Run Code Online (Sandbox Code Playgroud)
但是idea不了解这样的代码,建议添加:
import com.sun.istack.internal.NotNull;
Run Code Online (Sandbox Code Playgroud)
这里面编译后idea的作品,但如果运行构建通过gradle这样的build.gradle:
version '1.0-snaphshot_03.03.2017'
apply plugin: 'java'
targetCompatibility = '1.8'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
artifacts {
archives sourcesJar
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
error: package com.sun.istack.internal does not exist
import com.sun.istack.internal.NotNull;
error: cannot find …Run Code Online (Sandbox Code Playgroud) 我已经看到了几乎所有应用程序层中的每个变量都被检查的代码null.我也看到了代码几乎没有这个.
if(object != null){}
Run Code Online (Sandbox Code Playgroud)
检查变量是否为NULL的最佳做法是什么?它真正有意义,并且变得NullPointerException非常糟糕 - 所有这些null检查可能是您的应用程序健康状况不佳的症状吗?
我有一个类似于的简单数据集类:
class DataSet {
private String value;
private String additionalValue;
public DataSet(String value, String additionalValue) {
this.value = value;
this.additionalValue = additionalValue;
}
public String getAdditionalValue() {
return this.additionalValue;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个ArrayList<DataSet>并添加了一个新元素DataSet("Value1", null).
现在在某些时候我需要检查有值的条目"Value1"是否有additionalValue,如果有,它是什么.
我做了一个简单的循环检查value.equals("Value1") == true,然后我这样做:
if (element.getAdditionalValue() != null) {
return element.getAdditionalValue();
}
Run Code Online (Sandbox Code Playgroud)
但是,只要它到达if语句,它就会抛出一个错误,表示值为null.我怎样才能让这个它不会引发错误,只是跳过的return声明,如果additionalValue是null?
编辑:
但问题是元素不能null在additionalValue它通过element.getValue.equals("Value1")条件时检查它.
for (DataSet element : dataSet) { …Run Code Online (Sandbox Code Playgroud) 我正在阅读与处理空值有关的这篇文章.
其中一个建议(根据SO帖子)是在空值无效时使用Assert.
我(到目前为止)在测试项目中广泛使用了null.在普通代码(测试项目除外)中使用Asserts语句对我来说很奇怪.
为什么 - >因为我从未使用过这种方式,所以也不要在任何书中读过它.
问题
1.使用Asserts作为前提条件是否可行
2. 断言的优点/缺点检查参数并抛出Argument___Exception
如果重要,我要求.NET(不适用于java)
我收到了一些关于 StaggeredGridLayoutManager 中发生的 NullPointerException 的崩溃报告。看起来我的用户中有大约 10% 遇到了这个问题。
我有一个非常基本的 RecyclerView 和一个基本的适配器,没什么特别的,我确实尝试在我的设备中重现这个错误,但完全没有运气。
这是我收到的原始报告:
java.lang.NullPointerException
at android.support.v7.widget.StaggeredGridLayoutManager.recycleFromStart(StaggeredGridLayoutManager.java:1661)
at android.support.v7.widget.StaggeredGridLayoutManager.recycle(StaggeredGridLayoutManager.java:1529)
at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1471)
at android.support.v7.widget.StaggeredGridLayoutManager.scrollBy(StaggeredGridLayoutManager.java:1846)
at android.support.v7.widget.StaggeredGridLayoutManager.scrollVerticallyBy(StaggeredGridLayoutManager.java:1764)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3062)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(NativeStart.java)
Run Code Online (Sandbox Code Playgroud)
我不知道发生了什么。我想也许我在布局管理器之前设置了适配器,但不,事实并非如此。
我希望有人可以对此有所了解。
提前致谢!
更新:
我正在从我的应用程序中复制一些与 RecyclerView 相关的代码。
此代码来自片段:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView.LayoutManager layout = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
mRecyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(layout);
mRecyclerView.setAdapter(mAdapter);
} …Run Code Online (Sandbox Code Playgroud) 返回 Hibernate 唯一结果的最简洁方法是null什么?
这个解决方案有什么问题吗:
public Category getCategoryById(int id) {
Object result = currentSession.createCriteria(Category.class)...uniqueResult();
return (Category) result;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
在我的代码NullPointerException中,当a List为null 时,我通常使用这种方法来避免s in语句:
if (myList != null && myList.size() > 0) {
for ( MyObj obj : myList ) {
System.out.println("MyObjStr: "+obj);
}
}
Run Code Online (Sandbox Code Playgroud)
是否有其他方法可以在不写"if"语句的情况下执行相同操作,但使用相同的"for"语句?
假设我有一个List<SomeObject>函数返回该对象的引用(如果可用).
SomeObject GetSomeObject(List<SomeObject>, int x){
/* Search for object in list that has a properties
with value x */
if (found)
return /* The object found */
else
return NULL;
}
void DoSomething(SomeObject S){
if(S!=NULL){
/* Do action 1 */
}
else{
/* Do action 2 */
}
}
Run Code Online (Sandbox Code Playgroud)
我已经读过某个地方,返回NULL不是干净代码的一部分.所以我想知道这个案例的等效代码是什么.
更新:我已经阅读了这个问题,我认为我的情况有所不同.在这种情况下,if NULL返回然后什么都不做,而我需要做一些事情,如果NULL返回
许多建议使用CollectionUtils.isNotEmpty(coll)而不是coll != null在下面的用例也。
if (CollectionUtils.isNotEmpty(coll)) {
for (String str : coll) {
}
}
Run Code Online (Sandbox Code Playgroud)
代替
if (coll != null) {
for (String str : coll) {
}
}
Run Code Online (Sandbox Code Playgroud)
有什么理由/优势来CollectionUtils.isNotEmpty(coll)代替这里吗?谢谢。
我没有找到适合我的问题的解决方案。据我所知,正如《Clean Code》一书中所说,返回 null 并不是编写干净代码的好方法。然而,对于这种做法有很多不同的看法,我不确定哪一种适合我的职能。
private EArea getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : exsitingAreas) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return existingArea;
}
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
如果没有现有的类似区域,我应该返回什么?
PD:为了优化我的代码,如果找到现有区域,我将通过返回来打破循环。