0

This is my Class

  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 }

And this is my Test case.

  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 }

I keep getting this error

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

I can read and pinpoint where the problem is but I don't know what to correct it to. This is the first time I'm writing a user-defined class with an ArrayList inside. Previously I've only be writing classes with primitive data types. So I'm confused with a lot of things like what the constructors accessors and mutators are supposed to look like, when to use the 'this' reference. Someone answered and I inferred that if I'm using an arraylist here with the attribute 'members', I should use the reference to 'members' rather than 'this' because that's the attribute that I want to play around with. I know it's not a hard and fast rule but I sorta get the picture. Please help!!

CHEWWWWWWWWWW
  • 169
  • 3
  • 20
  • You should avoid "star imports". While technically valid, they are deprecated. In your particular case, you should be aware of the fact that there is already a Java interface called `Set` in the package `java.util`. – Mad Physicist Feb 12 '14 at 16:40
  • @MadPhysicist Do you any reliable source saying "start imports" are deprecated? I don't think so. At least eclipse doesn't mark them as deprecated. – Rohit Jain Feb 12 '14 at 16:41
  • @RohitJain http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad – Mad Physicist Feb 12 '14 at 16:44
  • @MadPhysicist Where is it mentioned in that question that it is deprecated? It is just talking about best practices. – Rohit Jain Feb 12 '14 at 16:50

1 Answers1

1

The constructor:

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

doesn't do anything. It just creates an object, and discards it. So the members instance variable is still null. Change the constructor to:

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

And please choose a different name for your class. Set is already an interface in Java API.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525