5

I want to know how I can use my object ObjetTWS with the parameter of my function ObjectTWS(). And how I can put the object in a Arraylist or a List.

public class ObjetTWS {

    String nom; 
    List<String> jobAmont; 
    List<String> jobAval; 
    String type;


    public ObjetTWS(String p_nom, String p_type,String p_jobAmont,String p_jobAval){

I already try this but it says ObjetTWS undefined :

ObjetTWS obj  = new ObjetTWS();

obj.nom = p_nom;
obj.jobAmont.add(p_jobAmont);
obj.jobAval.add(p_jobAval);
obj.type = p_type;
3
  • are you initializing the lists somewhere? Commented May 19, 2015 at 7:51
  • Yes but the problem is with the object I have create. The error of this line ObjetTWS obj = new ObjetTWS() is constructor ObjetTWS is undefined. Commented May 19, 2015 at 7:53
  • 2
    @Subas, Attributes (except if it's a particular case) should be all private and they way to access them should be always using getters and setters methods. Have it in mind. Commented May 19, 2015 at 7:58

5 Answers 5

6

You already defined a constructor:

public ObjetTWS(String p_nom, String p_type,String p_jobAmont,String p_jobAval){

That makes the JVM to omit the default constructor, so you must add it manually

public ObjetTWS() {}

Or declare object with given constructor:

ObjetTWS obj = new ObjetTWS(p_nom, p_type,p_jobAmont, p_jobAval);
Sign up to request clarification or add additional context in comments.

Comments

2

Since you have created a constructor of your own in your class with parameter so default constructor will not work at all, so you must have to pass the parameters with your constructor and also initialize the List before adding element to them.

Comments

1

By default, objects have a parameter less constructor (which is the one you are calling in your second code snippet). This however, gets replaced with other constructors when you provide them, which is what you are doing in your first example.

To solve this problems, you have 2 options:

  1. Add a parameter less constructor in your ObjetTWS class: public ObjeTWS() {} //Do any initialization here

  2. In your second code sample, use this: ObjetTWS obj = new ObjetTWS(p_nom, p_type, p_jobAmont, p_jobAval);

Comments

1

First you should initialize the list

public class ObjetTWS {   
    String nom; 
    List<String> jobAmont = new ArrayList<String> (); 
    List<String> jobAval =  new ArrayList<String> (); 
    String type;

Then you try to add elements into it.

And also try to keep your default constructor

As you are overriding it by arguments constructor

public ObjectTWS() {}

1 Comment

Sorry I was in middle of eidting my answer by the time you have commented :)
0

public ObjetTWS(String p_nom, String p_type,String p_jobAmont,String p_jobAval) what you have here is a parameterized constructor. However in the code you are trying to do this ObjetTWS obj = new ObjetTWS();

What it tell us is that you don't have a constructor with no arguments.

So, to be able to do this, you need to add another constructor to your class, which should look like this :

public ObjectTWS() {
    // Any code logic
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.