I am a beginner in Android studio and I am trying to send an array list to a list view. I know that I am passing the correct info from my main activity to the second activity and that it is received properly in the second activity. My issue is coming in when I try to pass the array list "filteredlist" to the array adapter "arradapter" to then send to list view. It is giving me this error: java.lang.NullPointerException: "Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference." I am sure I am missing something, but I am not sure what. I am aware the code isn't perfect, and I plan on streamlining it later.
Second activity:
package com.example.charityfinder;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ResultsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
ArrayList<NationalCharity> filteredlist = (ArrayList<NationalCharity>) getIntent().getSerializableExtra("Filtered List");
Log.d("DEBUG", "Second Activity: " + filteredlist);//prints out list here
ListView dataresults = (ListView) findViewById(R.id.dataresults);
ArrayAdapter<NationalCharity> arradapter = new ArrayAdapter<NationalCharity>(this, R.layout.item_view, R.id.itemTextView, filteredlist);
dataresults.setAdapter(arradapter);
}
}
Main Activity:
package com.example.charityfinder;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//references to buttons and other controls
Button submitbtn;
RadioButton medicalRadio, envirRadio, hserviceRadio, educationRadio, publicaRadio,
cultureRadio, domesticvRadio, hrightsRadio, homelessRadio, religionRadio, youthRadio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readcharitydata();
medicalRadio = findViewById(R.id.medicalRadio);
envirRadio = findViewById(R.id.envirRadio);
hserviceRadio = findViewById(R.id.hserviceRadio);
educationRadio = findViewById(R.id.educationRadio);
publicaRadio = findViewById(R.id.publicaRadio);
cultureRadio = findViewById(R.id.cultureRadio);
domesticvRadio = findViewById(R.id.domesticvRadio);
hrightsRadio = findViewById(R.id.hrightsRadio);
homelessRadio = findViewById(R.id.homelessRadio);
religionRadio = findViewById(R.id.religionRadio);
youthRadio = findViewById(R.id.youthRadio);
submitbtn = findViewById(R.id.submitbtn);
submitbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openNewActivity();
}
});
}
String category;
public void onRadioButtonClicked(View view) {
if (medicalRadio.isChecked()) {
category = "Medical";
} else if (envirRadio.isChecked()) {
category = "Environmental_Animal";
} else if (hserviceRadio.isChecked()) {
category = "Human_Services";
} else if (educationRadio.isChecked()) {
category = "Education";
} else if (publicaRadio.isChecked()) {
category = "Public_Affairs";
} else if (cultureRadio.isChecked()) {
category = "Culture_Arts_Humanities";
} else if (domesticvRadio.isChecked()) {
category = "Domestic_Violence";
} else if (hrightsRadio.isChecked()) {
category = "Human_Rights";
} else if (homelessRadio.isChecked()) {
category = "Homelessness";
} else if (religionRadio.isChecked()) {
category = "Religious";
} else if (youthRadio.isChecked()) {
category = "Youth";
}
filterCharity(category);
}
private List<NationalCharity> charities = new ArrayList<>();
private void readcharitydata() {
InputStream is = getResources().openRawResource(R.raw.charities);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
try {
//ignore headers
reader.readLine();
while ((line = reader.readLine()) != null) {
//split by comma
String[] token = line.split(",");
//Read data
NationalCharity charity = new NationalCharity();
charity.setCharity_name(token[0]);
charity.setCategory(token[1]);
charity.setWeb_address(token[2]);
charity.setAddress(token[3]);
charity.setCity(token[4]);
charity.setState(token[5]);
charity.setZipcode(token[6]);
charity.setMission_statement(token[7]);
charities.add(charity);
}
} catch (IOException e) {
Log.wtf("Error", "Error reading data file" + line, e);
e.printStackTrace();
}
}
private ArrayList<NationalCharity> filtered = new ArrayList<>();
//needing to filter here from the charities list
private void filterCharity(String type) {
for (NationalCharity charity : charities) {
if (charity.getCategory().equals(type)) {
filtered.add(charity);
Log.d("DEBUG", "Just created: " + filtered);
}
}
}
public void openNewActivity() {
Intent intent = new Intent(MainActivity.this, ResultsActivity.class);
intent.putExtra("Filtered List", filtered);
Log.d("DEBUG", "Passing: " + filtered);
startActivity(intent);
}
}
setAdapteron something that should be aListViewbut it's actually null.findViewByIdreturns null if it can't find that view in the layout, so at a guess you don't have aListViewwith an ID ofdataresultsinsideactivity_results.xml, meaning yourdataresultsvariable is null