0

I need to show gridview in alert dialog. But i stuck with the error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference

My code...

listView_prev.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
            //listview click event handling
           TextView id = (TextView) arg1.findViewById(R.id.textView17);
            final int id_To_Search = Integer.valueOf(id.getText().toString());
            Cursor item=mydb.singlecons(id_To_Search);
            Cursor att=mydb.attrs(id_To_Search);
            Cursor picloc=mydb.singleconspic(id_To_Search);
            att.moveToFirst();
            List<String> list = new ArrayList<>();
          //  Log.d("temp",att.getColumnName(1));

            while (!att.isAfterLast())
            {
                int l=att.getColumnCount();
                Log.d("length", String.valueOf(l));
                for(int i=2;i<l;i++){
                Log.d("for","for");
                    if(att.getString(i)!=null){
                        String b= att.getColumnName(i)+" "+att.getString(i);
                        list.add(b);
                        Log.d("att",b);
                    }
                }
                Log.d("while","while");
                att.moveToNext();
            }
            att.close();
            Log.d("list", String.valueOf(list));
            picloc.moveToFirst();

            FilePathStrings = new String[picloc.getCount()];
            int i=0;
            while (!picloc.isAfterLast()){
                Log.d("picloc",picloc.getString(2));
                FilePathStrings[i]=picloc.getString(2);
                i++;
                picloc.moveToNext();
            }

            item.moveToFirst();

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
            dialogBuilder.setView(dialogView);

            TextView con=(TextView)dialogView.findViewById(R.id.textView29);
            con.setText("Consultation on "+item.getString(4));
            TextView des=(TextView)dialogView.findViewById(R.id.textView28);
            des.setText(item.getString(2));
            TextView pre=(TextView)dialogView.findViewById(R.id.textView31);
            pre.setText(item.getString(3));
            TextView fee=(TextView)dialogView.findViewById(R.id.textView32);
            fee.setText(item.getString(5));
            adapter1 = new GridViewAdapter(FilePathStrings,getApplicationContext());
            grid.setAdapter(adapter1);
            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();

Gridviewadapter.java

class GridViewAdapter extends BaseAdapter {

private String[] filepath;
private static LayoutInflater inflater = null;
GridViewAdapter(String[] fpath,Context context) {
    filepath = fpath;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
public int getCount() {
    return filepath.length;

}

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
        ImageView image = (ImageView) vi.findViewById(R.id.image);
        Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
        image.setImageBitmap(bmp);
        return vi;
}

}

Alertlabeleditor.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="20dp">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView29"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="24sp"
            android:layout_marginBottom="10dp"
            android:textStyle="normal|bold" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:text="Description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView27"
                    android:layout_weight="2" />

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView28"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:text="Prescription"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView30"
                    android:layout_weight="2" />

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView31"
                    android:layout_weight="1" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:text="fee"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView33"
                    android:layout_weight="2" />

                <TextView
                    android:text="TextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView32"
                    android:layout_weight="1" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" />

</LinearLayout>

How can i resolve this issue.? Where is the error occurring in my code? I know null pointer error due to accessing values from where there is no value, but i cant figure it out where this is occurring and how can i resolve it. How can i solve this issue.?

1 Answers1

0

Your GridView reference is null

make sure you have initialized your GridView reference

example

grid = (GridView) dialogView.findViewById(R.id.gridview);

Replace your onclickListner with this

listView_prev.setOnItemClickListener(new AdapterView.OnItemClickListener() {


    @Override
    public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
        //listview click event handling
       TextView id = (TextView) arg1.findViewById(R.id.textView17);
        final int id_To_Search = Integer.valueOf(id.getText().toString());
        Cursor item=mydb.singlecons(id_To_Search);
        Cursor att=mydb.attrs(id_To_Search);
        Cursor picloc=mydb.singleconspic(id_To_Search);
        att.moveToFirst();
        List<String> list = new ArrayList<>();
      //  Log.d("temp",att.getColumnName(1));

        while (!att.isAfterLast())
        {
            int l=att.getColumnCount();
            Log.d("length", String.valueOf(l));
            for(int i=2;i<l;i++){
            Log.d("for","for");
                if(att.getString(i)!=null){
                    String b= att.getColumnName(i)+" "+att.getString(i);
                    list.add(b);
                    Log.d("att",b);
                }
            }
            Log.d("while","while");
            att.moveToNext();
        }
        att.close();
        Log.d("list", String.valueOf(list));
        picloc.moveToFirst();

        FilePathStrings = new String[picloc.getCount()];
        int i=0;
        while (!picloc.isAfterLast()){
            Log.d("picloc",picloc.getString(2));
            FilePathStrings[i]=picloc.getString(2);
            i++;
            picloc.moveToNext();
        }

        item.moveToFirst();

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
        dialogBuilder.setView(dialogView);

        TextView con=(TextView)dialogView.findViewById(R.id.textView29);
        con.setText("Consultation on "+item.getString(4));
        TextView des=(TextView)dialogView.findViewById(R.id.textView28);
        des.setText(item.getString(2));
        TextView pre=(TextView)dialogView.findViewById(R.id.textView31);
        pre.setText(item.getString(3));
        TextView fee=(TextView)dialogView.findViewById(R.id.textView32);
        fee.setText(item.getString(5));
        adapter1 = new GridViewAdapter(FilePathStrings,getApplicationContext());
        grid = (GridView) dialogView.findViewById(R.id.gridview);
        grid.setAdapter(adapter1);
        AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();
Jayanth
  • 5,954
  • 3
  • 21
  • 38
  • can you update the error log now and show us alert_label_editor xml file – Jayanth Jan 05 '17 at 06:39
  • thanks... it fixed it... But the alertdialog is very slow.. and sometimes its shows `java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 16777168 free bytes and 18MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method)` –  Jan 05 '17 at 07:20
  • if you are loading images in gridview try loading it with Picasso or Glide – Jayanth Jan 05 '17 at 07:25
  • take a look at this answer too http://stackoverflow.com/a/32245018/5235032 – Jayanth Jan 05 '17 at 07:25
  • I ve looked that ans but it works withoutcrashing but when i click on the list the devices freezes for 5 sec and displays the alert dialog –  Jan 05 '17 at 07:30
  • Do your database related operations background then it'll be fine – Jayanth Jan 05 '17 at 08:14