1

I am currently stuck with the following errors from Logcat. I've looked at over ten other posts similar to this problem and could not find solutions to my problem.

03-22 17:55:58.777: E/Trace(1172): error opening trace file: No such file or directory (2)
03-22 17:55:58.947: E/AndroidRuntime(1172): FATAL EXCEPTION: main
03-22 17:55:58.947: E/AndroidRuntime(1172): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.schoolwork.capstoneproject/com.schoolwork.capstoneproject.StartingPoint}: java.lang.NullPointerException
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:137)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invoke(Method.java:511)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native Method)
03-22 17:55:58.947: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.schoolwork.capstoneproject.StartingPoint.<init>(StartingPoint.java:11)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.Class.newInstanceImpl(Native Method)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at java.lang.Class.newInstance(Class.java:1319)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-22 17:55:58.947: E/AndroidRuntime(1172):     ... 11 more

Here is my java code:

package com.schoolwork.capstoneproject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class StartingPoint extends Activity{

    SharedPreferences sharedPreferences = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.starting_point);

        Thread verifyStatus = new Thread(){
            public void run(){
                try{
                    sleep(2000);
                }catch(Exception e){
                    e.printStackTrace();
                    Log.e("Thread Error", e.toString());
                }finally{
                    if(sharedPreferences.getBoolean("isLoggedIn", false)){
                        Intent openMainMenu = new Intent(getApplicationContext(), MainMenu.class);
                        openMainMenu.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(openMainMenu);
                    }else{
                        Intent openLoginScreen = new Intent(getApplicationContext(), LoginScreen.class);
                        openLoginScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(openLoginScreen);
                    }
                } //end of try sequence
            }
        }; //end of thread
        verifyStatus.start();
    } //end of onCreate

}

I'm totally stuck, I can't find the problem and here is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.schoolwork.capstoneproject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/prog_logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".StartingPoint"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".LoginScreen"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name=".MainMenu"
            android:label="@string/app_name" >
        </activity>

    </application> 

</manifest>

Thanks for your help.

3 Answers 3

6

The logcat is clear.

03-22 17:55:58.947: E/AndroidRuntime(1172): Caused by: java.lang.NullPointerException
03-22 17:55:58.947: E/AndroidRuntime(1172):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
03-22 17:55:58.947: E/AndroidRuntime(1172):     at com.schoolwork.capstoneproject.StartingPoint.<init>(StartingPoint.java:11)

You are trying to execute code outside a method. This will not work:

SharedPreferences sharedPreferences = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);

Instead:

SharedPreferences sharedPreferences;

and in onCreate()

sharedPreferences = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Simon, thanks for your quick response. Your answer does point out another problem which I fixed as per your recommendation but my main problem with Android java.lang.RuntimeException is still there.
Actually, I take that back, I made a typo in the correction. It seems to be working now. Thank you Simon, you're awesome.
Glad it worked. Please accept the answer by clicking the tick.
yep, will do, i tried that right away but a message popped and said i could only select an answer after 4 min, i'll try again after that time and thanks again for your help Simon
1

Try to call the getSharedPreferences inside the onCreate to make sure that your Context exists. Not sure whether this is the reason for your exception, but it's worth a try.

1 Comment

Thanks fish, after doing what Simon recommended it seems to be working now. Thank you for your reply
1

Have you tried to put getSharedPreferences(...) into the onCreate()?

public class StartingPoint extends Activity{

    SharedPreferences sharedPreferences = null; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.starting_point);

        sharedPreferences; = getSharedPreferences("capstone_preferences", Activity.MODE_PRIVATE);

That way it should work ... Cheers!

2 Comments

Thanks Andre, I did what Simon recommended and it is working now. I appreciate your reply.
Yes, that's perfect and actually the same :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.