Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java.lang.NullPointerException at getapplicationContext() while initiating adapter

While running the project on Android emulator,I face the exception as java.lang.NullPointerException at getApplicationContext.Please help me to resolve this issue.

Error

07-19 15:08:07.811: D/AndroidRuntime(366): Shutting down VM
07-19 15:08:07.811: W/dalvikvm(366): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-19 15:08:07.841: E/AndroidRuntime(366): FATAL EXCEPTION: main
07-19 15:08:07.841: E/AndroidRuntime(366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.TfsMutualFund/org.TfsMutualFund.loading}: java.lang.NullPointerException
07-19 15:08:07.841: E/AndroidRuntime(366):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-19 15:08:07.841: E/AndroidRuntime(366):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

07-19 15:08:07.841: E/AndroidRuntime(366): Caused by: java.lang.NullPointerException
07-19 15:08:07.841: E/AndroidRuntime(366):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
07-19 15:08:07.841: E/AndroidRuntime(366):  at org.TfsMutualFund.loading.<init>(loading.java:23)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="org.TfsMutualFund">   
<uses-sdk android:targetSdkVersion="8" />

<application android:name=".globalAdapter" android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".loading"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar"
              android:configChanges="orientation|keyboard|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".TFSManinActivity"
    android:configChanges="orientation|keyboard|keyboardHidden"/>
</application>

loading.java

package org.TfsMutualFund;

public class loading extends Activity{
private static ArrayAdapter<String> adapter;
private globalAdapter adpt = ((globalAdapter)getApplicationContext());
private String ServicePath = adpt.getServicePath();
private String ServiceName = adpt.getServiceName();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading);
 if(isOnline())
    new AsyncLoad().execute();
 else
like image 564
Mahavir Avatar asked Jul 19 '12 09:07

Mahavir


People also ask

How to solve Java lang NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

How to solve Java lang NullPointerException in Android?

Go to your project, there's ". gradle" folder. Delete it and restart Android Studio and immediately rebuild the project. or Go to your project, there's ". idea" folder in this folder delete libraries folder and restart Android Studio and immediately rebuild the project.

How to solve NullPointerException in Android Studio?

To avoid NullPointerException we have to initialize the Textview component with the help of findviewbyid( ) method as shown below. The findViewbyId( ) takes the “id” value of the component as the parameter. This method helps locate the component present in the app.

What is null pointer exception in Android?

NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.


2 Answers

Don't initialize it before OnCreate(), you can't get Context with there, do it in onCreate().

private globalAdapter adpt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading);
    adpt  = ((globalAdapter)getApplicationContext());
    ...
}
like image 163
Lalit Poptani Avatar answered Oct 23 '22 22:10

Lalit Poptani


1. Dont initialize the below before the onCreate, let the views get their ids first

private globalAdapter adpt = ((globalAdapter)getApplicationContext());
private String ServicePath = adpt.getServicePath();
private String ServiceName = adpt.getServiceName();

2. Just Declare them....

private globalAdapter adpt;
private String ServicePath;  
private String ServiceName;

3. Its because when the Activity is not formed, how can you get the Context to that activity, cause you are using the getApplicationContext() to get the current Activity context.

like image 2
Dhruvil Patel Avatar answered Oct 23 '22 23:10

Dhruvil Patel