0

私はこのアイテムクラスを持っています

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;
    }

}

次に、アイテムのリストを含む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;
    }
}

ItemSet のリストを含むトランザクション クラスがあります。

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;
    }

}

そして、これはメインクラスです

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");
           }
        */

    }
}

問題はそれが得ていることです

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

どこ

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

そしてどこに

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

私の間違いを見つけるのを手伝ってください。

4

3 に答える 3

5
this.hold.add(item); 

hold (List)nullインスタンス変数のため、デフォルトです。アイテムを追加する前にインスタンス化する必要があります。null結果に対して実行するすべての操作NullPointerException

例:

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

}

延長する具体的な理由ArrayList()?

于 2012-08-31T18:28:50.840 に答える
2

これを試して:

private List<Item> hold = new ArrayList<Item>();

ItemListクラス内。

これまでに記述した内容は、プライベート フィールドの定義にすぎません。holdオブジェクトをインスタンス化していない (つまり、リストを作成していない) ためhold、null であり、したがって NPE です。

于 2012-08-31T18:29:18.593 に答える
1

を使用する前に を初期化していませんhold List...

以下のコードを使用してItemSetを初期化しholdます。

private List<Item> hold = new ArrayList<Item>();

于 2012-08-31T18:33:52.647 に答える