Alex Rivera | Logout

NullPointerException at android.widget.ArrayAdapter.createViewFromResource

Asked 2012-10-10T14:25:40.067
8

I am creating a application with preference activity where the listpreference entries are problematically assigned. This application works fine on my emulator but while i test my application in mobile it shows error like this

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2033)

This problem arises when i click that listpreference in my mobile. here this is my code

prference.xml

<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
            android:title="@string/settings">            
        <ListPreference
                android:key="@string/resolution"
                android:title="@string/imageres"
                android:summary="@string/summaryres"
                android:dialogTitle="@string/resFormat" 
                />
    </PreferenceCategory>
</PreferenceScreen>

settings.java

package com.example;

import android.database.Cursor;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

public class Settings extends PreferenceActivity {
    private SQLiteoperations SQLiteAdapter;
    DTO get;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        get = (DTO) getIntent().getExtras().get("obj");
        SQLiteAdapter = new SQLiteoperations(this);
        SQLiteAdapter.openToWrite();
        addPreferencesFromResource(R.xml.preferences);
        ListPreference pref = (ListPreference) findPreference("resolution");
        pref.setEntries(getEntries());
        pref.setEntryValues(getEntries());
        System.out.println("after set entries");
        System.
Edit
Report

1 Answer

11

Anyone please tell where i am wrong?

The thing you did wrong in your code is the way you initialize the CharSequence array in the getEntries method. Your code:

if (cursor.moveToFirst()){
    int i=0;
    do{
       size[i]=new String(cursor.getString(1)+"X"+cursor.getString(2));
       System.out.println(cursor.getString(1)+"X"+cursor.getString(2));
    }while(cursor.moveToNext());
}

You initialize the size array with the size of the Cursor and then you go in to the while loop above. The problem is that your while loop only sets the value for i = 0 as you don't increase i's value in that loop. For this reason your size array will have a value only for position 0 and null on all other positions. This will make the ArrayAdapter throw that NullPointerException.

The correct loop would be:

if (cursor.moveToFirst()){
    int i=0;
    do{
       size[i]=new String(cursor.getString(1)+"X"+cursor.getString(2));
       System.out.println(cursor.getString(1)+"X"+cursor.getString(2));
       i++;
    }while(cursor.moveToNext());
}

The full getEntries method would be:

private CharSequence[] getEntries() {
    Cursor cursor = SQLiteAdapter.queueAll();
    int num = cursor.getCount();        
    String[] size = new String[num];
    System.out.println(cursor.getCount());
    if (cursor.moveToFirst()) {         
        int i = 0;
        do {
            size[i] = new String(cursor.getString(1) + "X"
                    + cursor.getString(2));             
            i++;
        } while (cursor.moveToNext());
    }
    cursor.close();
    return size;
}
answered 2012-10-15T06:17:08.293

Your Answer