0

When I click on attendance record and fill the details it keeps crashing app Crashes admin_attendanceSheet.xml ---------------------- android:layout_width="wrap_content" android:layout_height="wrap_content">

    <include
        android:id="@+id/ftoolbar"
        layout="@layout/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="8dp"
        />

    <Spinner
        android:id="@+id/spinner5"
        android:layout_width="368dp"
        android:layout_height="30dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:entries="@array/classes"
        android:layout_margin="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="8dp" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="10dp"
    android:weightSum="4">
    <TextView
        android:fontFamily="@font/geometry"
        android:layout_marginLeft="90dp"
        android:id="@+id/mydate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select your date"
        android:textSize="25sp"
        android:layout_marginTop="10dp"/>


</LinearLayout>
    <RelativeLayout

        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <CalendarView
            android:id="@+id/calenderView"
            android:background="@drawable/shape"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/button12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="viewlist"
            android:text="Submit"
            android:background="@drawable/button"
            android:layout_below="@id/calenderView"
            android:layout_marginTop="40dp"
            android:layout_centerHorizontal="true"/>
    </RelativeLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.893"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="101dp" />

</LinearLayout>
</ScrollView>`

admin_attendanceSheet.java

package com.nanosh.myapplication;

import android.graphics.Color;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.nanosh.myapplication.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class admin_attendanceSheet extends AppCompatActivity {

    ListView listView;
    CalendarView calenderView;
    TextView myDate;
    Spinner class_name;
    String classes;
    //EditText date;
    ArrayList Userlist = new ArrayList<>();
    ArrayList Studentlist = new ArrayList<>();

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    DatabaseReference dbAttendance;
    DatabaseReference dbStudent;
    String required_date;
    Toolbar mToolbar;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_attendance_sheet);
        mToolbar=(Toolbar)findViewById(R.id.ftoolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("Attendance Records");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        listView = (ListView) findViewById(R.id.list);
        class_name = (Spinner) findViewById(R.id.spinner5);

        calenderView=(CalendarView)findViewById(R.id.calenderView);
        myDate=(TextView)findViewById(R.id.mydate);

        classes = class_name.getSelectedItem().toString();
        calenderView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                String date=(dayOfMonth+1)+"/"+month+"/"+year;
                myDate.setText(date);

            }
        });

    }

    public void display_list(final ArrayList userlist) {
        Studentlist.clear();
      required_date = myDate.getText().toString();
        dbAttendance = ref.child("attendance");
        dbAttendance.child(required_date).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Result will be holded Here

                Studentlist.add("SID    "+"   CGA    "+"    SPM    "+"    DS     "+"    EJ     "+ "   LSA");
                for (Object sid : userlist) {

                    //DataSnapshot dsp=dataSnapshot.child(sid.toString());
                    String CGA=dataSnapshot.child(sid.toString()).child("CGA").getValue().toString().substring(0,1);
                    String SPM=dataSnapshot.child(sid.toString()).child("SPM").getValue().toString().substring(0,1);
                    String DS=dataSnapshot.child(sid.toString()).child("DS").getValue().toString().substring(0,1);
                    String EJ=dataSnapshot.child(sid.toString()).child("EJ").getValue().toString().substring(0,1);
                    String LSA=dataSnapshot.child(sid.toString()).child("LSA").getValue().toString().substring(0,1);

                    Studentlist.add(dataSnapshot.child(sid.toString()).getKey().toString()+"    "+CGA+"     "+SPM+"     "+DS+"     "+EJ+"      "+LSA); //add result into array list
                }
                //Toast.makeText(getApplicationContext(),Studentlist.toString(), Toast.LENGTH_LONG).show();
                list(Studentlist);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
            }

        });




    }




    public void viewlist(View v){

        Userlist.clear();
        dbStudent = ref.child("Student");
        dbStudent.orderByChild("classes").equalTo(class_name.getSelectedItem().toString()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Result will be holded Here
                for (DataSnapshot dsp : dataSnapshot.getChildren()) {
                    Userlist.add(dsp.child("sid").getValue().toString()); //add result into array list
                }
                display_list(Userlist);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(), "something went wrong", Toast.LENGTH_LONG).show();
            }

        });




    }
public void list(ArrayList studentlist){
    //int color = Color.argb(255, 255, 175, 64);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, studentlist);
    // Assign adapter to ListView
    listView.setAdapter(adapter);


}


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


}

I cant figure out what might the problem or format of the code is wrong ?

  • 1
    The exception message says that you are calling `toString()` on a `null` reference. I can see a few places where that might be happening. If you read the stacktrace, it will tell you which statement is causing the exception to be thrown. From that you can work back to the source of the `null` and figure out what to do about it. – Stephen C Mar 21 '19 at 13:14
  • For more information, read the linked Q&A – Stephen C Mar 21 '19 at 13:15

0 Answers0