IllegalArgumentException or NullPointerException for a null parameter? [closed]

You should be using IllegalArgumentException (IAE), not NullPointerException (NPE) for the following reasons: First, the NPE JavaDoc explicitly lists the cases where NPE is appropriate. Notice that all of them are thrown by the runtime when null is used inappropriately. In contrast, the IAE JavaDoc couldn’t be more clear: “Thrown to indicate that a method … Read more

Why is my Spring @Autowired field null?

The field annotated @Autowired is null because Spring doesn’t know about the copy of MileageFeeCalculator that you created with new and didn’t know to autowire it. The Spring Inversion of Control (IoC) container has three main logical components: a registry (called the ApplicationContext) of components (beans) that are available to be used by the application, … Read more

Is null check needed before calling instanceof?

No, a null check is not needed before using instanceof. The expression x instanceof SomeClass is false if x is null. The Java 11 Language Specification expresses this concisely in section 15.20.2, “Type comparison operator instanceof”. (Java 17 expresses this less concisely, after the introduction of instanceof patternmatching.) “At run time, the result of the … Read more

tech