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!
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 anArrayList, you can callremove(int).if (list.get(index).toString() == idNumber)this will always evaluate false because you are comparingtoStringinhereted fromObjectto the specific string entered in the ID field by the user. Instead, you need to be checking ID text againstidNumberin eachemployeesobject.nullstrings, 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 toemployeesthat returns a string with all of the fields (which is what I'd recommend)if (list.get(index).toString() == idNumber)a further note with this is that you should useString.equals()to compare strings rather than==. So in this example it would beif(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 inemployeesto returnidNumber