How can I set the Parameters in this case?

I have 2 entities - tutor and Subject.
At the controller, I have problem with the code where I need to set the tutor to get the list of subjects which are in String.

Here’s the snippet of code in the servlet:
tutor m4 = new tutor();
String[] subj = request.getParameterValues(“subject”);
Listsubjects = new ArrayList(Arrays.asList(subj));

         for(String subject : subjects){ //next() returned showed values
        	 System.out.println(subject);//no explicit return value
        	// m4.setSubject(subject); //setSubject() returned null
        	 //m4.setSubjlist(subjects);
       	 m4.setSubjs((Subject) subjects);

I have tried various ways to set the subject at the class Tutor without avail.

The error message // shows that the tutor is not reading in the values

Here’s how I did the setter at tutor class

public class tutor{
private String tutorName;
tutor subj;
public tutor setSubject(String subject) {
	return this.subj;
	}

My question is how do I write the setter in tutor Class in order for the code to work in the Servlet.
I am using Java EE7, 1.8 Java SE and Tomcat.

Hi, @tangara

I don’t know the full context that you’re operating under, so I’m just making a couple of guesses.

First of all, this part of the code does not do what the function name says it does. setSubject implies that the subject is being set (i.e. assigned, redefined, changed). Instead, it just returns the value of subj to the caller.

    tutor subj;
    public tutor setSubject(String subject) {
        return this.subj;
    }

Do you intend to set the tutor’s subject to be made equal to the passed parameter subject? Then I would think the code inside the function would be different, and also there wouldn’t be anything to return back to the caller.

Second, the type of subj will not hold a subject (assuming subjects are Strings). So to change the code in setSubject to set subj equal to subject, you will need to change the type of subj.

But I note that in the calling code, you are calling setSubject repeatedly with a series of subjects obtained from request.getParameterValues("subject"). If that’s what you want to do, store a collection of subjects for the tutor rather than the single one, the tutor data element subj must be a data type that can hold multiple subjects.

Third – and this is minor but significant – you must initialize your member variables tutorName and subj. The most common way to do that is via tutor’s constructor, but you can also define them when declaring them, like this…

public class tutor {
    private String tutorName = /* code for initial tutor name definition */;
    SomeTypeNameHere subjectCollection = /* code for initial subject collection definition */;
    ... code for rest of class ...

Lastly – and this is a nitpick but will make your code more readable – the standard in Java is for class names to be capitalized. That is, tutor should be Tutor, like so…

public class Tutor {
    private String tutorName = /* code for initial tutor name definition */;
    private SomeTypeNameHere subjectCollection = /* code for initial subject collection definition */;
    public void setSubject(String subject) {
        /* code some operation here with subjectCollection and subject */
    }
}
//