空指针异常Android Java上过滤的项目

时间:2016-07-26 07:12:53

标签: java android

有谁能告诉我如何解决这个问题?我一直试图解决这个问题,但它一直提示我这个错误信息。

CREATE OR REPLACE PROCEDURE add_to_arch( arch_ archive%rowtype ) as begin SELECT * FROM Client WHERE TIMESTAMP >= sysdate -1; loop INSERT INTO archive ( CLIENT_ID, NAME, SURNAME, PHONE, VEH_ID, REG_NO, MADE_MODEL, MAKE_YEAR, WD_ID, WORK_DESC, INV_ID, INV_SERIES, INV_NUM, INV_DATE, INV_PRICE ) SELECT CL_ID, CL_NAME, CL_SURNAME, CL_PHONE, VEH_ID, VEH_REG_NO, VEH_MODEL, VEH_MAKE_YEAR, WD_ID, WORK_DESC, INV_ID, INV_SERIES, INV_NUM, INV_DATE, INV_PRICE FROM CLIENT, INVOICE, VEHICLE, WORKS, WORKS_DONE WHERE CL_ID = arch_.Client_ID; end loop; END;

给我错误的方法。

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.example.apple.qs.Activity.adapter.MessageListAdapter.getItemCount(MessageListAdapter.java:173)

第173行

@Override
    public int getItemCount() {
        System.out.println(filtered_items);
        if(filtered_items.size()==0 || filtered_items==null) {
            return 1;
        }
        else return filtered_items.size();
    }

MessageListAdapter.java

if(filtered_items.size()==0 || filtered_items==null) {

5 个答案:

答案 0 :(得分:1)

getItemCount()替换为

public int getItemCount() {
   System.out.println(filtered_items);
    if(filtered_items!=null || filtered_items.size()>0){
       return filtered_items.size();
    }else{
       return 1;
    }
}

答案 1 :(得分:0)

问题本身就在这一行

if(filtered_items.size()==0 || filtered_items==null) {

你必须改变表达式的顺序:

if(filtered_items==null || filtered_items.size()==0 ) {

另请阅读JLS 15.24:

  

条件或运算符|| operator ...评估它的右手   仅当其左侧操作数的值为false时才操作数

答案 2 :(得分:0)

更改您的陈述
if(filtered_items.size()==0 || filtered_items==null)

到:

if(filtered_items==null || filtered_items.size()==0)

因为有顺序检查条件。如果first为true,则不会检查第二个。

答案 3 :(得分:0)

更改您的陈述
if(filtered_items.size()==0 || filtered_items==null)

为:

if(filtered_items !=null && filtered_items.size()==0)

因为这将首先检查filtered_items值是否为null。如果值为if not null,那么只有它将调用size()方法。

您可以将代码修改为:

if(filtered_items != null) {
    switch(filtered_items.size()) {
        case 0:  
            return 1;
            break;

        default:
            return filtered_items.size();
            break;
    }
}

答案 4 :(得分:0)

@Override
public int getItemCount() {
    int size = 1;
    System.out.println(filtered_items);
    if (filtered_items != null) {
        if (filtered_items.size() > 0) {
            size = filtered_items.size();
        }
    }
    return size;
}

试试这个