| | |
im not quite sure what's wrong with my code.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
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
----------------------------------------------
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
Check the API and see if the add method is static or not:
ArrayList.add(person);
Perhaps it should be al.add(person);
ArrayList.add(person);
Perhaps it should be al.add(person);
Check out my New Bike at my Public Profile at the "About Me" tab
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.
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
----------------------------------------------
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
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
![]() |
Similar Threads
- I cant find what is wrong in this code (C++)
- Please help me i dont understand whats wrong with my code (C++)
- what's wrong in this code? (PHP)
- What's wrong with this code? (C#)
- Why won't this code work? (VB.NET)
- What is wrong with this code? (C++)
- What is wrong with this code??? (Visual Basic 4 / 5 / 6)
- Whats wrong with this code (PHP)
- Something wrong with my code, why Junk character appears? (C)
Other Threads in the Java Forum
- Previous Thread: Help...I need Java code.....
- Next Thread: Colour Cube Faces
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint 2dgraphics android api apple applet application applications arguments array arrays automation bank binary bluetooth chat class classes client code collision component database db development draw eclipse eclipsedevelopment error event exception file fractal game givemetehcodez graphics gui helpwithhomework homework html ide image input integer integration j2me jarfile java javadesktopapplications javafx javaprojects jmf jni jpanel julia learningresources linux list loop map method methods mobile netbeans newbie number object oracle print problem program programming project projectideas recursion researchinmotion scanner screen server service set size sms socket sort sorting sql sqlserver state string swing swt tcp test text-file threads time tree web windows






