Home > Enterprise >  Why do I get a null object reference on my RealtiveLayout.setBackgroundColor although I have initial
Why do I get a null object reference on my RealtiveLayout.setBackgroundColor although I have initial

Time:10-30

Here is my java code:

package com.example.project_final;

import static android.content.ContentValues.TAG;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;

import java.util.ArrayList;

public class history extends AppCompatActivity {
    FirebaseFirestore Fstore;
    String userID;
    FirebaseAuth fAuth ;
    int times;
    int heart;
    int avg_heart;
    String date;
    int count = 0;
    TextView Data;
    RelativeLayout [] L = new RelativeLayout[7];
    TextView [] hr = new TextView[7];
    TextView [] dates = new TextView[7];
    ImageView [] img = new ImageView[7];
    TextView last;
    ArrayList<Integer> intheart = new ArrayList<>();
    ArrayList<String> intdates = new ArrayList<>();
    TextView average;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        Fstore = FirebaseFirestore.getInstance();
        fAuth = FirebaseAuth.getInstance();
        userID= fAuth.getUid();
        DocumentReference documentReference = Fstore.collection("users").document(userID);
        Data = findViewById(R.id.txtNodata);
        Intent intent1 = getIntent();
        String signup = intent1.getStringExtra("signup");
            last = findViewById(R.id.txtlast);
            average = findViewById(R.id.txtaverage);
            for(int i = 0; i<=6 ;i  ) {
                int resourceId = this.getResources().getIdentifier("layout"  i 1, "id", this.getPackageName());
                L[i] = findViewById(resourceId);
            }
            for(int i = 0; i<=6 ;i  ) {
                int resourceId = this.getResources().getIdentifier("heartrate"  i 1, "id", this.getPackageName());
                hr[i] = findViewById(resourceId);
            }
            for(int i = 0; i<=6 ;i  ) {
                int resourceId = this.getResources().getIdentifier("txtdate"  i 1, "id", this.getPackageName());
                dates[i] = findViewById(resourceId);
            }
            for(int i = 0; i<=6 ;i  ) {
                int resourceId = this.getResources().getIdentifier("img"  i 1, "id", this.getPackageName());
                img[i] = findViewById(resourceId);
            }
            documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    times = Math.toIntExact(task.getResult().getLong("times"));
                    avg_heart = Math.toIntExact(task.getResult().getLong("average_heart_rate"));
                    for (int i = 0 ; i <times ;i  ){
                        intdates.add(task.getResult().getString("date"   i));
                        intheart.add(Math.toIntExact(task.getResult().getLong("heart" i)));
                    }
                }
            });
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(!(intheart.isEmpty()||intdates.isEmpty())){
                        int count =0;
                        if(times>7){
                            times=7;
                        }
                        last.setText("Last "  times  " Days history");
                        for(int i =0 ; i<intdates.size();i  ){
                            if(1.2*avg_heart<intheart.get(i)||0.8*avg_heart<intheart.get(i)){
                                L[i].setBackgroundColor(R.drawable.color_gradient1);
                                img[i].setImageResource(R.drawable.sad);
                            }
                            else{
                                L[i].setBackgroundColor(R.drawable.gradient_color);
                                img[i].setImageResource(R.drawable.happy);
                            }
                            dates[i].setText(intdates.get(i));
                            hr[i].setText(intheart.get(i) " Bpm");
                            L[i].setVisibility(View.VISIBLE);
                        }
                    }
                    else{
                        handler.postDelayed(this, 100);

                    }

                }
            }, 100);



        
    }
}

