Results 1 to 10 of 10
- 02-10-2010, 01:09 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
arraylist help please? trying to add objects to an array list
ok so i want to add an object to the arraylist
code:
ArrayList<facultytag> ttags = new ArrayList<facultytag>();
facultytag a = new facultytag(); //facultytag is another class
ttags.add(a);
When i run this. i get a null pointer exception.......its probably a really simple problem and im just a noob but any help plz?
thanks a bunch
-
Hello, and welcome to the forum!
You're problem is not in the code that you've posted. You will either need to do some debugging (see which object is actually null on the line that causes the NPE to be thrown) or post more of your code here. Oh, and if you do post code, please look at my signature below to learn to use code tags. It makes it much easier to read your posted code in the forum.
Much luck!
- 02-10-2010, 01:33 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
thanks
sorry next time i will post code that way. i figured out what was wrong. i look forward to using this thread some more!!
D:D
-
- 02-10-2010, 02:03 AM #5
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
another question
for an arraylist, if the contents of the arraylist is an object (a class), how do i get a class variable from that class?
so like this is my class
[CODEclass facultytag
{
public String getteachername()
{
return tname;
}
public int getroom()
{
return roomnumber;
}
private String tname;
int roomnumber;
}][/CODE]
that is my arraylistJava Code:Arraylist<faculty> ttags = new Arraylist<faculty>; faculty b = new faculty(); ttags.add(b)
does the int number = ttags.get(i).gettag(); actually get roomnumber? how can i write the program so that i can search through the arraylist trying to find a number called tag? and if i find it, wat code do i use to print the name?Java Code:public void search1(int tag) { int tsize = ttags.size(); for(int i = 0;i<tsize;i++) { int number = ttags.get(i).gettag(); if(number == tag); System.out.println("the name is " + ttags.get(i).getteachername()); } }
- 02-10-2010, 02:04 AM #6
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
another question
for an arraylist, if the contents of the arraylist is an object (a class), how do i get a class variable from that class?
so like this is my class
Java Code:class facultytag { public String getteachername() { return tname; } public int getroom() { return roomnumber; } private String tname; int roomnumber; }that is my arraylistJava Code:Arraylist<faculty> ttags = new Arraylist<faculty>; faculty b = new faculty(); ttags.add(b)
does the int number = ttags.get(i).gettag(); actually get roomnumber? how can i write the program so that i can search through the arraylist trying to find a number called tag? and if i find it, wat code do i use to print the name?Java Code:public void search1(int tag) { int tsize = ttags.size(); for(int i = 0;i<tsize;i++) { int number = ttags.get(i).gettag(); if(number == tag); System.out.println("the name is " + ttags.get(i).getteachername()); } }
-
1) You never did answer my question about the source of your NPE. If you ask a question here, and you find the solution, it's always good form to share your solution with all, especially with anyone who contributed to the thread. It's not a requirement, of course, but it's nice to have resolution and closure for any problem.
2) Please start all class names with capital letters. It may seem trivial to you, but to those of us used to using Java syntax a lot, sticking to conventions means code that is much easier to read and interpret. The faster others can read and understand your code, the quicker they'll be able to help you (or in your teacher's case -- to grade you).
3) Your class of objects held in the ArrayList will need getters and either setters or a constructor that allows parameters. Else you'll never be able to set the int and String fields in your objects.
4) There are several ways to search through collections. For one, if you are often going to be trying to find an object associated with a particular (and unique) int, then consider not using an ArrayList but instead using a different colleciton, a HashMap where the key for the map is an Integer and the value the class you currently have held in your ArrayList. If you can't use a HashMap, then you'll probably need to use a for loop to loop through the arraylist, extracting the object held at each index with a get(i) call, getting the int from this object with it's getter method, as it appears you are doing above.Last edited by Fubarable; 02-10-2010 at 02:32 AM.
- 02-10-2010, 02:34 AM #8
Member
- Join Date
- Feb 2010
- Posts
- 11
- Rep Power
- 0
i wrote the declaration in another class it was stupid..
that is wat im trying to do but is that the right way to get the variables? it doesnt seem to work when i run it. when i run it, i get that the tag number is returned when i wroteany ideas on how to get the name returned? thanksJava Code:System.out.println("the name is " + ttags.get(i).getteachername() );
-
The only stupid error is the ignored or undiscovered error.
If your Faculty class has been properly coded (did you post the entire code for the class? If so, I'm still not sure how you set the faculty name field), then this seems to me how you'd extract the name of the teacher. Perhaps more debugging is in order.that is wat im trying to do but is that the right way to get the variables? it doesnt seem to work when i run it. when i run it, i get that the tag number is returned when i wroteany ideas on how to get the name returned? thanksJava Code:System.out.println("the name is " + ttags.get(i).getteachername() );
- 02-10-2010, 03:19 AM #10
Senior Member
- Join Date
- Mar 2009
- Location
- USA
- Posts
- 127
- Rep Power
- 0
Similar Threads
-
How can i store ArrayList objects in Access database
By frankycool in forum Advanced JavaReplies: 3Last Post: 11-04-2009, 06:55 AM -
Retrieving specific objects from ArrayList
By soketti in forum New To JavaReplies: 49Last Post: 10-29-2009, 07:10 AM -
List of objects
By rekha in forum New To JavaReplies: 6Last Post: 03-20-2009, 11:39 AM -
ArrayList with different objects? Help
By xtrmi in forum New To JavaReplies: 4Last Post: 02-27-2009, 08:51 PM -
How to access ArrayList in List of List?
By alvations in forum New To JavaReplies: 5Last Post: 10-08-2008, 12:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks