im not quite sure what's wrong with my code.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 2
Reputation: roadrage75 is an unknown quantity at this point 
Solved Threads: 0
roadrage75 roadrage75 is offline Offline
Newbie Poster

im not quite sure what's wrong with my code.

 
0
  #1
May 20th, 2008
Hello all, im rather confused about what's wrong with my code.... it says non-static method add(E) cannot be referenced from a static context, yet i can't see any reference to a static context....

here is my code; thanks in advance!

import java.util.*;
public class person {

public String name[];
public int phone[];
public String address[];
public String email[];
public person (String name, int phone, String address, String email)
{

String[] person = { "Josh", "Sam", "Paul", "Peter" };
ArrayList<String> al = new ArrayList<String>(Arrays.asList(person));
ArrayList.add(person);
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: im not quite sure what's wrong with my code.

 
0
  #2
May 20th, 2008
Use the variable al not the Type/Class ArrayList when you call add.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: im not quite sure what's wrong with my code.

 
0
  #3
May 20th, 2008
Check the API and see if the add method is static or not:
ArrayList.add(person);
Perhaps it should be al.add(person);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 2
Reputation: roadrage75 is an unknown quantity at this point 
Solved Threads: 0
roadrage75 roadrage75 is offline Offline
Newbie Poster

Re: im not quite sure what's wrong with my code.

 
0
  #4
May 20th, 2008
thanks for the suggestion, it partially cleared the problem.... however now it says:
Error: cannot find symbol
symbol : method add(java.lang.String[])
location: class java.util.ArrayList<java.lang.String>

and i don't really understand why it is saying this...
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: im not quite sure what's wrong with my code.

 
0
  #5
May 20th, 2008
Because you're attempting to add the entire array. It doesn't work that way. Why don't you crack open the API documentation?

You're going to need to loop through that array and add each of the Strings separately. Or, crack open the API documentation and see if you can't figure out how to use a combination of an addAll call in ArrayList with an asList call from Arrays.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,713
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: im not quite sure what's wrong with my code.

 
0
  #6
May 20th, 2008
Because you have declared ArrayList to take Strings and you pass as parameter in the add() an array. Decide what you want, arrays or Strings and make the appropriate declaration
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC