|
-
array object class problem
hi there. i am trying to figure out what i am doing wrong here.
i have a class called Bag. in this class, the constructor is to create an array of type int called "x".
then i have a class called main that will call provide the interface and testing for the Bag class.
i am not declaring my Bag object correctly, though . . . i think.
also, do i need any packages imported . . . ? could that be what i am doing wrong here?
public class Bag{
private int i;
private int [] x;
private int top_pos = 0;
public Bag (){ //def. const.
i = 10;
int [] x = new int [i];
}
// method to add values to x
public void add (int val){
x[top_pos] = val;
top_pos++;
}
and then the class main is as follows . . .
public class main {
public static void main(String[] args) {
Bag b = new Bag();
b.add(5);
}
i tried making other method too. but as with the add method, when i call them, i get a null pointer exception.
i can't find an example of how to declare an array object or of an array constructor. have any?
thanks
crq
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks