I have error
Process: id.ac.astra.polytechnic.toolmaintenance, PID: 22120
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at id.ac.astra.polytechnic.toolmaintenance.viewModel.RoleListViewModel.addRole(RoleListViewModel.java:53)
at id.ac.astra.polytechnic.toolmaintenance.AddRoleFragment$2.onClick(AddRoleFragment.java:79)
at android.view.View.performClick(View.java:7756)
at android.view.View.performClickInternal(View.java:7729)
at android.view.View.access$3700(View.java:860)
at android.view.View$PerformClick.run(View.java:29318)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8298)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073)
And this is my code:
AddRoleFragment.java
public class AddRoleFragment extends Fragment {
private Role mRole;
private EditText roleName;
private RoleListViewModel mRoleListViewModel;
public static AddRoleFragment newInstance() {
return new AddRoleFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.i(TAG, "UserFragment.onCreate() called");
mRole = new Role();
mRoleListViewModel = new ViewModelProvider(this)
.get(RoleListViewModel.class);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_role, container, false);
Button btnSimpan = view.findViewById(R.id.btnSimpan);
roleName = view.findViewById(R.id.inputNamaJabatan);
roleName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int i, int i1, int i2) {
mRole.setRolename(s.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
btnSimpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRoleListViewModel.addRole(mRole);
Fragment peminjamanFragment = ListKelolaRoleFragment.newInstance();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, peminjamanFragment, "peminjaman");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
RoleListViewModel.java
public class RoleListViewModel extends ViewModel {
private MutableLiveData<List<Role>> mRoleListMutableLiveData;
private RoleRepository mRoleRepository;
public RoleListViewModel(){
Log.d(TAG, "RoleListViewModel constructor called");
mRoleRepository = RoleRepository.get();
}
public MutableLiveData<List<Role>> Roles(){
mRoleListMutableLiveData = mRoleRepository.Roles();
Log.d(TAG,"RoleListViewModel.getRoles() called ="+
mRoleListMutableLiveData.toString());
return mRoleListMutableLiveData;
}
public void addRole (Role Role) {
mRoleRepository.addRole (Role);
Log.d(TAG,"RoleListViewModel.getRoles() called ="+
mRoleListMutableLiveData.toString());
}
}
RoleRepository
public class RoleRepository {
private static final String TAG = "RoleRepository";
public static RoleRepository INSTANCE;
private RoleService mRoleService;
private MutableLiveData<List<Role>> mRoleListMutableLiveData;
private RoleRepository(Context context) {
mRoleService = ApiUtils.getRoleService();
}
public static void initialize(Context context) {
if (INSTANCE == null) {
INSTANCE = new RoleRepository(context);
}
}
private RoleRepository(){
}
public static RoleRepository get() {
return INSTANCE;
}
public MutableLiveData<List<Role>> Roles() {
MutableLiveData<List<Role>> Roles = new MutableLiveData<List<Role>>();
Call<List<Role>> call = mRoleService.Roles();
call.enqueue(new Callback<List<Role>>() {
@Override
public void onResponse(Call<List<Role>> call, Response<List<Role>> response) {
if (response.isSuccessful()) {
Roles.setValue(response.body());
Log.d(TAG, "getRoles.onResponse() called");
}
}
@Override
public void onFailure(Call<List<Role>> call, Throwable t) {
Log.e("Error API call :", t.getMessage());
}
});
return Roles;
}
public void addRole(Role Role){
Log.i(TAG, "addRole() called");
Call <Role> call = mRoleService.addRole (Role);
Log.i(TAG, "addRole2() called");
call.enqueue(new Callback<Role>() {
@Override
public void onResponse (Call<Role> call, Response <Role> response) { if (response.isSuccessful()){
Log.i(TAG, "Role added " + Role.getRolename());
}
}
@Override
public void onFailure (Call<Role> call, Throwable t) {
Log.e("Error API call ", t.getMessage());
}
});
}
}
What I have tried:
Please help me to solve this error