473,408 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Hi, I've got a problem with an Arraylist!!

Hi, im creating an arraylist of object Person, which contains the information First_name, Surname, but i have come across a problem. This is my code :

import java.util.*;
import java.util.ArrayList.*;

public class People
{

public static void main (String[] args)
{
ArrayList<Person> people = new ArrayList<Person>();
people.add( Person.First_name );
people.add( Person.Surname );


}
}

When I try to compile the code it tells me i cant reference the non-static variable First_name into a static context. I know i'm not allowed to do this but i cant see how to over come it. I cant change either of my methods from static to non-static and visa versa. Any ideas??
Apr 27 '07 #1
4 1102
JosAH
11,448 Expert 8TB
You can only add Persons to that list so you have to create them first as in:
Expand|Select|Wrap|Line Numbers
  1. Person p= new Person("Joe", "Jones");
  2. // you can add 'p' to your list
kind regards,

Jos
Apr 27 '07 #2
You can only add Persons to that list so you have to create them first as in:
Expand|Select|Wrap|Line Numbers
  1. Person p= new Person("Joe", "Jones");
  2. // you can add 'p' to your list
kind regards,

Jos
Thanks for that, but my class person has parameters which allow me to create a new person. I have tried creating a new person through these parameters and then doing:

people.add(person1.First_name);

and i just tried creating a person like yo usuggested.

I still get the error message of non-static cannot be used in a static context.

Any ideas??
Apr 27 '07 #3
JosAH
11,448 Expert 8TB
Any ideas??
Yep, read my previous reply again: create a Person first and add that Person
to your list. You can't add single names to your list nor can you refer to a
non-static object memer (such as "First_name") from a static context where
no Person object is in scope.

kind regards,

Jos
Apr 27 '07 #4
Yep, read my previous reply again: create a Person first and add that Person
to your list. You can't add single names to your list nor can you refer to a
non-static object memer (such as "First_name") from a static context where
no Person object is in scope.

kind regards,

Jos
Oh ok i understand! Sorry! Thanks a lot.

Dave
Apr 27 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bob Weiner | last post by:
What I want to be able to do is create an indexer that can index into an ArrayList filled with objects of my own type. I have the following class structure: ...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
2
by: Dirk | last post by:
Hello The following code does not work ArrayList __gc* allControls = new ArrayList(); ..... // put some Controls in allControls .... Control __gc* arr = dynamic_cast<Control...
2
by: VB Programmer | last post by:
I have an arraylist which holds a custom structure I made. (The structure contains FirstName, LastName, Address and EmpId.) I am trying to loop through the arraylist to write out each item. ...
8
by: Patrick.O.Ige | last post by:
Hi All, I have a code below:- That Binds a DropDownList to a database using ArrayList. This code works well in ASP.NET webMatrix. But when i use it below in VS.NEt it gives me the error:-...
2
by: Bernard Bourée | last post by:
I have defined a Class called EntSor (see code ) with a procedure Sub Définit which assign the values of this class. Then I have defined an Arraylist with Dim colEntSor As New ArrayList() ...
1
by: nickpay | last post by:
Hello, I've trawled through many of the postings on this site but am still unable to find the right solution to removing the XML tag that is created when Serializing using an ArrayList. The...
1
by: nondos | last post by:
I have problem with arraylist the object i get from the array is always the last i'll give example: Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As...
4
by: =?Utf-8?B?R3JlZw==?= | last post by:
Can someone give me e simple example of to populate a combo box / list box using an ArrayList? THanks.
6
by: Slickuser | last post by:
Hi, I am picking up C#.net and I'm trying to add many values to one single key at different time in a loop. If the key already exist, append new value to previous? I'm not sure how to do that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.