I have been following the book very carefully and have been working with Android Studio 3.1.3
I have not been able to solve this NullPointerException error by any means so I am in need of urgent help.
On running the app it shows a java.lang.NullPointerException in the getCrimes() method of the CrimeLab class!!!
I have provided the complete error description here :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.owner.criminalintent, PID: 7691
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.owner.criminalintent/com.example.owner.criminalintent.CrimeListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.owner.criminalintent.CrimeLab.getCrimes()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.owner.criminalintent.CrimeLab.getCrimes()' on a null object reference
at com.example.owner.criminalintent.CrimeListFragment.updateUI(CrimeListFragment.java:35)
at com.example.owner.criminalintent.CrimeListFragment.onCreateView(CrimeListFragment.java:27)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2425)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2623)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2410)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2365)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2272)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3271)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3227)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:610)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
at android.app.Activity.performStart(Activity.java:6722)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
I am also providing my complete CrimeListFragment.java file and CrimeLab.java file here as these are the files that seem to be associated with the error,
CrimeListFragment.java :-
package com.example.owner.criminalintent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class CrimeListFragment extends Fragment {
private RecyclerView mCrimeRecyclerView;
//private CrimeAdapter mAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mCrimeRecyclerView = view.findViewById(R.id.crime_recycler_view);
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private void updateUI() {
CrimeAdapter mAdapter;
CrimeLab crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes = crimeLab.getCrimes();
mAdapter = new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(mAdapter);
}
private class CrimeHolder extends RecyclerView.ViewHolder{
private CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_crime, parent, false));
}
}
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
private List<Crime> mCrimes;
private CrimeAdapter(List<Crime> crimes) {
mCrimes = crimes;
}
@NonNull
@Override
public CrimeHolder onCreateViewHolder(@Nullable ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new CrimeHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(@Nullable CrimeHolder holder, int position) {
}
@Override
public int getItemCount() {
return mCrimes.size();
}
}
}
and CrimeLab.java :-
package com.example.owner.criminalintent;
import android.content.Context;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class CrimeLab{
private static CrimeLab sCrimeLab;
private List<Crime> mCrimes;
@Nullable
public static CrimeLab get(Context context){
if (sCrimeLab == null)
sCrimeLab = new CrimeLab(context);
return null;
}
private CrimeLab(Context context){
mCrimes = new ArrayList<>();
for(int i=0;i<100;i++){
Crime crime = new Crime();
crime.setTitle("Crime #" + i);
crime.setSolved(i%2 == 0);
mCrimes.add(crime);
}
}
public List<Crime> getCrimes(){
return mCrimes;
}
public Crime getCrime(UUID id){
for (Crime crime : mCrimes){
if(crime.getId().equals(id))
return crime;
}
return null;
}
}
Any kind of help from anyone will be really very nice cause I haven’t been able to continue with android development anymore!!!
Please help