java


Why immutable null is discouraged?


I'm currently using Immutable library for generating JSON object from my web application.
Looking at this chapter, the first line says:
The use of nullable attributes is discouraged.
So my question is:
1) why? what's wrong with a null object?
2) what if I'm using a wrapper of thirdy object and I don't know if item is null or not, so using the classing builder code will fail:
MyImmutableWrapperObject
.builder().
.mobile(input.getMobile()) // don't know if null or not
.build();
Is there any optimal solution?
EDIT:
#JsonProperty("mobile")
public abstract Optional<String> mobile();
...
// composing builder
if (input.getMobile() != null)
builder.mobile(input.getMobile());
The produced json is:
"mobile": {
"present": false
},
How can I completely remove the empty fields?
I read this, however it uses gson.toJson which return a String object which is not my desiderable way.
POSTEDIT:
I just found that Optional doesn't show real value even if present, but it just display the true/false value, so I don't need it.
That's exactly the point IMO, you don't know if an object is null or not; so when later using it you might obviously get a NullPointerException. By using a library that prohibits that, you will not care if something is null or not null (because it will not be null), thus less code and less if-statements that pollute the code with potential null checks.
Also having a null reference might mean different things in different contexts. For example I've seen code that needs three states for a Boolean : true, false and no yet set(as null). I find that wrong, but still occasionally see it.
That's the reason why Optional was introduced in Guava and now is in the JDK. It forces the user to actively do something with an Optional. For example:
Optional<Integer> i...
if(i.isPresent()).... // you are forced to do this check
Well obviously you could do:
i.get()
but that method is documented that it might break.
the same is simply not true for an reference:
Integer i = null;
if(i == null) // this is not an operation that is mandatory
The bet article I've seen/read to date on this subject is Guava explanation

Related Links

Reading input into an int 2D array with varying number of columns per row
Does SAP Hybris uses Resful services or it can be use using SOAP too? Can we use AngularJS at UI?
Dozer: Mapping fields for specific subclasses
How to pass image View to another layout and save it inside the layout?
Adding tab spaces in a file using ant
Trigger #Scheduled method
Consume wsdl URLs data in java web based spring application, have to create a classes or jar from WSDL which will be used in application. How?
How to replace Integer objects with primitives
Joomla calls rest with session_id
Use of XPath.compile() is vulnerable to XPath injection
C# Custom Ole Server No Such Interface 0x80004002 in OleControlSite
Does Drools Kie support deploying to WildFly in domain mode?
Weblogic JMX error IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
JUnit test fails when method arg is annotated #Nonnull
How does android generate 'current.txt' it's api description file?
Whenever I run “apt-get install php5-gd php-mysql” it states “Unable to locate package php-mysql” on Ubuntu 14.04

Categories

HOME
testing
osgi
bluetooth
hook
proxy
path-finding
syntax
react-router
bookshelf.js
rubygems
ckeditor
datastax-java-driver
swagger-ui
multiple-records
floating-action-button
moonmail
clojurescript
seaborn
fatal-error
serilog
normalizr
crystal-reports-2008
plunker
arabic
kudan
rundeck
selectedindexchanged
fluentvalidation
saas
socialengine
uisplitview
exuberant-ctags
mpmediaquery
ios5
unspecified
splice
trim
android-tabhost
gulp-sourcemaps
turbogears
http-redirect
segment
revapi
dotnetzip
statsd
google-perftools
iso8601
spring-cache
gnome-shell-extensions
angular-strap
ableton-live
deadbolt-2
dstu2-fhir
trash
py2app
firebaseui
react-native-listview
captivenetwork
asp.net5
browser-link
unity-networking
dukescript
ibaction
service-accounts
arcanist
createprocessasuser
cisco-ios
unity3d-gui
graph-api-explorer
csquery
formatjs
website-monitoring
code-access-security
zend-route
project-planning
android-radiobutton
rtmfp
meteor-velocity
openlaszlo
soundtouch
hungarian-algorithm
xamlparseexception
comaddin
type-equivalence
ocunit
referrer
anonymous-methods
datawindow
armcc
regsvr32
qtkit
hgsubversion
php-parser
javax.script
propagation
android-sdk-2.1
asp.net-mvc-areas
coda-slider
revisions
vc90
castle-monorail
backcolor
uimenucontroller
ubuntu-9.04

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App