首页 \ 问答 \ 注释或一些自动代码生成用于设置默认值以避免Android / Java中的NullPointerExceptions?(annotation or some automated code generation for setting default values to avoid NullPointerExceptions in Android/Java?)

注释或一些自动代码生成用于设置默认值以避免Android / Java中的NullPointerExceptions?(annotation or some automated code generation for setting default values to avoid NullPointerExceptions in Android/Java?)

我想知道在Android中是否有任何设计模式,第三部分库或任何处理null的Annotation处理器?

例如,假设您使用Gson解析JSON Response并将响应字符串直接映射到带有Gson的Object named "Person"Object named "Person"

Now assume Person has 3 fields
1) Name
2) Age
3) Gender

并且您将要在UI上显示它们,我的问题是如何NULL pointer Exception without checking for Nulls for each field when displaying this data.避免使用NULL pointer Exception without checking for Nulls for each field when displaying this data.

是否有注释处理器或允许您提供空替换值的任何库? 像这样的东西

@defVal("N/A")
private String name;
@defVal(0)
private int age;
@defVal("N/A")
private String gender;

所以我们可以避免所有这些字段的空检查语句,并且在调用getter时它提供值,如果存在则为null,然后提供默认值。 有什么建议么?

编辑:我知道处理Null Pointer崩溃的两种方法

1) to use try catch 
2) to use Getters and inside every getter i should check the null value and sets a default value if there is null

但我的问题是有没有办法减少这些代码行并使用一些自动生成的东西? 我相信某种注释


I want to know if in Android there is any design pattern,third part library or any Annotation processors that handles null?

For example, let's say you parse a JSON Response using Gson and maps the response string directly to an Object named "Person" with Gson.

Now assume Person has 3 fields
1) Name
2) Age
3) Gender

and you are about to display them on UI, my Question is how I can avoid a NULL pointer Exception without checking for Nulls for each field when displaying this data.

Is there an Annotation Processor or any library that allows you to provide null replacement values? Something like this

@defVal("N/A")
private String name;
@defVal(0)
private int age;
@defVal("N/A")
private String gender;

so we can avoid null check statements for all these fields and when calling there getters it provides the value if exists if there is null then provides default values. Any Suggestions?

EDIT: I know the two ways for handling Null Pointer crash

1) to use try catch 
2) to use Getters and inside every getter i should check the null value and sets a default value if there is null

But My Question is is there a way to reduce these lines of code and use some auto generation thing? Some kind of Annotations I believe


原文:https://stackoverflow.com/questions/36334365
更新时间:2022-08-11 22:08

最满意答案

托管服务时,您无法使用开放式泛型。 您必须指定具体类型来托管具体服务。 如果要为更多通用参数托管服务,则必须为每个参数类型创建一个新主机,并为每个参数公开一个具有唯一地址的端点。 托管服务后,它必须能够说出它接受的类型,并在服务描述(WSDL源代码)中描述它们。

WCF在您托管服务的基础上工作,该服务必须能够描述它接受的消息 - 因为它生成服务描述。 服务能够根据描述序列化消息。 任何平台上的任何客户端都可以使用该描述并向服务发送正确的消息 - 客户端将从描述中了解消息的允许内容,并且因为该服务主机必须使用具体类型 - 而不是T.

您可以通过将基类型指定为泛型参数来克服此问题,但即使在此之后,您的服务必须知道可以使用的所有派生类型而不是基类型(有多种技术可用于此但它们都不提供随机类型)。

编辑:

从技术上讲,您要求的方法是:将类型的名称从客户端发送到主服务。 主服务将检查具有该类型的服务是否已存在。 如果是,它将把服务的URL发送回客户端。 如果不是,它将通过反射创建服务并启动它。 它将存储有关新托管服务的信息,并将URL发送回客户端。

另一种变体是预先启动所有服务并具有单个WCF路由服务,该服务将请求路由到正确的服务。

这样的解决方案看起来像维护噩梦,第一个解决方案将会有更糟糕的性能,因为每个操作都需要两次网络呼叫。 此外,它通常不具有互操作性,因为您的客户必须预先获得通用合同才能调用此类服务​​。 根本不这样做 - 定义有限的已使用类集,并使用单一服务使用KnownTypeDataContractResolver


When you host the service you cannot use open generics. You must specify concrete type to host concrete service. If you want to host services for more generic arguments you must indeed create a new host for each argument type and expose an endpoint with an unique address for each of them. Once the service is hosted it must be able to say what types it accepts and describe them in the service description (source for WSDL).

WCF works on basis where you host the service which must be able to describe messages it accepts - because of that it generates service description. Service is able to serialize messages according to the description. Any client on any platform can use the description and send the correct message to the service - client will know about allowed content of the message from the description and because of that service host must use the concrete type - not T.

You can overcome this by specifying base type as generic argument but even after that your service must know about all derived types which can be used instead of the base type (there are multiple techniques for this but none of them offer random type).

Edit:

Technically what you ask for means: send the name of the type from the client to the main service. The main service will check if the service with the type already exists. If yes it will send the url of the service back to the client. If not it will creates the service via reflection and starts it. It will store information about new hosted service and sends the url back to the client.

Another variant is starting all services upfront and having single WCF Routing service which will route request to correct service.

Such solutions look like maintenance nightmare and the first one will have much worse performance because two network calls will be needed for each operation. Also it is not generally interoperable because your clients will have to get generic contract upfront to be able to call such services. Simply don't do it - define finite set of used classes and use KnownType or DataContractResolver with single service.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)