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

Frame always on top of my program only
Data reading from file, file name pass through Commandline argument in java
Active MQ 5.10.0 message re-delivery policy not working
How to use StAX parser with umlauts (ÄÜÖ)?
Issue with unrecognized option in ANT
Create a jar file using jdev that includes weblogic.jar
how to connect two schemas one in local host and one in remote computer
Java.NullPointerException Android [closed]
Manipulation of Word Document using OOXML?
Setting Jackson 2 with Jersey and Tomcat
Java date field tolerating different locales/mistakes
How to keep auto track back the latest Trainer ID number and plus 1 to it in Java?
Exception in thread “AWT-EventQueue-0”NullPointerException
Java Applet Thread Animation
Suppressing logs from AppClassLoader
compile assembly:single error

Categories

HOME
variables
clips
image-processing
isabelle
deezer
ngrx
single-sign-on
kde
path-finding
layout
fft
grep
filtering
rubygems
dxl
google-project-tango
installshield
cross-browser
qore
amazon-cloudformation
spring-kafka
volttron
spring-xd
rascal
spring-tool-suite
lombok
highlight.js
samoa
lucene.net
karma-jasmine
visual-composer
dbext
immutable.js
one-hot-encoding
clickonce
scriptcs
mustache.php
firebase-crash-reporting
angular2-aot
h2db
bpel
sql-server-2012-express
bluestacks
bcd
binary-data
code-contracts
framemaker
commit
phpfreechat
io-redirection
devextreme
mapzen
elmah
skeleton-css-boilerplate
menuitem
avconv
appcompat
celery-task
hls.js
slickedit
windows-iot-core-10
imanage
setuptools
instant
qpid
fuzzy-search
tform
itextpdf
uid
ios8-today-widget
thredds
libressl
emailrelay
map-projections
ios9.1
dereference
npapi
nsmutabledictionary
website-monitoring
operator-precedence
jscript.net
qpainter
rdtsc
openexr
dml
htmlcleaner
message-driven-bean
java.nio.file
ms-project-server-2010
dotnetnuke-5
rabl
xamlparseexception
android-hardware
ohm
zpt
external-accessory
remember-me
lpeg
dbal
iweb
temporal-database

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