0

I'm trying to create an intercom app, but when I try to run it, it keeps giving me an error which is getContext() is null.
Can anyone help me with this?

public class intercomApp extends Application {

protected static final String SHARED_PREFS_REFERENCE_LABEL = "ComeNChat";

protected static final String DEFAULT_NAME = "Default_Name";

private static intercomApp instance;

public static intercomApp getInstance() {
    return instance;
}

public static Context getContext() {
    return instance;
}

@Override
public void onCreate() {
    instance = this;
    super.onCreate();
}

// store our name to shared prefs
public static void storeName(String name) {
    PreferenceManager.getDefaultSharedPreferences(getContext()).edit().putString(SHARED_PREFS_REFERENCE_LABEL, name).commit();
}

// get our name from shared prefs
public static String getName() {
    return PreferenceManager.getDefaultSharedPreferences(getContext()).getString(SHARED_PREFS_REFERENCE_LABEL, DEFAULT_NAME); //(intercomApp.java:45)
}

This is my logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ricky.comenchat/com.example.ricky.comenchat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5466)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                             at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
                                                                             at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
                                                                             at com.example.ricky.comenchat.util.intercomApp.getName(intercomApp.java:45)
                                                                             at com.example.ricky.comenchat.MainActivity.getClientNameList(MainActivity.java:383)
                                                                             at com.example.ricky.comenchat.MainActivity.setupClientListAdapter(MainActivity.java:214)
                                                                             at com.example.ricky.comenchat.MainActivity.onCreate(MainActivity.java:149)
                                                                             at android.app.Activity.performCreate(Activity.java:6251)

While this is the function used in MainActivity

public class MainActivity extends AppCompatActivity {

@Overide
protected void onCreate(Bundle savedInstanceState){
     setupList; //(MainActivity.java:149)
}

private void setupList{
     nameList = getNamelist(); //(MainActivity.java:214)
}

private String[] getNamelist(){
     ArrayList<String> names = new ArrayList<>();
     names.add(intercomApp.getName()); //(MainActivity.java:383)
}

EDITED: Below is my manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ricky.comenchat">

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.NETWORK" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
rickysy
  • 89
  • 3
  • 9

2 Answers2

3

It seems you are missing the Application in your Manifest, so the Context is null

<application
    android:name=".util.intercomApp"
    android:allowBackup="true"

Sidenote: Java conventions - class names should start with a capital letter

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
-1

Rather than creating an instant like this

public static intercomApp getInstance() {
    return instance;
}

public static Context getContext() {
    return instance;
}

You can do the following and make the IntercomApp class generic as like follows

public class IntercomApp {

    protected static final String SHARED_PREFS_REFERENCE_LABEL = "ComeNChat";
    protected static final String DEFAULT_NAME = "Default_Name";

    // store our name to shared prefs
    public static void storeName(String name, Context context) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putString(SHARED_PREFS_REFERENCE_LABEL, name).commit();
    }

    // get our name from shared prefs
    public static String getName(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString(SHARED_PREFS_REFERENCE_LABEL, DEFAULT_NAME); //(intercomApp.java:45) 
    }        
}

And use it in any of your activities like follows

public class MainActivity extends AppCompatActivity {
    private Context context;

    @Overide
    protected void onCreate(Bundle savedInstanceState){
        context = this;
        setupList(); //(MainActivity.java:149)
    }

    private void setupList() {
        nameList = getNamelist(); //(MainActivity.java:214)
    }

    private String[] getNamelist(){
        ArrayList<String> names = new ArrayList<>();
        names.add(IntercomApp.getName(context)); //(MainActivity.java:383)
    }
}
Gaurav Sarma
  • 2,022
  • 2
  • 19
  • 39
  • You missed the point of what he was trying to do- he wanted to make it so he could just get his global application object and use it as the context, rather than pass in a context which can sometimes be awkward (it may require passing a context down through multiple layers). – Gabe Sechan Sep 06 '16 at 20:31
  • this does not work for me, it makes cannot resolve symbol 'context' – rickysy Sep 06 '16 at 20:33
  • Did you define this at the top : private Context context; – Gaurav Sarma Sep 06 '16 at 20:38
  • Whoever down voted this could you please be kind or man enough to tell me why ? – Gaurav Sarma Sep 06 '16 at 20:40
  • Sorry that I missed out that part, but it still doesn't work due to I'm still using the getContext() in another java file – rickysy Sep 06 '16 at 20:44
  • 1
    getContext() is supported from API 21 i.e lolipop. So if you are support older APIs then you will need to use getBaseContext() or getApplicationContext() – Gaurav Sarma Sep 06 '16 at 20:55
  • @Im_Just_A_Beginner Did this answer helped you solve the issue in any way ? – Gaurav Sarma Sep 07 '16 at 13:22
  • your answer did gave me a lot of hints! my application worked now, thanks a lot for your help! – rickysy Sep 07 '16 at 22:10