I have an array of objects displayed in a list view inside the MainActivity, and to add a filter to the list I added a floating button in the MainActivity that shows a dialog fragment.
Edit I know it's a NullPointerException error and it's coming from the edittext, I want to know what's wrong with to code that made it so.
The dialog shows 2 edit text fields with default positive and negative button.
I want to call a custom method when the positive button is pressed that retrieve the text entered in the edit text and then filter the list based on that.
the problem is I get
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
The code
MainActivity
public class MainActivity extends AppCompatActivity {
private ListView listView;
public CustomAdaptar adaptar;
protected static ArrayList<Trip> trips = new ArrayList<>();
protected static ArrayList<Trip> allTrips = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
adaptar = new CustomAdaptar();
// A lot of code for setting the list and other funcutionality
FloatingActionButton fab_filter = (FloatingActionButton) findViewById(R.id.fab_filter);
fab_filter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFrag filter = new DialogFrag();
filter.show(getFragmentManager(),"Filter");
}
} }
DialogFragment Code
public class DialogFrag extends DialogFragment{
LayoutInflater inflater;
EditText min;
EditText max;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.filter_popup, null));
builder.setMessage("Filter")
.setPositiveButton("Filter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
customFilter();
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
min = (EditText) getView().findViewById(R.id.et_filterMinPrice);
max = (EditText) getView().findViewById(R.id.et_filterMaxPrice);
return builder.create();
}
public void customFilter() {
MainActivity.trips.clear();
String et_minPrice = min.getText().toString();
String et_maxPrice = max.getText().toString();
int minPrice = Integer.parseInt(et_minPrice);
int maxPrice = Integer.parseInt(et_maxPrice);
if ( et_minPrice.length()>0 && et_maxPrice.length()==0) {
//Do the filtering
}
edit stacktrace
FATAL EXCEPTION: main
Process: com.transcendedapps.customlistandjson, PID: 1071
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.transcendedapps.customlistandjson.DialogFrag.customFilter(DialogFrag.java:49)
at com.transcendedapps.customlistandjson.DialogFrag$2.onClick(DialogFrag.java:29)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)