Articles

Affichage des articles du mars, 2023

The Importance of Checking for Null Values and the Order of Conditions in Java

Image
If you have been programming in Java for a while, you have probably encountered NullPointerExceptions at some point. NullPointerExceptions occur when you try to call a method or access a variable on a null object reference.  In this article, we will discuss a common cause of NullPointerException and how to avoid it. Consider the following code: !foundEmployeeFromAPI.getFirstName().isEmpty() && !foundEmployeeFromAPI.getFirstName() == null This code is checking whether the firstName field of the foundEmployeeFromAPI object is not empty and not null. However, if the firstName field is null, then we will have a NullPointerException in Java. This is because we are calling the isEmpty() method on a null object reference. To avoid this issue, we should first check if the object reference is not null before checking if it is empty. Here is the corrected line of code: foundEmployeeFromAPI.getFirstName() != null && !foundEmployeeFromAPI.getFirstName().isEmpty() To summarize, it

Circular Dependency Warning in IntelliJ with Maven in a Java Web Project

Image
Circular Dependency Warning in IntelliJ with Maven in a Java Web Project : When working with a Java web project using Maven, you may encounter a circular dependency warning in IntelliJ. This can happen when a module depends on another module that also depends on the first module, creating a loop. The warning indicates that adding a new dependency may create a circular dependency and lead to a build failure. To solve this issue, you can try several solutions :  Split your project into smaller modules with clear responsibilities. This can help you identify and manage dependencies more effectively. For example, you can move the HrRestApiConnectionException class to a separate module and then include that module as a dependency in the modules that need it. Use Maven exclusions: Another way to solve circular dependency issues is to use Maven exclusions to remove unnecessary dependencies. You can specify the dependencies that you want to exclude from your module in the pom.xml file. For exa