Java vs Objective C in case of nullpointer exception

Why does Java often throw a null pointer exception if I try to access null values ​​while Objective-C does not?

Java:

public class SubscribeEmailActivity { String status; public static void main(String[] args){ if(Status.equals(null)||status.equals(""){ // i am getting error here } } } 

Goal C:

 //in .h file NSString *exp; //in .m file if ([exp isEqualToString:NULL] ||[exp isEqualToString:@""]) { // i am not getting any error } 
+6
source share
7 answers

Ultimately, the only way to completely solve this problem is to use a different programming language:

  • In Objective-C, you can make the equivalent of calling a method on nil, and absolutely nothing will happen. This makes unnecessary zeros checks, but can make error diagnosis difficult.
  • In Nice, a Java-based language, there are two versions of all types: a potentially null version and a non-empty version. You can use methods only for non-empty types. Potentially null types can be converted to non-empty types through an explicit check for null. This greatly facilitates the understanding of the need for zero checks and where they are not.
+6
source

The problem is your assessment in java, it should be:

 if(status == null||status.equals("")) { //Do not try to dereference status.... 

It should be noted that this only works because Java and other languages ​​can reduce logical grades. In this case, some simple Boolean algebra:

 true || ??? => true 

Once the first term evaluates to true, we know that the conditional OR will also evaluate to true, there is no need to study the second term.

Alternatively, you can use Apache Commons isEmpty() or collapse your own:

 public static final boolean isEmpty(String arg0) { if(arg0 == null) return true; if(arg0.trim().length() < 1) return true; return false; } 

The featured version is akin to isBlank in Apache Commons.

+7
source

This probably won't answer your question, but one rule to throw an exception from a null pointer and at the same time avoid a null check is to invert the check as follows:

 Status status = null; if("".equals(status)){ //.... } 

which will return false.

using:

 if(status.equals("")){ } 

throws an NPE exception.

+4
source

In calls to the Objective-C method, it is considered equivalent to sending the message, and sending the message to nil does nothing. This means that you do not need to write the null checks that are required in Java, which is more convenient for programmers who can write more compact code.

On the other hand, it can also be a source of error: if you expected to send a message to a real object, but instead send it to nil , in Objective-C you will not receive a warning. In Java, this operation throws a NullPointerException , making it clear that you have an error.

Since language developers must compromise between convenience and security, none of the options is objectively better. Objective-C designers decided to better implement nil as a black hole: it swallows everything you throw at it. Java designers decided that it was better to deal with null with an obvious error. In fact, nothing more.

+4
source

If the status variable is null in Java, then trying to call status.equals() will throw a NullPointer exception. isEqualToString not a member function of Objective-C strings, it just compares the string and the object.

+2
source

It is simply a question of how the language was developed. You can look here if you are brave enough to see how objc_msgSend works and why you can send messages to Nile objects.

+2
source

In your code

if(Status.equals(null)||status.equals("") you are comparing String with null (null is not a string).

Cannot compare String to non-String. In the case of iOS coding, I believe that

[exp isEqualToString:NULL]

converts the argument to nothing like null or nil are the same, and then compares it, so it works.

+2
source

Source: https://habr.com/ru/post/951319/


All Articles