Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.constraintlayout.widget.ConstraintLayout.findViewById(int)' on a null object reference
It says that this line causes it:
LinearLayout all_sets = main.findViewById(R.id.all_sets);
The Java code (MainActivity.java
):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@SuppressLint("ResourceType") ConstraintLayout main = findViewById(R.layout.activity_main);
LinearLayout all_sets = main.findViewById(R.id.all_sets); //causes the error when the code is as-is
LinearLayout set1 = all_sets.findViewById(R.id.set1); //causes the error without the '@Suppress..._main);' and the 'main.' in '...all_sets = main.find...'
LinearLayout set2 = all_sets.findViewById(R.id.set2);
LinearLayout set3 = all_sets.findViewById(R.id.set3);
ImageView triangle1 = set1.findViewById(R.id.triangle1);
ImageView triangle2 = set1.findViewById(R.id.triangle2);
ImageView cloud = set2.findViewById(R.id.cloud);
ImageView hexagon = set2.findViewById(R.id.hexagon);
triangle1.setVisibility(View.INVISIBLE);
triangle2.setVisibility(View.INVISIBLE);
cloud.setVisibility(View.INVISIBLE);
hexagon.setVisibility(View.INVISIBLE);
setContentView(R.layout.activity_main);
The XML (activity_main.xml
):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/all_sets"
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/set1"
android:layout_width="match_parent"
android:layout_height="243dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/triangle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/triangle1" />
<ImageView
android:id="@+id/triangle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/triangle2" />
</LinearLayout>
<LinearLayout
android:id="@+id/set2"
android:layout_width="match_parent"
android:layout_height="243dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/cloud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/cloud" />
<ImageView
android:id="@+id/hexagon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/hexagon" />
</LinearLayout>
<LinearLayout
android:id="@+id/set3"
android:layout_width="match_parent"
android:layout_height="243dp"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I also tried without specifying which View to find in:
LinearLayout all_sets = findViewById(R.id.all_sets);
LinearLayout set1 = all_sets.findViewById(R.id.set1); //causes the error in this case
LinearLayout set2 = all_sets.findViewById(R.id.set2);
LinearLayout set3 = all_sets.findViewById(R.id.set3);
I don't see any "it's because view1
is not directly in view2
but in view3
which is in view2
".
What is the problem? Why does this give an NPE (Null Pointer Exception)?
I want to show the layout only after making the ImageView
s invisible programmatically.