In my app fragment whenever I press the button, my app crashes. I have an edit text in which when the user will enter the text will go to firebase database after he/she presses the save button, but whenever I press the button my app crashes.
Here is the error message in logical window :
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.MechaProjectS6.whatisinmyfridge.fragment.NoteFragment$1.onClick(NoteFragment.java:74)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Here is the code :
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.MechaProjectS6.whatisinmyfridge.R;
import com.MechaProjectS6.whatisinmyfridge.model.ModelNotes;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.MechaProjectS6.whatisinmyfridge.model.model;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class NoteFragment extends Fragment {
EditText e1;
Button b1;
DatabaseReference databaseReference;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public NoteFragment() {
// Required empty public constructor
}
public static NoteFragment newInstance(String param1, String param2) {
NoteFragment fragment = new NoteFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes, container, false);
e1 = view.findViewById(R.id.add_notes);
b1 = (Button) view.findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String notes = e1.getText().toString().trim();
if (!notes.isEmpty()) {
databaseReference = FirebaseDatabase.getInstance().getReference().child("notes");
ModelNotes Notes = new ModelNotes(notes);
databaseReference.setValue(Notes);
Toast.makeText(getContext(), "Saved Successfully", Toast.LENGTH_LONG).show();
e1.setText("");
} else {
Toast.makeText(getContext(), "Please Enter Notes", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
Can anyone suggest the solution, I have tried finding possible solutions on the web but not able to solve the problem ?
Thanks in advance