Исправление ошибки logcat «Попытка записи в нулевой массив»

Я пишу очень простой Home Launcher, я создал класс AppsInfoModel, CustomAdapter и GridView в AppsGridActivity.

Вот мой код для заполнения адаптера в Activity

    appsGrid = (GridView) findViewById(R.id.appsGrid);
    appsInfo = new AppsInfoModel(this);
    appsGrid.setAdapter(new CustomAdapter(this,appsInfo));

Вот адаптер

public class CustomAdapter extends BaseAdapter {

Context context;
AppsInfoModel appInfo;
CustomAdapter(Context context,AppsInfoModel appInfo) {
    this.context = context;
    this.appInfo = appInfo;
}

@Override
public int getCount() {
    return appInfo.getSize();
}

@Override
public Object getItem(int position) {
    return appInfo.getItem(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.single_grid, parent, false);

        ImageView icon = (ImageView) convertView.findViewById(R.id.app_icon);
        icon.setImageDrawable(appInfo.getIcon(position));

        TextView label = (TextView) convertView.findViewById(R.id.app_label);
        label.setText(appInfo.getLabel(position));
        return convertView;
    }
    else
        return convertView;
}

}

И модель класса

public class AppsInfoModel {
String label[];
String name[];
Drawable icon[];
PackageManager pm;
List<ResolveInfo> appsInfo;

AppsInfoModel(Context context){
    pm=context.getPackageManager();
    appsInfo = pm.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER), 0);
    for(int i=0;i<appsInfo.size();i++){
        label[i]= (appsInfo.get(i).loadLabel(pm)).toString();//***This is where problem lies according to LogCat***
        name[i]= (appsInfo.get(i)).activityInfo.packageName;
        icon[i]=(appsInfo.get(i)).activityInfo.loadIcon(pm);
    }
}

Drawable getIcon(int i){
    return icon[i];
}

String getLabel(int i){
    return label[i];
}

String getPackageName(int i){
    return name[i];
}
int getSize(){
    return appsInfo.size();
}

public ResolveInfo getItem(int i) {
    return appsInfo.get(i);
}
}

И вот ошибка Logcat

 java.lang.RuntimeException: Unable to start activity ComponentInfo{example.home/example.home.AppsGridActivity}: java.lang.NullPointerException: Attempt to write to null array
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to write to null array
        at example.home.AppsInfoModel.<init>(AppsInfoModel.java:25)
        at example.home.AppsGridActivity.onCreate(AppsGridActivity.java:21)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Почему это происходит? В чем проблема с логикой или синтаксисом.

Ответы на вопрос(1)

Ваш ответ на вопрос