Here is my XML code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout 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"
    android:padding="20dp"
    android:orientation="vertical"
    tools:context=".history">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@ id/txtlast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text=""
        android:textSize="30sp"
        android:layout_gravity="center"
        android:textColor="#000"
        android:textStyle="bold"
        />
    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text=""
        android:id="@ id/txtaverage"
        android:textColor="#000"
        android:layout_gravity="center"
        />
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:cardCornerRadius="20dp"
            android:layout_marginTop="20dp">
            <RelativeLayout
                android:id="@ id/layout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/gradient_color"
                android:padding="10dp"
                android:visibility="invisible"
                >

                <TextView
                    android:id="@ id/txtday1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Day 1"
                    android:textColor="#ffff"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@ id/txtdate1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtday1"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtdate1"
                    android:id="@ id/heartrate1"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff"
                    />

                <ImageView
                    android:id="@ id/img1"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:layout_marginLeft="220dp"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:cardCornerRadius="20dp"
            android:layout_marginTop="20dp">
            <RelativeLayout
                android:id="@ id/layout2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="invisible"
                android:background="@drawable/color_gradient1"
                android:padding="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Day 2"
                    android:textColor="#ffff"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    android:id="@ id/txtday2"
                    />

                <TextView
                    android:id="@ id/txtdate2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtday2"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff" />

                <TextView
                    android:id="@ id/heartrate2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtdate2"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff" />

                <ImageView
                    android:id="@ id/img2"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:layout_marginLeft="220dp"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:cardCornerRadius="20dp"
            android:layout_marginTop="20dp">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/gradient_color"
                android:id="@ id/layout3"
                android:visibility="invisible"
                android:padding="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Day 3"
                    android:textColor="#ffff"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    android:id="@ id/txtday3"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtday3"
                    android:textColor="#ffff"
                    android:layout_marginTop="10dp"
                    android:id="@ id/txtdate3"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtdate3"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff"
                    android:id="@ id/heartrate3"
                    />

                <ImageView
                    android:id="@ id/img3"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:layout_marginLeft="220dp"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:cardCornerRadius="20dp"
            android:layout_marginTop="20dp">
            <RelativeLayout
                android:id="@ id/layout4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/color_gradient1"
                android:visibility="invisible"
                android:padding="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Day 4"
                    android:textColor="#ffff"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    android:id="@ id/txtday4"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtday4"
                    android:textColor="#ffff"
                    android:layout_marginTop="10dp"
                    android:id="@ id/txtdate4"
                    />

                <TextView
                    android:id="@ id/heartrate4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtdate4"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff" />
                <ImageView
                    android:id="@ id/img4"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:layout_marginLeft="220dp"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:cardCornerRadius="20dp"
            android:layout_marginTop="20dp">
            <RelativeLayout
                android:id="@ id/layout5"
                android:visibility="invisible"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/gradient_color"
                android:padding="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Day 1"
                    android:textColor="#ffff"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    android:id="@ id/txtday5"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtday5"
                    android:textColor="#ffff"
                    android:layout_marginTop="10dp"
                    android:id="@ id/txtdate5"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtdate5"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff"
                    android:id="@ id/heartrate5"
                    />
                <ImageView
                    android:id="@ id/img5"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:layout_marginLeft="220dp"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:cardCornerRadius="20dp"
            android:layout_marginTop="20dp">
            <RelativeLayout
                android:visibility="invisible"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/gradient_color"
                android:id="@ id/layout6"
                android:padding="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Day 6"
                    android:textColor="#ffff"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    android:id="@ id/txtday6"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtday6"
                    android:textColor="#ffff"
                    android:layout_marginTop="10dp"
                    android:id="@ id/txtdate6"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtdate6"
                    android:id="@ id/heartrate6"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff"
                    />

                <ImageView
                    android:id="@ id/img6"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:layout_marginLeft="220dp"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:cardCornerRadius="20dp"
            android:layout_marginTop="20dp">
            <RelativeLayout
                android:visibility="invisible"
                android:id="@ id/layout7"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/gradient_color"
                android:padding="10dp"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Day 7"
                    android:textColor="#ffff"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    android:id="@ id/txtday7"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtday7"
                    android:textColor="#ffff"
                    android:layout_marginTop="10dp"
                    android:id="@ id/txtdate7"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/txtdate7"
                    android:layout_marginTop="10dp"
                    android:textColor="#ffff"
                    android:id="@ id/heartrate7"
                    />

                <ImageView
                    android:id="@ id/img7"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:layout_marginLeft="220dp"
                    />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>


    </LinearLayout>
    <TextView
        android:id="@ id/txtNodata"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:fontFamily="@font/lovelo"
        android:gravity="center"
        android:text="No data Available"
        android:textColor="@color/colorRed"
        android:textSize="20sp"
        android:visibility="invisible" />

</LinearLayout>
</ScrollView>

When I run the code I get this error on a null object reference when I try to setbackground color inside the handler:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)'

The code should run fine since I initialized all the views. I've been stuck for a while I don't know where is the problem help.

CodePudding user response:

I think you are not getting the name correct, so it's not initializing it, name must be a string as a whole :

            for(int i = 0; i<=6 ;i  ) {
                int resourceId = this.getResources().getIdentifier("layout"   (i 1).toString(), "id", this.getPackageName());
                L[i] = findViewById(resourceId);
            }
  • Related