-1

First, I've been looking for the answer all over, but didn't find anything that can explain how to resolve my case.

I want to declare a few final fields in my Android FragmentActivity and initialize them later (I need onCreate to be executed first to get the needed data) I know that I can initialize final fields only in the constructor, however, it is called before onCreate when nothing else is yet initialized, therefore the application crashes with java.lang.NullPointerException.

My code:

public class MyActivity extends FragmentActivity {
    private final File mPhotosDir;
    private final ConstraintLayout mConstraintLayout;
    ...
    // This is the constructor of MyActivity activity
    public MyActivity() {
        this.mPhotosDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        this.mConstraintLayout = findViewById(R.id.myActivityConstraintLayout);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

Is it even possible to declare final fields in an activity and initialize them later? Shall I give up the final declaration?

DeadStar
  • 99
  • 1
  • 7
  • From where you are calling `MyActivity()` method – AskNilesh Dec 21 '18 at 12:46
  • I don't call it, this is the constructor which is being called just before onCreate – DeadStar Dec 21 '18 at 12:46
  • share your whole activity code also read this – AskNilesh Dec 21 '18 at 12:47
  • no need to add constructor in your activity read [Understand the Activity Lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle) – AskNilesh Dec 21 '18 at 12:49
  • shared, btw this is not a duplicate! There is no answer for cases with activity related data that is first initiated in onCreate – DeadStar Dec 21 '18 at 12:50
  • Its duplicate your `Context` is null in `MyActivity()` constructor – AskNilesh Dec 21 '18 at 12:50
  • Read the question! Of course it is null before the onCreate is called, but how can I resolve it! – DeadStar Dec 21 '18 at 12:52
  • Please Read [Understand the Activity Lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle) it will help u – AskNilesh Dec 21 '18 at 12:53
  • I understand the Activity lifecycle very well, you seem to not understand the problem and shooting in some random general direction. Read the first answer given below, which answers my question. Don't just spam if you don't know – DeadStar Dec 21 '18 at 12:55
  • @NileshRathod, you comment "no need to add constructor in your activity" shows you have no understanding of final field! Go read what final means. – DeadStar Dec 21 '18 at 13:00

1 Answers1

2

Is it even possible to declare final fields in an activity and initialize them later?

Not in Java, at least not directly. final fields need to be initialized directly (when the field is defined) or in a constructor. You cannot wait to define them until some later point in time.

Kotlin supports this sort of thing via lateinit var properties.

The closest thing that you can do in Java is to have the final field hold something that in turn holds the value that you need. For example, you can have a final field for an AtomicReference, and only set() the reference in onCreate(). However, going this approach just to have a final field is a code smell.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491