3

I am currently working on a school project where we are learning to use Arrays of Objects. I have come across two separate issues along the way which I would like to address. I am using the NetBeans IDE with all code I mention below.

Firstly, after I have added values to the array, I attempt to use a list button which should display the values in a jtextArea. There are no noticeable errors that pop-up although, all the values say "null" instead of the user inputted values they should say. Since there are 5 values the program displays "null null null null null". I am not sure as to why this is and any input would be appreciated.

Secondly, I am attempting to allow the user to remove information based on one of the 5 values which have been stored. In the case of this program the user must enter the "Employee ID" to remove all of said employee's data from the program list. I am having trouble doing this as you will be able to see in my code below.

So to sum things up: How can I correct this random "null" error so that the imputed data is displayed? How can I effectively remove the values of a given index based on user input for the idNumber?

Things to keep in mind: - I need to use an array of objects - I am fairly new to coding so please excuse my possible ignorance - Anything that appears to be missing is likely included in the NetBeans code and therefore may not have been included, this is the main code of concern in my mind

Here's the code:

//ArrayList of Objects Declared
ArrayList <employees> list = new ArrayList <employees>();

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

        employees e;

        //Declaring Strings
        String idNumber, firstName, lastName, annualSalary, startDate;

        //Reads the input fields
        idNumber = idInput.getText();
        firstName = fNameInput.getText();
        lastName = lNameInput.getText();
        annualSalary = annualInput.getText();
        startDate = startDateInput.getText();

        //Sends input information as a class
        e = new employees(idNumber, firstName, lastName, annualSalary, startDate);

        //If data is missing, the user will receive and error message
        if (idNumber.isEmpty()||firstName.isEmpty()||lastName.isEmpty()||annualSalary.isEmpty()||startDate.isEmpty()){
            infoOutput.setText("Please fill out all catagories!");
        }

        //Otherwise, the information provided will be stored
        else {
        list.add(e);
        infoOutput.setText("");
        infoOutput.setText("Employee data added!");   
        } 
}                                         

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

        //Reset infoOutput
        infoOutput.setText("");

        employees e;

        //Declaring Strings
        String idNumber, firstName, lastName, annualSalary, startDate;

        //Reads the input fields
        idNumber = idInput.getText();
        firstName = fNameInput.getText();
        lastName = lNameInput.getText();
        annualSalary = annualInput.getText();
        startDate = startDateInput.getText();

        //Sends input information as a class
        e = new employees(idNumber, firstName, lastName, annualSalary, startDate);

        //Determines if the requested employee exists in the list
        for (int index = 0; index < list.size(); index++) {

            //If the employee ID is found, their information is removed
            if (list.get(index).toString() == idNumber){
                infoOutput.setText("Employee data removed!");
                list.remove(index);
            }

            //Otherwise, an error message is displayed to the user
            else{
                infoOutput.setText("The employee ID " + idNumber + " was not found!");
            }
        }

}                                            
class employees {
        String idNumber, firstName, lastName, annualSalary, startDate;

        employees(String idNumber, String firstName, String lastName, String annualSalary, String startDate) {

            idNumber = idInput.getText();
            firstName = fNameInput.getText();
            lastName = lNameInput.getText();
            annualSalary = annualInput.getText();
            startDate = startDateInput.getText();
        }
}
private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

    //Reset temp
    String temp = "";

    //List all of the stored data
    for (int x = 0; x <= list.size()-1; x++) {
        temp = temp + list.get(x).idNumber + " "
                + list.get(x).firstName + " "
                + list.get(x).lastName + " "
                + list.get(x).annualSalary + " "
                + list.get(x).startDate + "\n";
    }
    outputField.setText(temp);
}    

Thanks for any help!

7
  • An array or an ArrayList? Arrays aren't resizable, so you'd need to create another array that's one element smaller and copy the values you want to keep. If it's an ArrayList, you can call remove(int). Commented Jan 16, 2016 at 1:38
  • 1
    if (list.get(index).toString() == idNumber) this will always evaluate false because you are comparing toString inhereted from Object to the specific string entered in the ID field by the user. Instead, you need to be checking ID text against idNumber in each employees object. Commented Jan 16, 2016 at 1:57
  • As for the null strings, I suspect the outer class is unable to see the fields of the inner class (employees). To resolve this, either change the visibility of the variables or add a method to employees that returns a string with all of the fields (which is what I'd recommend) Commented Jan 16, 2016 at 2:10
  • if (list.get(index).toString() == idNumber) a further note with this is that you should use String.equals() to compare strings rather than ==. So in this example it would be if(list.get(index).idNumber.equals(idNumber)) This likely won't work for the same reason that your strings print null, so I'd suggest adding another method in employees to return idNumber Commented Jan 16, 2016 at 2:24
  • @CalvinP. I am quite new to this. Do you mind explaining how you would go about adding the method to return the string with all fields? Commented Jan 16, 2016 at 5:25

2 Answers 2

2

The constructor for employees does not initialize the instance variables, it initializes the parameters. For example, initialize the idNumber as

this.idNumber = idNumber;

Do similar statements for the remaining instance variables. This is why all the values in the employee class are null.

When testing for the idNumber, you are comparing the toString() of an employee with an idNumber. You must compare the idNumber of the employee in the list to the idNumber from the UI.

if (list.get(index).idNumber.equals(idNumber)) {

It would be better to create an accessor for each property in the employee class.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This fixed my null issue.
0

Here is the employees class with a getData method as I described in comments. (I also changed the constructor as corrected by @downyt)

class employees {
    String idNumber, firstName, lastName, annualSalary, startDate;
    employees(String idNumber, String firstName, String lastName, String annualSalary, String startDate) {
        this.idNumber = idNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.annualSalary = annualSalary;
        this.startDate = startDate;
    }

    String getData(){
        return idNumber + " " + firstName + " " + lastName + " " + annualSalary + " " + startDate;
    }
}

Using this method, the print code would change to something akin to this:

for (int x = 0; x < list.size(); x++){
    temp = list.get(x).getData() + "\n";
}
outputField.setText(temp);

If you so desired, you could also put "\n" in getData(), changing temp to

temp += list.get(x).getData();

1 Comment

It makes little difference, but also notice the use of x<list.size() as opposed to x<=list.size()-1. Both achieve the same result, but if you can make your code cleaner and more concise, there's no reason not to :)

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.