I have two classes.
In EvidenceBox class, I have a method called getParticularEvidence()
public ArrayList <Evidence> getParticularEvidence(int caseNumber, String evidenceType) {
EvidenceBox e = new EvidenceBox(caseNumber, evidenceType);
return e.getEvidenceByType(caseNumber, evidenceType);
}
What this method is doing doing is that it is taking caseNumber and evidenceType as parameter. Inside this method we are creating an Object instance of EvidenceBox class so that I can call another method called getEvidenceByType() which is in EvidenceBox Class. The whole point of this method is to take those two arguments above and pass it to another method in another class which has an if statement inside for loop that checks if the arguments is equal to something inside an ArrayList called evidenceBoxList, if it is equal then return the list of Evidences that were equal.
When I invoke this in the Main class:
System.out.println(crimeBoxOne.getEvidenceByType(2005000381, "S.Avery"));
What it does, is that it goes to the getEvidenceByType method, and then calls the other method which is in Evidence class:
public ArrayList < Evidence > getEvidenceByType(int keyNumber, String keyName) {
ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
for (Evidence e: setOfEvidence) {
if (this.getCaseNumber() == keyNumber) {
if (this.getCaseName().equals(keyName)) {
System.out.println("Pieces of DNA Evidence in Evidence Box number " + this.getCaseNumber() + " found by " + this.getCaseName() + ": " + this.getEvidence());
getEvidence().add(e);
} else {
System.out.println("Not found!");
}
} else {
System.out.println("Not Found!");
}
}
return getEvidence();
}
The getCaseNumber is the number of the EvidenceBox, and the getCaseName is the name of the evidenceBox
My problem is that The return of the list is always empty, even though it isn't because I added the EvidenceBox to the ArrayList. How can I resolve this problem?
getEvidence()
is the getter method that returns the list of evidenceBox.
Here is my EvidenceBox class:
package looselycoupled;
import java.util.*;
public class EvidenceBox {
private int caseNumber;
private String caseName;
private ArrayList evidenceBoxList;
public EvidenceBox(int caseNumber, String caseName) {
this.evidenceBoxList = new ArrayList < Evidence > ();
this.caseNumber = caseNumber;
this.caseName = caseName;
}
public ArrayList < Evidence > getEvidenceByType(int keyNumber, String keyName) {
ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
for (Evidence e: setOfEvidence) {
if (this.getCaseNumber() == keyNumber) {
if (this.getCaseName().equals(keyName)) {
System.out.println("Pieces of DNA Evidence in Evidence Box number " + this.getCaseNumber() + " found by " + this.getCaseName() + ": " + this.getEvidence());
getEvidence().add(e);
} else {
System.out.println("Not found!");
}
} else {
System.out.println("Not Found!");
}
}
return getEvidence();
}
public void add(Evidence getTypeFromEvidenceClass) {
evidenceBoxList.add(getTypeFromEvidenceClass);
}
public ArrayList < Evidence > getEvidence() {
return evidenceBoxList;
}
public void printRecordsOfTheEvidence() {
Set < Evidence > setOfEvidence = new HashSet < > (evidenceBoxList);
for (Evidence e: setOfEvidence) {
System.out.println(Collections.frequency(evidenceBoxList, e) + "x " + e);
}
}
public int getCaseNumber() {
return caseNumber;
}
public String getCaseName() {
return caseName;
}
@Override
public String toString() {
return caseNumber + " " + caseName;
}
}
and Police class:
package looselycoupled;
import java.util.ArrayList;
public class Cop {
private String name;
//private ArrayList collectionOfEvidences = new ArrayList<>();
private ArrayList<EvidenceBox> collectionOfEvidences = new ArrayList<>();
public Cop(String name) {
this.name = name;
}
public void copTakesAnEvidenceBox(EvidenceBox box) {
collectionOfEvidences.add(box);
}
public ArrayList<Evidence> getParticularEvidence(int caseNumber, String evidenceType) {
EvidenceBox e = new EvidenceBox(caseNumber, evidenceType);
return e.getEvidenceByType(caseNumber, evidenceType);
}
public ArrayList<EvidenceBox> getEvidence() {
return collectionOfEvidences;
}
public String toString() {
return name;
}
}
evidenceBoxList
is private to EvidenceBox
class.
Whenever you are doing EvidenceBox e = new EvidenceBox(caseNumber, evidenceType);
it is creating a new Blank instance of this list with no values.
Next you are iterating this list and trying to add a value using the following piece of code:
ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
for (Evidence e: setOfEvidence) {
if (this.getCaseNumber() == keyNumber) {
if (this.getCaseName().equals(keyName)) {
System.out.println("Pieces of DNA Evidence in Evidence Box number " + this.getCaseNumber() + " found by " + this.getCaseName() + ": " + this.getEvidence());
getEvidence().add(e);
} else {
System.out.println("Not found!");
}
} else {
System.out.println("Not Found!");
}
}
return getEvidence();
}
Now since you initially have no elements in the evidenceBoxList
it will never go in the for
loop and will never call add()
method.
The getEvidence()
method is then returning this blank list to you.
Add getEvidence().add(new Evidence(keyNumber,keyName));
before the for loop to verify the problem.
ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
/* Add Here */
getEvidence().add(new Evidence(keyNumber,keyName));
for (Evidence e: setOfEvidence) {
/* Rest of the code */
Now, it should return you the list with one object.