arraylist在自我设计的类错误。请帮我

时间:2014-02-12 16:36:59

标签: java syntax arraylist nullpointerexception

这是我的班级

  1 //  This class reads in integers and puts the values into a set
  2 
  3 import java.util.ArrayList;
  4 import java.util.*;
  5 
  6 class Set {
  7 
  8     private ArrayList<Integer> members;
  9     private static int quantity = 0;
 10 
 11     // Constructors
 12     public Set()    {
 13         new ArrayList<Integer>();
  7 
  8     private ArrayList<Integer> members;
  9     private static int quantity = 0;
 10 
 11     // Constructors
 12     public Set()    {
 13         new ArrayList<Integer>();
 14     }
 15 
 16     public Set(int member){
 17         new ArrayList<Integer>();
 18         addMember(member);
 19         quantity++;
 20     }
 21     //  Accessors
 22     public static int getQuantity() {
 23         return quantity;
 24     }
 25 
 26     //  Mutators
 27     public void addMember(int member)   {
 28         members.add(member);
 29     }
 30     // toString() method
 31     public String toString() {
 32         int i;
 33         String str = "[";
 34         System.out.print("[");
 35         for(i=0; i<getQuantity(); i++){
 36             str += members.get(i);
 37             if(i+1 == getQuantity())
 38                 str += "]";
 39             else
 40                 System.out.print(", ");
 41                 str += ", ";
 42         }
 43         return str;
 44     }
 45 
 46     // Return true if 'this' is a subset of 'set', 
 47     // otherwise return false.
 48     public boolean isSubset(Set set) {
 49         if(this.members.contains(set))
 50             return true;
 51         else
 52             return false;
53     }
 54     // Return true if 'this' is equals to 'obj',
 55     // Otherwise return false
 56     public boolean equals(Set set)  {
 57         return (members.contains(set) && set.members.contains(this));
 58     }
 59 }

这是我的测试案例。

  1 // This program reads two sets of integers A and B, and determines
  2 // if A is a subset of B, and if A is same as B.
  3 
  4 import java.util.Scanner;
  5 import java.util.ArrayList;
  6 
  7 public class TestSet {
  8 
  9     public static void main(String[] args) {
 10         Scanner sc = new Scanner(System.in);
 11         int i, setAnum, setBnum;
 12         
 13         System.out.print("Enter number of elements in set A: ");
 14         setAnum = sc.nextInt();
 15         ArrayList<Integer> list1 = new ArrayList<Integer>();
 16         
 17         System.out.print("Enter elements for set A: ");
 18         for(i=0; i<setAnum; i++)    
 19             list1.add(sc.nextInt());
 20             
 21         Set setA = new Set();
 22         for(i=0; i<setAnum; i++)
 23             setA.addMember(list1.get(i));
 24             
 25         System.out.print("Enter number of elements in set A: ");
 26         setBnum = sc.nextInt();
 27         ArrayList<Integer> list2 = new ArrayList<Integer>();
 28         
 29         System.out.print("Enter elements for set A: ");
 30         for(i=0; i<setBnum; i++)    
 31             list2.add(sc.nextInt());
 32             
 33         Set setB = new Set();
 34         for(i=0; i<setAnum; i++)
 35             setB.addMember(list2.get(i));
 36             
 37         System.out.println("Set A: " + setA);
 38         System.out.println("Set B: " + setB);
 39         
 40         if (setA.isSubset(setB)) {
 41             System.out.println("Set A is a subset of set B.");
 42         }   
 43         else {
 44             System.out.println("Set A is not a subset of set B.");
 45         }   
 46         if (setA.equals(setB))
 47             System.out.println("Set A is equal to set B.");
 48         else
 49             System.out.println("Set A is not equal to set B.");
 50     }
 51 
 52 }

我一直收到此错误

Exception in thread "main" java.lang.NullPointerException
        at Set.addMember(Set.java:28)
        at TestSet.main(TestSet.java:23)

我可以阅读并查明问题所在,但我不知道该怎么纠正它。这是我第一次编写一个带有ArrayList的用户定义类。以前我只是用原始数据类型编写类。所以我很困惑很多东西,比如构造函数访问器和mutator应该是什么样的,什么时候使用'this'引用。有人回答说我推断如果我在这里使用带有'members'属性的arraylist,我应该使用'members'而不是'this'的引用,因为这是我想要玩的属性。我知道这不是一个严格而快速的规则,但我有点了解情况。请帮忙!!

1 个答案:

答案 0 :(得分:1)

构造函数:

public Set()    {
    new ArrayList<Integer>();
}

没有做任何事情。它只是创建一个对象,并将其丢弃。因此members实例变量仍为null。将构造函数更改为:

public Set()    {
    members = new ArrayList<Integer>();
}

请为您的班级选择其他名称。 Set已经是Java API中的一个接口。