数据库为空时出现ArrayAdapter错误

时间:2018-01-15 19:19:29

标签: android sqlite

我试图将我的数据从我的SQLite数据库调整到我的ListView,当表有一些行时,没有问题,但是当我从表中删除所有插入时,我有一个错误:< / p>

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference

我的适配器代码:

public class AdapterTop extends ArrayAdapter<Top> {
    private LayoutInflater mInflat;
    private ArrayList<Top> top = new ArrayList<Top>();
    private int mVRessId;

    public AdapterTop (Context context, int ressId, ArrayList<Top> topu){

        super(context,ressId,topu);
        this.top =topu;
        mInflat = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mVRessId =ressId;
    }

    public View getView(int position, View convertedView, ViewGroup parents){
        convertedView = mInflat.inflate(mVRessId,null);

        Top topu = top.get(position);
        if (topu != null) {
            TextView name = (TextView) convertedView.findViewById(R.id.txtnomp);
            TextView nbre = (TextView) convertedView.findViewById(R.id.txtnbre);

            if (name != null) {
                name.setText("" + topu.getNom() + ": " + topu.getPrenom());
            }
            if (nbre != null) {
                nbre.setText(String.valueOf(+topu.getNum()));
            }

        }
        return convertedView;

        }


}

用于返回光标的函数:

public Cursor topuser(){
        SQLiteDatabase db = this.getReadableDatabase();;
        Cursor data = db.rawQuery("Select U." +KEY_NOM+ ", U." +KEY_PRENOM+ ", count (A." +KEY_MATRICULE+ " ) FROM " +TABLE_ANSWER+ " A, "+TABLE_USER+ " U where U." +KEY_MATRICULE+ " = A." +KEY_MATRICULE+ " LIMIT 3",null );
        return data;
    }

我的主要java代码:

ArrayList<Top> users;
    Top top;
    mCsr = openhelper.topuser();
            int rows = mCsr.getCount();

        if (rows == 0) {
            Toast.makeText(Kpis.this, "no users", Toast.LENGTH_LONG).show();
        } else {

            while (mCsr.moveToNext()) {

                top = new Top(mCsr.getString(0).toString(), mCsr.getString(1).toString(), mCsr.getInt(2));
                users.add(top);

            }
            AdapterTop adapttop = new AdapterTop(this, R.layout.activity_template_top_users, users);
            mListView.setAdapter(adapttop);
        }

有什么问题吗?我做了条件如果(rows == 0)tu避免这种情况但仍然有同样的问题。

Top classe是这样的:

public class Top {
    private String nom;
    private String prenom;
    private int num;

    public Top(String nom, String prenom, int num) {
        this.nom = nom;
        this.prenom = prenom;
        this.num = num;
    }

    public String getNom() {
        return nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public int getNum() {
        return num;
    }
}

我尝试在创建它时启动顶部但是没有工作。

1 个答案:

答案 0 :(得分:1)

  

我做了条件如果(rows == 0)tu避免这种情况但仍然有同样的问题。

它会阻止您获得越界异常,但可能在您保存数据时,列可以为空。那么你有记录,但是这条记录的某些字段为空。

此时:

top = new Top(mCsr.getString(0).toString(), mCsr.getString(1).toString(), mCsr.getInt(2));

您正在致电toString()了解您确实知道的事情。 所以处理这个错误的简单方法是:

String noum = "";
if(mCsr.getString(0) != null){
    noum = mCsr.getString(0).toString()
}

String preNoum = "";
if(mCsr.getString(1) != null){
    preNoum = mCsr.getString(1).toString()
}
top = new Top(noum, preNoum, mCsr.getInt(2));

您可以使用某些模式改进代码,但它可以解决问题。