How to Fix this code? It keeps closing the app for these reasons in logcat?

Error that I got is in here

It keeps on crashing or stopping everytime i run it. I do not have build errors but i checked my logcat for the reason and still cant resolve it. Send help please Error: See picture if needed to be clearer

2021-04-26 04:31:41.006 29414-29414/com.mobdeve.santosm.androidchallenge E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mobdeve.santosm.androidchallenge, PID: 29414
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobdeve.santosm.androidchallenge/com.mobdeve.santosm.androidchallenge.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7099)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:388)
        at android.widget.ListView.setAdapter(ListView.java:614)
        at com.mobdeve.santosm.androidchallenge.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:7327)
        at android.app.Activity.performCreate(Activity.java:7318)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7099) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 

This is my codes

Main activity:

import androidx.appcompat.app.AppCompatActivity;

   import android.content.Intent;
   import android.os.Bundle;
   import android.util.Log;
   import android.view.View;
   import android.widget.Button;
   import android.widget.ListView;

   import java.util.ArrayList;

   public class MainActivity extends AppCompatActivity {

    private Button newEmail;
    private ListView listView;
    private EmailAdapter emailAdapter;
    private ArrayList<Emails> emailsArrayList;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();


         emailAdapter = new EmailAdapter(this, emailsArrayList);
         listView.setAdapter(emailAdapter);


        newEmail.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SendActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onStart() {
         super.onStart();
         Log.d("MainActivity","onStart invoked");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("MainActivity","onResume invoked");
    }

    @Override
    protected void onPause() {

        super.onPause();
        Log.d("MainActivity","onPause invoked");
    }

    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onRestart() {

        super.onRestart();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    private void init(){
        newEmail = (Button) findViewById(R.id.newBtn);
        listView = (ListView) findViewById(R.id.list);
    }
}

Adapter:

package com.mobdeve.santosm.androidchallenge;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


import java.util.ArrayList;

public class EmailAdapter extends ArrayAdapter<Emails>{

    private Activity activity;

    public EmailAdapter(Activity activity, ArrayList<Emails> emailsArrayList){
        super(activity, R.layout.emails_row, emailsArrayList);
        this.activity = activity;
    }

    @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        View rowView = convertView;


        if (rowView == null){

            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.emails_row, null);


            ViewHolder emailsViewHolder = new ViewHolder();
            emailsViewHolder.rEmail= (TextView) rowView.findViewById(R.id.rEmail);
            emailsViewHolder.rBody= (TextView) rowView.findViewById(R.id.rBody);
            emailsViewHolder.rSubject= (TextView) rowView.findViewById(R.id.rSubject);

            rowView.setTag(emailsViewHolder);
        }

        final ViewHolder holder = (ViewHolder) rowView.getTag();
        Emails info = getItem(position);
        holder.rEmail.setText(info.getEmails());
        holder.rBody.setText(info.getBody());
        holder.rSubject.setText(info.getSubject());

        return rowView;
    }

    static class ViewHolder{
        public TextView rEmail;
        public TextView rBody;
        public TextView rSubject;
    }
}
1

There are 1 answers

0
OffRange On

Just add the following line into your init() method:

emailsArrayList = new ArrayList<>();

That should solve the NullPointerException