Results 1 to 4 of 4
  1. #1
    kesi is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Beginner question about ArrayList

    Hi all, I'm trying to implement an array list of generic data types.

    Essentially I'm only using the following methods and getting the error:

    Exception in thread "main" java.lang.NullPointerException
    at ASet.toString(ASet.java:51)
    at java.lang.String.valueOf(String.java:2838)
    at java.lang.StringBuilder.append(StringBuilder.java: 132)
    at TestASet.main(TestASet.java:8)

    Can anyone tell me what I'm doing wrong here? Thanks in Advance!

    Java Code:
    import java.util.*;
    
    public class ASet<T> implements SetInterface<T>
    {
    
        private static Random rand = new Random();
        private ArrayList<T> store;
    
        public boolean isEmpty()
        {
    	if (store.isEmpty()== true)
    	    return true;
    	else
    	    return false;
        }
    
        public String toString()
        {
    	return "{" + store.toString() + "}";
        }
    }
    Java Code:
    public class TestASet{
       public static void main(String[] args){
    
    ASet set4 = new ASet<Integer>();
    
          System.out.print("set4 = " + set4 + "\n");                     //toString() in action
    
          if(set4.isEmpty())                                             //isEmpty() in action
             System.out.println(set4 + " is empty");
          else
             System.out.println(set4 + " is not empty");
    }}

  2. #2
    kesi is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    OK, I see something see something obvious. It's null because I never instantiate the arraylist. Now I have to figure out how to do that. It's been awhile since I've had to do any of this. :)

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    store was never created in the ASet class. So,
    Java Code:
    if (store.isEmpty()== true)
    	    return true;
    	else
    	    return false;
    is checking the size of a null object. Create the array in the constuctor:
    Java Code:
    public class ASet<T> implements SetInterface<T>
    {
    
        private static Random rand = new Random();
        private ArrayList<T> store;
        
        public ASet(){
            store = new ArrayList<T>();
        }
    Also, why is your Random static? If this class NEEDS to be static for some reason, then you need to put object creation in a static{} block instead, but generally you should keep away from static -- also, create your objects in the constructor as a general rule of thumb, instead of creating then during declaration. For example, instead of:
    Java Code:
    public class ASet<T> implements SetInterface<T>
    {
        private static Random rand = new Random();
        ...
    }
    Do this instead:
    Java Code:
    public class ASet<T> implements SetInterface<T>
    {
        private Random rand;
    
        public ASet(){
             rand = new Random();
        }
        ...
    }

  4. #4
    kesi is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

Similar Threads

  1. arraylist question
    By lisa.lipsky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-16-2009, 12:07 PM
  2. Another beginner question for AP test
    By DanK in forum New To Java
    Replies: 1
    Last Post: 04-27-2009, 06:36 AM
  3. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 05:29 AM
  4. Basic Question from Networking Beginner
    By JDCAce in forum Networking
    Replies: 7
    Last Post: 10-10-2008, 09:29 PM
  5. Confused beginner with Rectangle type question
    By sigssoft in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •