Hi guys im kind of new in Java and I am trying to clone a arrayList into another ArrayList that resides in another class, here's the code of the original ArrayList in my TaskPage.java:
public ArrayList<String> tName = new ArrayList<>();
public ArrayList<String> tDesc = new ArrayList<>();
public ArrayList<String> fLName = new ArrayList<>();
public ArrayList<String> tStatus = new ArrayList<>();
public ArrayList<Integer> tDuration = new ArrayList<>();
public ArrayList<String> tID = new ArrayList<>();
String TName = taskName.getText();
tName.add(TName);
String TDescription = taskDescription.getText();
tDesc.add(TDescription);
String FName = FirstName.getText();
String LName = LastName.getText();
fLName.add(FName " " LName);
int Duration = Integer.parseInt(taskDuration.getText());
tDuration.add(Duration);
String taskID;
String taskNumber = "";
Random rand = new Random();
int rNum = rand.nextInt(21);
taskNumber = "0" Integer.toString(rNum);
TaskClass tClass = new TaskClass();
taskID = tClass.createTaskID(TName, taskNumber, FName);
tID.add(taskID);
here is the code for Report.java:
public class Report {
TaskPage tPage = new TaskPage();
public ArrayList<String> taskName;
public ArrayList<String> taskDesc;
public ArrayList<String> fLName;
public ArrayList<String> taskStatus;
public ArrayList<String> taskDuration;
public ArrayList<String> taskID;
public String res = "";
public void init() {
taskName = (ArrayList)tPage.tName.clone();
taskDesc = (ArrayList)tPage.tDesc.clone();
fLName = (ArrayList)tPage.fLName.clone();
taskStatus = (ArrayList)tPage.tStatus.clone();
taskDuration = (ArrayList)tPage.tDuration.clone();
taskID = (ArrayList)tPage.tID.clone();
}
public void statusDone() {
init();
for (int k=0; k <= tPage.tName.size(); k ) {
if (tPage.tStatus.get(k) == "Done") {
res = tPage.fLName.get(k) ", " tPage.tName.get(k) ", " tPage.tDuration.get(k) "\n";
} else {
res = "There is nothing to display here!";
}
}
}
}
And lastly here is the code for my ComingSoon.java:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Report rpt = new Report();
rpt.statusDone();
String Status = rpt.res;
JOptionPane.showMessageDialog(null, Status);
}
When I run the code it gives the error:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at userlogin.Report.statusDone(Report.java:36)
at userlogin.ComingSoon.jButton1ActionPerformed(ComingSoon.java:188)
at userlogin.ComingSoon$1.actionPerformed(ComingSoon.java:54)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
CodePudding user response:
There are a number of problems in your code, but the reason you get an ArrayIndexOutOfBoundsException is that the for loop in the statusDone()
function is wrong.
for (int k=0; k <= tPage.tName.size(); k ) {
...
The problem occurs when k = tPage.tName.size()
.
Since arrays (and ArrayLists) are 0 indexed, the element at k-1
is the last element, so tPage.tStatus.get(k)
throws an ArrayIndexOutOfBoundsException. You should use <
instead of <=
.
Another possible problem is that you don't seem to call init()
anywhere. I assume that init()
should be called before statusDone()
.
Additional suggestions for better readability and maintainability:
Instead of having 6 ArrayLists of strings, make a
Task
class that has 6 fields, and replace the 6 lists withArrayList<Task>
(thanks Dave Newton)Use
new ArrayList<>(tPage.tName)
instead of(ArrayList)tPage.tName.clone()
. Usingclone()
on collections is usually discouraged, and casting it is also not very clean.Consider moving everything from
init()
to the constructor. That way, you don't have to remember to callinit()
; the code just gets run when you callnew Report()