无法容纳物品

Nyf*_*fer 0 java nullpointerexception

我有这个Item类

public class Item {

    private String id; 
    private int count;
    private String  name;


    public int getcount() {
        return this.count; 
    }

    public Item(String name) {
        this.name=name;
        this.id = "";
    }

    public Item(String id, String name) {
        this.name=name;
        this.id=id;
    }

    public Item(int count) {
        this.count=count;
    }

    public String getItemName() {
        return this.name;
    }

    public String getItemId() {
        return this.id;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后我有ItemSet类,它将包含项目列表

public  class ItemSet  extends ArrayList<Item> {

    private List<Item> hold;

    ItemSet(Item item) {
        this.hold.add(item);
    }

    ItemSet() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }


    public List<Item> getItemSet() {
        return this.hold;
    }
}
Run Code Online (Sandbox Code Playgroud)

我有包含ItemSet的List的事务类:

public class Transaction extends ArrayList<ItemSet> {

    // ArrayList<String> l;
    public ItemSet getUniqueItem() {
        ResultSet rs;
        Database d=new Database();
        ItemSet unique=new ItemSet();
        unique.clear();
        String query="Select id,name from item";
        rs=d.sendQuery(query);
        try{  
            while(rs.next()) {
                //System.out.println(rs.getString(1)+"\t"+rs.getString(2));
                Item item=new Item(rs.getString(1),rs.getString(2));
                unique.add(item);        
            } 
        }catch(Exception e){
            System.out.print(e.getMessage());
        }

        for(Item item:unique) {
            // System.out.println(item.getItemId()+": "+item.getItemName());
        }
        return unique;
    }

    public int countItems(ItemSet itemset) {
        ResultSet rs;
        Database d=new Database();
        String query="";
        int count=0;
        for(Item i:itemset) {
            String id=i.getItemId();
            query="SELECT count(*) FROM `item_transaction` where item_id="+i;
            rs=d.sendQuery(query);
            try {
                while(rs.next()) {
                    //System.out.print(rs.getString(1));
                    count=Integer.parseInt(rs.getString(1));
                    System.out.print(count+"\t");
                }
            }catch(Exception e){ }
        }

        return count;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是主要的课程

public class Ap {

    public static void main(String args[]) {
        Transaction t=new Transaction();
        Transaction Ci=new Transaction();
        Transaction Li=new Transaction();

        ItemSet I=t.getUniqueItem();
        for(Item i:I) {
            System.out.println(i);
            ItemSet itemSet=new ItemSet(i);
            Ci.add(itemSet);
        }

        for(ItemSet itemSet:Ci) {
            int x=t.countItems(itemSet);
            System.out.print(x+"\t");
        }

        /* Iterator iter=Li.iterator();
           while(iter.hasNext()) {
               // System.out.print(iter.next()+"\t");
           }
        */

    }
}
Run Code Online (Sandbox Code Playgroud)

问题是我得到了

Exception in thread "main" java.lang.NullPointerException
at ItemSet.<init>(ItemSet.java:25)
at Ap.main(Ap.java:30)
Run Code Online (Sandbox Code Playgroud)

哪里

ItemSet.java:25 是指 this.hold.add(item);

在哪里

Ap.java:30 是指 ItemSet itemSet=new ItemSet(i); // creating new ItemSet

请帮我找出错误.

kos*_*osa 5

this.hold.add(item); 
Run Code Online (Sandbox Code Playgroud)

hold (List)是默认的,null因为实例变量.您需要在添加项目之前进行实例化.您对null结果执行的操作NullPointerException

例:

ItemSet(Item item)
{
   hold = new ArrayList<Item>();
   this.hold.add(item);

}
Run Code Online (Sandbox Code Playgroud)

任何具体的延伸原因 ArrayList()?