Which exception is thrown when an application attempts to use null when an object is required?

An exception is an error that occurs at runtime which disrupts the normal flow of the application. It is either generated by the Java Virtual Machine (JVM) in response to an unexpected condition or by your code as a result of executing a throw statement.

Exceptions generated at runtime are called unchecked exceptions, since it is not possible for the compiler to determine that your code will handle the exception. The exception classes that descend from RuntimeException and Error classes are unchecked exceptions.

Thrown exceptions are referred to as checked exceptions. The compiler will confirm at compile time that the method includes code that might throw an exception. Moreover the compiler requires the code that calls such a method to include this call within a try block, and provide an appropriate catch block to catch the exception.

What is a NullPointerException?

One of the most common unchecked exceptions is the NullPointerException. It is thrown whenever an application attempts to use an object reference that has a null value.

Object o = null;
String s = o.toString();

If you call a static method on an object with a null reference, you won’t get an exception and the code will run.

public class Player {
private static final String type = "Human";
private int age;
public static String getType() {
return type; }
public int getAge() {
return age; }

public static void main(String[] args) {
Player player = null;
System.out.println(player.getType()); // Human
System.out.println(player.getAge()); // throws NPE
}
}

Static members belongs to the class rather than instance. So at compile time, player.getType() is converted to Player.getType()

This is admittedly very misleading when reading someone else’s code, and it is best practice to always use the class name when calling a static method.

Which exception is thrown when an application attempts to use null when an object is required?

This will be a short post, where I want to share a life hack with you that I learned about recently. Let’s start off by examining the problem.

According to the java docs, a null pointer exception (NPE) is thrown when an application attempts to use an empty value in cases where a value is required. For example, trying to trim an empty value, or trying to change an empty object will both throw an NPE. In fact, most activities and expressions will throw NPE if an empty object is used. To the best of my knowledge, only the following cases will not throw an NPE:

  • equality comparisons e.g. $Entity/Attribute = empty (but not in combination with not )
  • XPath constraint during retrieve e.g.[Module.Associaiton < $Entity/Attribute]
  • string concatenation e.g. 'value is '+$Entity/Attribute → value is null
  • retrieve over association → will return empty

To avoid NPEs it is always advisable to check variables for empty before using them in any activity or expression that throws the exception. As a developer behind the automatic code reviewer for Mendix, I have analyzed many real-world Mendix projects to find what are the most common bugs. As you can probably guess a lot of the bugs revolve around null pointer exceptions.

Interestingly, according to the data most NPEs occurs not because there is no check for empty, but because the check was implemented incorrectly. How could something as simple as an empty check be implemented incorrectly you ask? Try to spot the error in the microflow bellow

Which exception is thrown when an application attempts to use null when an object is required?

https://modelshare.mendix.com/models/a2146fb1-a5ef-4b35-8add-e7a11d2240de/act-object-set-to-empty?embed=true

At first sight, this flow looks correct. That is precisely why it is so difficult to detect these errors. Upon closer inspection, one can see that the expression in the exclusive split is $Object = empty. This contradicts the caption of the exclusive split "has Object". To fix this error one could change the expression to $Object != empty. But what if I were to tell you there is a better way, a safer way:

Which exception is thrown when an application attempts to use null when an object is required?

https://modelshare.mendix.com/models/154402e7-e903-4528-b8ab-8d93f9cb0af1/act-object-set-to-empty-safe?embed=true

With this construct, it is impossible to repeat the mistake from above where the caption and the expression were contradicting. That is a neat trick, don’t you agree!?

I hope you enjoyed this post and that it helps you build better Mendix apps.

Automatic code reviewer for Mendix

The automatic code reviewer for Mendix checks for over 100 rules in different categories such as security, performance, and maintainability. There are also around 40 reliability rules whose goal is to find bugs in your app such as the ones above.

Which exception used for referring a null object?

NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from a null object.

What is Java Lang NullPointerException null?

lang. NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.

Which exception is thrown when an instance method of a null object gets called access or modifies the field of a null object?

The null pointer exception (NPE) you're seeing is because you're invoking an instance method on a null reference. You need to set the reference to an actual object of that class (or a subclass of it).

What is NullPointerException in Salesforce?

The "NullPointerException" is an error in Salesforce which occurs when a line of code is trying to use an object that has not been instantiated or expecting that a field is not empty. In this case, SyncApps was unable to set the field, leaving it empty, creating the error.