Program Crashing Null Pointer Exception


#1

I’m not sure what is causing this, thought I’d check in here. I even went back and pasted your example code in (since I couldn’t find any issues doing a side-by-side analysis) but still get the same issue. Stack trace and code below:

11-25 17:11:06.921: E/Trace(1029): error opening trace file: No such file or directory (2)
11-25 17:11:07.391: D/dalvikvm(1029): GC_CONCURRENT freed 79K, 7% free 2781K/2980K, paused 77ms+3ms, total 137ms
11-25 17:11:07.391: D/dalvikvm(1029): WAIT_FOR_CONCURRENT_GC blocked 14ms
11-25 17:11:07.451: D/dalvikvm(1029): GC_FOR_ALLOC freed 3K, 7% free 2875K/3080K, paused 23ms, total 24ms
11-25 17:11:07.461: I/dalvikvm-heap(1029): Grow heap (frag case) to 3.996MB for 1127536-byte allocation
11-25 17:11:07.601: D/dalvikvm(1029): GC_FOR_ALLOC freed <1K, 5% free 3976K/4184K, paused 139ms, total 139ms
11-25 17:11:07.731: D/dalvikvm(1029): GC_CONCURRENT freed <1K, 5% free 3980K/4184K, paused 4ms+93ms, total 132ms
11-25 17:11:07.873: D/libEGL(1029): loaded /system/lib/egl/libEGL_emulation.so
11-25 17:11:08.019: D/(1029): HostConnection::get() New Host Connection established 0x2a15ecf8, tid 1029
11-25 17:11:08.092: D/libEGL(1029): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-25 17:11:08.112: D/libEGL(1029): loaded /system/lib/egl/libGLESv2_emulation.so
11-25 17:11:08.171: W/EGL_emulation(1029): eglSurfaceAttrib not implemented
11-25 17:11:08.181: D/OpenGLRenderer(1029): Enabling debug mode 0
11-25 17:11:08.431: I/Choreographer(1029): Skipped 37 frames! The application may be doing too much work on its main thread.
11-25 17:11:08.691: D/dalvikvm(1029): GC_CONCURRENT freed 22K, 4% free 4363K/4504K, paused 85ms+4ms, total 175ms
11-25 17:11:27.140: D/AndroidRuntime(1029): Shutting down VM
11-25 17:11:27.150: W/dalvikvm(1029): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
11-25 17:11:27.190: E/AndroidRuntime(1029): FATAL EXCEPTION: main
11-25 17:11:27.190: E/AndroidRuntime(1029): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.criminalintent/com.bignerdranch.android.criminalintent.CrimeActivity}: java.lang.NullPointerException
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.os.Looper.loop(Looper.java:137)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-25 17:11:27.190: E/AndroidRuntime(1029): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 17:11:27.190: E/AndroidRuntime(1029): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 17:11:27.190: E/AndroidRuntime(1029): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-25 17:11:27.190: E/AndroidRuntime(1029): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-25 17:11:27.190: E/AndroidRuntime(1029): at dalvik.system.NativeStart.main(Native Method)
11-25 17:11:27.190: E/AndroidRuntime(1029): Caused by: java.lang.NullPointerException
11-25 17:11:27.190: E/AndroidRuntime(1029): at com.bignerdranch.android.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:49)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.Activity.performStart(Activity.java:5114)
11-25 17:11:27.190: E/AndroidRuntime(1029): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
11-25 17:11:27.190: E/AndroidRuntime(1029): … 11 more

[code]package com.bignerdranch.android.criminalintent;

import java.util.UUID;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class CrimeFragment extends Fragment {
public static final String EXTRA_CRIME_ID = “criminalintent.CRIME_ID”;

Crime mCrime;
EditText mTitleField;
Button mDateButton;
CheckBox mSolvedCheckBox;

public static CrimeFragment newInstance(UUID crimeId) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CRIME_ID, crimeId);

    CrimeFragment fragment = new CrimeFragment();
    fragment.setArguments(args);

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    UUID crimeId = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);
    mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_crime, parent, false);
    
    mTitleField = (EditText)v.findViewById(R.id.crime_title);
    mTitleField.setText(mCrime.getTitle());
    mTitleField.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence c, int start, int before, int count) {
            mCrime.setTitle(c.toString());
        }

        public void beforeTextChanged(CharSequence c, int start, int count, int after) {
            // this space intentionally left blank
        }

        public void afterTextChanged(Editable c) {
            // this one too
        }
    });
    
    mDateButton = (Button)v.findViewById(R.id.crime_date);
    mDateButton.setText(mCrime.getDate().toString());
    mDateButton.setEnabled(false);

    mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
    mSolvedCheckBox.setChecked(mCrime.isSolved());
    mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set the crime's solved property
            mCrime.setSolved(isChecked);
        }
    });       

    return v; 
}

}
[/code]


#2

I’m having the exact same issue. I’m using Android Studio 0.3.6

Here is my logcat:

11-30 12:34:52.638 2183-2183/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fiixed.criminalintent/com.fiixed.criminalintent.CrimeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5414)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.fiixed.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:50)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
at android.app.Activity.performStart(Activity.java:5379)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5414)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
11-30 12:36:39.458 4137-4137/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fiixed.criminalintent/com.fiixed.criminalintent.CrimeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5414)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.fiixed.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:69)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
at android.app.Activity.performStart(Activity.java:5379)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5414)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
11-30 12:40:10.318 6555-6555/com.fiixed.criminalintent E/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts


#3

Hi, did either of you ever find a solution to this? I’m getting the same problem.


#4

I’m have this problem to. :frowning: How you resolved this problem???


#5

I was inattentively read.
This problem was because i mixed CrimeActivity and CrimeListActivity. :blush:

Good book, i reed it in the russian language


#6

I didn’t understand the solution provided:

Where should I be looking for this mix-up?

Thanks for the help!


#7

I’m having the same problem as well. mCrimes is raising a null pointer exception. Has anyone been able to solve this???


#8

Has anyone found a solution yet? Mines crashing when I click on a crime :confused:


#9

OK found a solution.

Found the issue in my manifest file:

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".CrimeListActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".CrimeActivity" android:label="@string/app_name" > </activity> </application>

I was getting a warning for “.CrimeActivity” not being a concrete class. CrimeActivity.java looked like:

package ie.sampower.criminalintent.app;

import android.support.v4.app.Fragment;

public abstract class CrimeActivity extends SingleFragmentActivity {

   @Override
   protected Fragment createFragment(){
       return new CrimeFragment();
   }

}

so once I removed abstract from the class declaration it disappeared and it worked.


#10

Had the same problem. The problem is that the UUID comparison in the CrimeLab singleton, which is implicitly called in the onCreate method of the CrimeFragment is failing. For some reason the UUID comparison always returns false, even if the UUID’s match. This results in a null object being returned for the mCrime object.

If you replace the current comparison in the CrimeLab::GetCrime method, i.e. the line:

with

the UUIDs will match and a valid Crime object will be returned.

–Ken


#11

[quote=“khoremans”]Had the same problem. The problem is that the UUID comparison in the CrimeLab singleton, which is implicitly called in the onCreate method of the CrimeFragment is failing. For some reason the UUID comparison always returns false, even if the UUID’s match. This results in a null object being returned for the mCrime object.

If you replace the current comparison in the CrimeLab::GetCrime method, i.e. the line:

with

the UUIDs will match and a valid Crime object will be returned.

–Ken[/quote]

Ken,
Using compareTo(UUID) has sorted it out in my case.
Thank you for sharing your solution.
Peter


#12

Fixed my problem, thanks for sharing Ken :smiley: