0

My application stops with a null pointer exception at the ArrayAdaptor line in the code. It is a basic implementation of ArrayAdaptor. It should work according to the tutorials available on net.The arrays are not null i am able to print them. Not able to figure out why is this error coming. Any help is appreciated. I am pasting the relevant code below.

Class to display the data using adapter:

@Override
ListView l;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_displayinvestment);
    context = this;
    l = (ListView) findViewById(R.id.listView1);

}

public void displayArr(String[] arr) {

    System.out.println(arr);

    Log.d("MYAPP in displayinvestment", arr[2]);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, arr);

    l.setAdapter(adapter);
}

}

Displayactivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayInvestment" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="95dp" >
</ListView>

LogCat output :

04-27 09:32:34.610: I/System.out(1464): **Array being passed to the display method[Ljava.lang.String;@536ae1ec**

04-27 09:32:34.610: I/System.out(1464): [Ljava.lang.String;@536ae1ec
**04-27 09:32:34.614: D/MYAPP: array in the display method(1464): test3**

04-27 09:32:34.614: D/AndroidRuntime(1464): Shutting down VM
04-27 09:32:34.618: W/dalvikvm(1464): threadid=1: thread exiting with uncaught exception (group=0xa6292288)
04-27 09:32:34.622: E/AndroidRuntime(1464): FATAL EXCEPTION: main
04-27 09:32:34.622: E/AndroidRuntime(1464): java.lang.IllegalStateException: Could not execute method of the activity
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View$1.onClick(View.java:3591)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View.performClick(View.java:4084)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View$PerformClick.run(View.java:16966)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.os.Handler.handleCallback(Handler.java:615)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.os.Looper.loop(Looper.java:137)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at dalvik.system.NativeStart.main(Native Method)
04-27 09:32:34.622: E/AndroidRuntime(1464): Caused by: java.lang.reflect.InvocationTargetException
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.view.View$1.onClick(View.java:3586)
04-27 09:32:34.622: E/AndroidRuntime(1464):     ... 11 more

**04-27 09:32:34.622: E/AndroidRuntime(1464): Caused by: java.lang.NullPointerException**
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.example.portfoliomanagement.DisplayInvestment.displayArr(DisplayInvestment.java:34)
04-27 09:32:34.622: E/AndroidRuntime(1464):     at com.example.portfoliomanagement.InvestmentPage.getdata(InvestmentPage.java:63)
04-27 09:32:34.622: E/AndroidRuntime(1464):     ... 14 more

2 Answers 2

1

context is null for some reason.

The most likely reason would be you instantiated the activity with new which you cannot do - for starters, the onCreate() lifecycle callback is not called.

To instantiate activities, use an Intent.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked...i used the intent to send the array to new activity. Thanks for the solution. appreciate it.
0
04-27 09:32:34.622: E/AndroidRuntime(1464): Caused by: java.lang.NullPointerException
04-27 09:32:34.622: E/AndroidRuntime(1464):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)

Line 310 of ArrayAdapter.java is the following:

mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

This means that context is null.

Comments

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.