Results 1 to 4 of 4
- 09-19-2009, 10:38 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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"); }}
- 09-19-2009, 10:48 PM #2
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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. :)
- 09-19-2009, 10:53 PM #3
store was never created in the ASet class. So,
is checking the size of a null object. Create the array in the constuctor:Java Code:if (store.isEmpty()== true) return true; else return false;
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(); private ArrayList<T> store; public ASet(){ store = new ArrayList<T>(); }
Do this instead:Java Code:public class ASet<T> implements SetInterface<T> { private static Random rand = new Random(); ... }
Java Code:public class ASet<T> implements SetInterface<T> { private Random rand; public ASet(){ rand = new Random(); } ... }
- 09-19-2009, 11:30 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
arraylist question
By lisa.lipsky in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-16-2009, 11:07 AM -
Another beginner question for AP test
By DanK in forum New To JavaReplies: 1Last Post: 04-27-2009, 05:36 AM -
Beginner Java question
By DanK in forum New To JavaReplies: 3Last Post: 04-27-2009, 04:29 AM -
Basic Question from Networking Beginner
By JDCAce in forum NetworkingReplies: 7Last Post: 10-10-2008, 08:29 PM -
Confused beginner with Rectangle type question
By sigssoft in forum New To JavaReplies: 2Last Post: 10-08-2008, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks