How to add things to a class that is exending arraylist

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 770 Views - Last Post: 07 May 2011 - 12:17 PM Rate Topic: -----

#1 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

How to add things to a class that is exending arraylist

Posted 06 May 2011 - 02:20 PM

I have a class called People which extends ArrayList.

I have a method in People class that reads in a file and creates an object. I want to add the object to my People ArrayList, but when i put People.add(object); it doesn't work. Am i suppose to make a constructor that creates an arraylist? I am very lost.
Is This A Good Question/Topic? 0
  • +

Replies To: How to add things to a class that is exending arraylist

#2 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 02:21 PM

Posting your code would certainly help.
Was This Post Helpful? 0
  • +
  • -

#3 lockon777  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-May 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 02:34 PM

there is alot of ways to do this. and a bunch of errors if you cant put up your program at least describe the error how java does :whatsthat:
Was This Post Helpful? 0
  • +
  • -

#4 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 02:41 PM

I can't post the code because it is against the assignment.

but this is the error i get:
cannot make a static reference to the non-static method add(object) from the type arraylist.


this is when i try to do People.add(my_object).

Right now all i have is the method that adds the object, and i have a class for my_object which creates an object. That is all that I have, and I am trying to add my_object to my People ArrayList.

This post has been edited by paki123: 06 May 2011 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:09 PM

By the way, I am instatiating my object inside of my method, there is no instance of it outside of the realm of that method.
Was This Post Helpful? 0
  • +
  • -

#6 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:23 PM

If anyone of you have AIM or TeamViewer, I can show you my code. I just can't post it on here!
Was This Post Helpful? 0
  • +
  • -

#7 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:33 PM

Alright I think I found the problem... I think.

I am using .add method which is static, I think I am suppose to add my object in a non-static way... Is there a way to do this?
Was This Post Helpful? 0
  • +
  • -

#8 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:37 PM

Well the error says you're referencing a non-static variable from a static method. Try making the variable static or the other way around.
Was This Post Helpful? 0
  • +
  • -

#9 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:39 PM

how do i do that?
Was This Post Helpful? 0
  • +
  • -

#10 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:41 PM

Your People list. Put static before it. But still we can't help much without code. Also realise what static means. Static means beloning to a class, while non-static means beloning to a particular instance of the class.
Was This Post Helpful? 0
  • +
  • -

#11 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:46 PM

I removed the code.

This post has been edited by paki123: 06 May 2011 - 04:00 PM

Was This Post Helpful? -4
  • +
  • -

#12 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:51 PM

Ok, I'm confused now. Your first post you talked about a People class with problems with a static method. Now I see code for a class called Server with nothing static in it. What is the problem you're having?
Was This Post Helpful? 0
  • +
  • -

#13 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 03:54 PM

View PostRenagado, on 06 May 2011 - 03:51 PM, said:

Ok, I'm confused now. Your first post you talked about a People class with problems with a static method. Now I see code for a class called Server with nothing static in it. What is the problem you're having?


The problem is that when I do Server.add(mv) i get that message.

and there was an error, tehre should have been a new object in that section

This post has been edited by paki123: 06 May 2011 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#14 paki123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 88
  • Joined: 18-February 11

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 04:26 PM

How am I suppose to add something to it without instatiating the class?

Say i have a class called People which extends an ArrayList.

so
Code:
public class People extends ArrayList

Part of my assignment is to add a string that i get while using a method which reads code and instatiates string objects inside a while loop.


AFter I instatiate a string, I'm suppose to add it to the People ArrayList within the People class. How would i do this?



EDIT: NEVER MIND I GOT IT! you are suppose to do this.add(object);

This post has been edited by paki123: 06 May 2011 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

Re: How to add things to a class that is exending arraylist

Posted 06 May 2011 - 04:29 PM

why are you extending arraylist in first place?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2