Results 1 to 10 of 10
  1. #1
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by zhangster View Post
    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?
    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!

  3. #3
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default 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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by zhangster View Post
    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
    What was the problem? Why the NPE?

  5. #5
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default 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]

    Java Code:
    Arraylist<faculty> ttags = new Arraylist<faculty>;
    faculty b = new faculty();
    ttags.add(b)
    that is my arraylist

    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());
    		 }
    		
    	}
    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?

  6. #6
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default 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;
    }
    Java Code:
    Arraylist<faculty> ttags = new Arraylist<faculty>;
    faculty b = new faculty();
    ttags.add(b)
    that is my arraylist

    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());
    		 }
    		
    	}
    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  8. #8
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    i wrote the declaration in another class it was stupid..

    Quote Originally Posted by Fubarable View Post
    1)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.
    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 wrote
    Java Code:
    System.out.println("the name is " + ttags.get(i).getteachername()	);
    any ideas on how to get the name returned? thanks

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by zhangster View Post
    i wrote the declaration in another class it was stupid..
    The only stupid error is the ignored or undiscovered error.

    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 wrote
    Java Code:
    System.out.println("the name is " + ttags.get(i).getteachername()	);
    any ideas on how to get the name returned? thanks
    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.

  10. #10
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    too many experts here..!! i think solution has been achieved.

Similar Threads

  1. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 06:55 AM
  2. Retrieving specific objects from ArrayList
    By soketti in forum New To Java
    Replies: 49
    Last Post: 10-29-2009, 07:10 AM
  3. List of objects
    By rekha in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 11:39 AM
  4. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 08:51 PM
  5. How to access ArrayList in List of List?
    By alvations in forum New To Java
    Replies: 5
    Last Post: 10-08-2008, 12:23 PM

Posting Permissions

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