lundi 20 janvier 2020

Error to retrieve data in listview and checkbox jtds; Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

I've been following a tutorial to display listview and checkbox in jdts in android studio. The problem is, when I convert the Map to List, I get error which is : Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

Here is my full code:

package com.example.myapplication;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


import android.os.Bundle;
import android.app.Activity;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DataModel extends Activity  {
    ConnectionClass connectionClass;
    ResultSet rs;
    CheckBox checkBox;
    UserSessionManager session;
    List<Map<String, String>> MyArrList = null;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkboxtest);

        // listView1
        final ListView lisView1 = (ListView)findViewById(R.id.listView1);
        connectionClass = new ConnectionClass();
        Connection con = connectionClass.CONN();
        String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
        String querycmd = "select * from WORKTR ";

//                where Start_date='"+dates+"' and Scan_by='"+lblname+"'
        try {
            Statement statement = con.createStatement();
            rs = statement.executeQuery(querycmd);
            List<Map<String, String>> MyArrList = null;
            MyArrList = new ArrayList<Map<String, String>>();


            while (rs.next()) {
                Map<String, String> map = new HashMap<String, String>();


                /*** Rows 1 ***/
                map = new HashMap<String, String>();
                map.put("ID", rs.getString("Pf_no"));

                MyArrList.add(map);


            }
//
//                    ViewHolder holder = null;
//                    holder = new  ViewHolder();
//                    holder.checkBox = (CheckBox) v.findViewById(R.id.checkBox);
//                    v.setTag(holder);
//                    holder.checkBox.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            CheckBox cb = (CheckBox) v;
//
//
//                        }
//                    });

//                    lstcountry.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//                        @Override
//                        public void onItemClick(AdapterView parent, View view, int position, long id) {
//
////                            DataModel dataModel= dataModels.get(position);
////                            dataModel.checked = !dataModel.checked;
////                            adapter.notifyDataSetChanged();
//
//
//                        }
//                    });


        } catch (SQLException e) {

//            Toast.makeText(.this, e.getMessage().toString(),
//                    Toast.LENGTH_LONG).show();

        }


        lisView1.setAdapter(new CountryAdapter(this));

        // Check All
        Button btnCheckAll = (Button) findViewById(R.id.btnCheckAll);
        btnCheckAll.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = lisView1.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
                    checkbox.setChecked(true);
                }
            }
        });

        // Clear All
        Button btnClearAll = (Button) findViewById(R.id.btnClearAll);
        btnClearAll.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = lisView1.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
                    checkbox.setChecked(false);
                }
            }
        });

        // Get Item Checked
        Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
        btnGetItem.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = lisView1.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
                    if(checkbox.isChecked())
                    {
                        Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());

                        Toast.makeText(DataModel.this,checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();
                    }
                }
            }
        });

    }


    public class CountryAdapter extends BaseAdapter
    {
        private Context context;

        public CountryAdapter(Context c)
        {
            //super( c, R.layout.activity_column, R.id.rowTextView, );
            // TODO Auto-generated method stub
            context = c;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return MyArrList.size();
        }
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

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

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_column, null);

            }

            // ColID
            TextView txtID = (TextView) convertView.findViewById(R.id.ColID);
            txtID.setText(MyArrList.get(position).get("ID") +".");

            // ColCode

            // ColChk
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("ID"));

            return convertView;

        }

    }

//
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.checkboxtest, menu);
//        return true;
//    }

}

Please help me as I don't know where to change to fix this error. My aim is to display listview from the MSSQL database and ensure the checked textbox can get the list value




Aucun commentaire:

Enregistrer un commentaire