This question already has answers here:
No Exception while type casting with a null in java
(10 answers)
Closed 7 years ago.
When we are using System.out.println(null) then we get compilation error and to resolve this we typecast null like:
System.out.println((String)null)
and then it starts working fine.
My questions is how we are allowed to typecast null when we say that null is something which is not pointing to any location and thus has no value in itself.
Also if null is not pointing to any location then the object which is null will get Garbage collected then why we are using it at first place? I know it is litle bit out of the topic but it came to my mind.
I have seen few answers in below link but they mostly talk about println method functionality and implementation but I want to know about null only.
No Exception while type casting with a null in java
In Java, "null" is a special literal of the null type. It can be cast to any reference type, but not to any primitive type such as int or boolean. The null literal doesn't necessarily have value zero. And it is impossible to cast to the null type or declare a variable of this type.
That's why it does not throw error you can also do this null==null
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
Am using Bean class to get/set value for the attributes. In some cases am gettig Exception in thread "main" java.lang.NullPointerException error due to the value is null. What will be the best practice to handle null pointer exception when we get/set values from/to bean class.
Is that ternary operator is good to use or any other suggestions?
Please find the below line of code where am getting null pointer exception.
doc.setCatalog_title(sourceAsMap.get("catalog_title").toString());
The nullpointer exception basic reason is that you are calling a method or variable from null variable where with null variable I mean a variable which is currently not holding reference of any object. So, the easy way to avoid it is assign that variable a refernce on which subsequent tasks can be called
Now this can be handled in n no. of ways, out of which some basic ways are:
1) Using if condition
if(doc!=null && sourceAsMap!=null && sourceAsMap.get("catalog_title")!=null)
doc.setCatalog_title(sourceAsMap.get("catalog_title").toString());
2) Using ternary operator:
doc = null == doc ? new Document():doc;
doc.setCatalog_title(sourceAsMap!=null && sourceAsMap.get("catalog_title")!=null ? sourceAsMap.get("catalog_title").toString() : null);
Hope that helps
You can use Guava Preconditions (https://github.com/google/guava/wiki/PreconditionsExplained), as it good practice to check preconditions of your class before executing it. As you can use checkNotNull(T).
If you are expecting null in your system then instead of using a null check, use Optional class https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html,
If you are using Java 7 then you can use Optional from Guava https://github.com/google/guava/wiki/UsingAndAvoidingNullExplained
This question already has answers here:
Null check in Java 8 Elvis operator?
(2 answers)
Closed 5 years ago.
I have a situation where I need get the property of a Java object if the object exists or null.
Something like foo == null ? null : foo.bar
Is there an operator available in java to do the same?
No, there is no propagate null operator in Java, cf. C# for example, which does have one. (It was a proposal at some point in Java's evolution, but has not sadly yet been incorporated into the language: foo = foo?.bar would be an obvious notation.)
You need to write this out longhand, as you have done.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I have the following code:
final JsonObject source = source.toJson();
final JsonElement relatedSpace = source.get("myField");
if(relatedSpace.isJsonObject()){
//do something
}
isJsonObject() does return instanceof JsonObject; according the the Gson code. instanceof should do a null check first, so I'm not sure how it's possible that my code is throwing a NullPointerException when relatedSpace is null. I can easily check for null first to fix the issue, but I'm wondering why this happens.
The . operator is executed prior to the method after it.
If the object is null it will cause a NullPointerException, regardless to the content of the method.
Because relatedSpace is set to null (before you use it) in the situation you describing here. You actually get null from the following expression -
final JsonElement relatedSpace = source.get("myField");
(That means thee is no JsonElement with associated with the key "myField")
To avoiding null you may simply just check it before using it -
if(null!=relatedSpace && relatedSpace.isJsonObject()){
//do something
}
For more on how to avoid NullPointerException you may check these nice suggestions
You are getting NPE because you are doing a method call on null reference. What you are confused about is between operator and methods in java. instanceof is an operator in java which works with null references too but the method calls on null reference is an exceptional condition and should be handled by null check.Other thing that you can do is to ditch the GSON method altogether and do a direct instance of check.
This question already has answers here:
Interview : Java Equals
(7 answers)
Closed 6 years ago.
I have seen in many places that the constant value is used first and then the variable for example ("ram").equals(a) and in many places I have seen that the variable is used first and then the constant value with which they want to compare for example a.equals("ram").
what is the difference between a.equals("ram") and ("ram").equals(a) ?
which one is better to use and why ?
The first style is safer in situations when variable a is allowed to be null, because you can skip null checking.
When you write
if (a.equals("ram")) { ... }
you must ensure that a is not null to avoid a null pointer exception. This is not necessary for
if ("ram".equals(a)) { ... }
because equals() method is required to process null arguments without throwing an exception:
For any non-null reference value x, x.equals(null) should return false.
("ram").equals(a) is better. The code will never break even if a is null.
saves us from null pointer exception.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is null in Java?
How is null implemented in Java?
Suppose I say
String x = null;
How is this null stored internally?
Check out this question:
What is null in Java?
Basically, usually stored as 0's same as C++ but this can be implementation-specific and so you shouldn't rely on it.
As per java specification it is a sort of literal.
There is also a special null type, the type of the expression null,
which has no name. Because the null type has no name, it is impossible
to declare a variable of the null type or to cast to the null type.
The null reference is the only possible value of an expression of null
type. The null reference can always be cast to any reference type. In
practice, the programmer can ignore the null type and just pretend
that null is merely a special literal that can be of any reference
type.
In Java the runtime must ensure that all heap-allocated memory is zeroed out before the pointer to the block is exposed to the Java code. The all-zeroes block will be interpreted as the initial values of the instance fields. This pretty much guarantees that null will be implemented as a zero value in any JVM implementation.
The null value is stored in the variable x.