3

I have a class that holds another objects inside it (List, Set and objects from my application).

public class SomeClass {
    private List l;
    private SomeObject obj;
    //...
}

Is a good practice instantiate these objects where the SomeClass object is created to avoid NullPointerException? Something like:

public class SomeClass{
    private List l = new ArrayList();
    private SomeObject obj = new SomeObject();
    //...
}

In a normal way, these objects will be generated in some processing/analysis, but errors can occur and the objects still with null value.

6 Answers 6

2

Yes, it is a good practice to do so. The constructor is a natural place to instantiate member objects. You can also create them right where they are declared:

private List l = new ArrayList();

However, it might be a good idea to restructure or modify your code so that NullPointerExceptions won't occur, regardless of the order in which the methods are called.

Sign up to request clarification or add additional context in comments.

1 Comment

All answers are good, but this was chosen because says to modify the code to avoid NullPointerException, and I fully agree with this.
1

If an empty List or a default object of that class is a valid state in every following operation, yes, instantiate a default. However, if the default would be an invalid state, don't do it.

Comments

1

Well I prefer to initialize because sometimes is a headache to find where's the null pointer exception, and if u initialize your objects with constructors the object inside should be initialized.

Hope this help you.

Comments

1

It's generally good practice to instantiate member fields (whether objects or primitives) at creation time whenever the default value (0, false, or null) is not what you want. One time to defer this is for lazy instantiation. (This is used, for instance, when an object might not be needed after all and creating it is expensive.) Another time to defer this is when other initialization needs to take place beforehand.

Assuming you want to initialize a field at object creation time, there are two ways to do it: with an initializer expression as you showed or in the constructor(s). There isn't too much difference, other than that instance initializers run before the first line of the constructor. This may or may not cause problems, depending on your code logic.

It's also a good idea to declare member fields final whenever they are initialized at object creation and are expected to not change during the life of the object. A side benefit of declaring a field final is that the compiler will catch any failure to initialize it. (The compiler requires definite assignment to consider a final field to be properly initialized.)

Comments

1

You are talking about eager construction versus lazy construction. There are places where each has value.

In many situations, it is better to lazily create things as it saves memory. But then you must check for null every time you try to get data

In some situations, it makes sense to create the object upfront, either to avoid the null checking mentioned above or to avoid the object creation time hit during an intensive process

Comments

1

It is normal to generate them like this, but it's not a nice way to code to generate them just to avoid NPE. There should be proper validation in code, rather than assigning garbage-eligible objects which won't be utilized.


You can also assign some default states - like Collections.emptyList(), or in a constants class:

DEFAULT_STATE = new SomeState();

then simply

class A { 
      State obj = Constants.DEFAULT_STATE;
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.