tags:

views:

181

answers:

6

This is a part of my source (when I enter a button) of my JDilog form. I have made an object from the SystemManagement class which is management (I have written also some part of this class). When the user add her/his information, with the addStudent() method in SystemManagement class I add her/his information to the student's list but when I write

System.out.println(management.getField());

It will return null! Why?

 private void submit() {
    String name = nameField.getText();
    String family = familyField.getText();
    String ye =  (String) yearComboBox.getSelectedItem();
    String ci = (String) cityComboBox.getSelectedItem();
    String fi = (String) fieldComboBox.getSelectedItem();
    if (ye == null) {
        JOptionPane.showMessageDialog(this, "You haven't chosen the year of entrance", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (ci == null) {
        JOptionPane.showMessageDialog(this, "You have not set the city", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (fi == null) {
        JOptionPane.showMessageDialog(this, " You have not set the field", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (name.equals("")) {
        JOptionPane.showMessageDialog(this, "You have not set your name ", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (family.equals("")) {
        JOptionPane.showMessageDialog(this, " You have not set the family", "Error ", JOptionPane.ERROR_MESSAGE);
        return;
    }
    management.addStudent(name, family, ye,ci, fi);
    System.out.println(management.getField());
    JOptionPane.showMessageDialog(this, management.toString(), " registered successefully", JOptionPane.INFORMATION_MESSAGE);
    clear();

}

public class SystemManagement implements Serializable {

private String siteName;
private List<Fields> fields;
private List<Professors> professors;
private List<Students> students;
private List<Lessons> lessons;
public Professors pro;
public Lessons les;


public SystemManagement(String siteName) {
    this.siteName = siteName;
    professors = new ArrayList<Professors>();
    students = new ArrayList<Students>();
    lessons = new ArrayList<Lessons>();
    fields = new ArrayList<Fields>();
}

public List<Fields> getFields() {
    return fields;
}

public void setField(String field) {
    this.field = field;
}

public String getField() {
    return field;
}


public void addStudent(String name, String family, String yearOfEntrance, String city, String field) {
    Students student = new Students(name, family, yearOfEntrance, city, field);
    students.add(student);

}
+1  A: 

Well with the additional source code of the management class provided, the getField() method will always return an empty ArrayList. If you call addStudent, a new student will be added to the students list, but the fields ArrayList will not be modified (based on the code given). So, the getField method will always return an empty result.

Olli
so what should I do???
Johanna
A: 

Hello.

I don't see getField() method in your SystemManagement class

Luno
sorry,I edited my code !!
Johanna
A: 

You should increase consistency - is the content of the List structure related to the values you want to access with getField() and setField(String field) and related to the field parameter you use to create a Student?

Right now your code doesn't compile unless you define a class member 'String field'.

If you want to get the field value you just set with the submit method, then you have to get it from the Student. That's a problem right now - easiest fix:

Student added = management.addStudent(name, family, ye,ci, fi);
System.out.println(added.getField());

assuming Student has a getter for the field value, and in the management class:

public Student addStudent(String name, String family, String yearOfEntrance, String city,     String field) {
    Students student = new Students(name, family, yearOfEntrance, city, field);
    students.add(student);
    return student;    
}

But you still have to solve the 'Field' problem (consistency as mentioned above)

Andreas_D
A: 

but when I write

System.out.println(management.getField());

It will return null! Why?

Because you have not set a value to the field attribute.

Somehow you manage to mess up the code. What you posted, although interesting, doesn't contain any reference the the field attribute, nor shows where it is being assigned a value.

The closest thing is the addStudent method

public void addStudent(String name, String family, String yearOfEntrance, String city,    String field) {
    Students student = new Students(name, family, yearOfEntrance, city, field);
    students.add(student);

}

But, that doesn't set the value to the field attribute.

To solve your problem search for the field attribute and find out where are you setting a value.

Search for the string:

field =

The method being executed is:

public String getField() {
    return field;
}

The only way for it to return null would be that field is null. Search for the assignation and you may find the answer.

OscarRyz
A: 

The simplest way to figure out what's wrong is for you to run your code in debug mode in your IDE's debugger, and follow the program flow, inspect the variables, and the error should become obvious.

There's some excellent video tutorials on using the Eclipse debugger here, but even if you are not using Eclipse the general concepts are all relevant.

JRL
A: 

A few general comments that might be of help.

You seem to have a getField() and and a getFields() method. One presumably is intended to return your list of fields but I am unsure as to what you expect the other to return. Do you want a specific field for a particular student? In which case you would need to pass in some sort of parameter to the method (such as student name) and then you could find a specific field to return.

When you add a student using your addStudent() method then you need to keep the rest of your lists up to date as well. If you want to maintain a master list of all fields then you will need to add to that (if necessary - don't need to add any duplicates) when adding a new student.

I hope the above is of help.

chillysapien