Home » Android » NullPointerException at android.widget.ArrayAdapter.createViewFromResource

NullPointerException at android.widget.ArrayAdapter.createViewFromResource

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.out.println(get.getDefaultvalues());
        pref.setDefaultValue(get.getDefaultvalues());

    }
    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));
                   System.out.println(cursor.getString(1)+"X"+cursor.getString(2));
               }while(cursor.moveToNext());
            }
            cursor.close();

        return size;
    }

}

camerademo.java

package com.example;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class CameraDemo extends Activity {
    private final int DIALOG_LICENSE=5;
    private final int MENU_SETTINGS=2;
    private SharedPreferences settings;
    private static final String TAG = "CameraDemo";
    Camera camera;
    Preview preview;
    Button buttonClick,buttonSave,buttonBack;
    byte[] image;
    private static SQLiteoperations placeData;
    final DTO set = new DTO();
    String defsize;
    // Called when the activity is first created. 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        placeData = new SQLiteoperations(this);
        placeData.openToWrite();

        preview = new Preview(this);
        ((FrameLayout) findViewById(R.id.preview)).addView(preview);
        buttonSave = (Button) findViewById(R.id.Save);
        buttonBack = (Button) findViewById(R.id.back);
        buttonBack.setVisibility(View.INVISIBLE);
        buttonSave.setVisibility(View.INVISIBLE);
        buttonClick = (Button) findViewById(R.id.buttonClick);
        settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences licensPref = getSharedPreferences("PREFS", 0);
        boolean licenseShown = licensPref.getBoolean("LicenseShown", false);
        if(!licenseShown)
            showDialog(DIALOG_LICENSE);

        buttonClick.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                preview.camera.takePicture(shutterCallback, rawCallback,
                        jpegCallback);
                buttonClick.setVisibility(View.INVISIBLE);
                buttonSave.setVisibility(View.VISIBLE);
                buttonBack.setVisibility(View.VISIBLE);
            }
        });
        buttonBack.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                back();
            }
        });
        buttonSave.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                save();
                }
        });

        Log.d(TAG, "onCreate'd");
    }

    ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
            Log.d(TAG, "onShutter'd");
        }
    };

    // Handles data for raw picture 
    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - raw");
        }
    };

    // Handles data for jpeg picture 
    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            image = data;
        }
    };

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {     
        super.onPrepareDialog(id, dialog);
        }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id){
        case DIALOG_LICENSE:
            return new AlertDialog.Builder(this)
            .setTitle(R.string.welcome)
            .setMessage(R.string.welcome_text)
            .setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    settings = getSharedPreferences("PREFS", 0);
                    Parameters parameters = preview.camera.getParameters();
                    Size defaultsize = parameters.getPictureSize();
                    defsize = (new String(defaultsize.width+"X"+defaultsize.height));
                    List<Camera.Size> size = parameters.getSupportedPictureSizes();
                    int num = size.size();
                    String[] sizeArray = new String[num];
                    int i=0;
                    Iterator<Size> si = size.iterator();
                    while (si.hasNext()) {
                        Camera.Size sizeObj = (Size) si.next();
                        sizeArray[i]=new String(sizeObj.width+ "X" +sizeObj.height);
                        Log.e("size in licence dialog", sizeObj.height+"x"+sizeObj.width);
                        i++;
                        placeData.insert(String.valueOf(sizeObj.width),String.valueOf(sizeObj.height));
                    }
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("LicenseShown", true);
                    editor.commit();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    finish();
                }              
            })
            .setCancelable(false)
            .create();
        }
        return null;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem mi;
        mi = menu.add(0, MENU_SETTINGS, 0, R.string.settings);
        mi.setIcon(android.R.drawable.ic_menu_preferences);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_SETTINGS:
            set.setDefaultvalues(defsize);
            Intent intent = new Intent(getApplicationContext(), Settings.class);
            intent.putExtra("obj", set);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
public void back(){
    preview.camera.startPreview();
    buttonBack.setVisibility(View.INVISIBLE);
    buttonSave.setVisibility(View.INVISIBLE);
    buttonClick.setVisibility(View.VISIBLE);
}
@Override
public void onDestroy(){
    super.onDestroy();
    if(preview.camera != null){
        preview.camera.stopPreview();
        preview.camera = null;
    }
}
public void save(){
           back();
            }
}

Anyone please tell where i am wrong?

How to&Answers:

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;
}