Hi I am trying to create a program that collects tasks from a database and displays them for editing. There is an issue that I am not sure how to resolve.
I want to load the several task objects with info from the database. The error only shows up when the program is actually ran.
This is the error:
Please enter a corresponding number: 2
This works? 1
5.6.25-log
This works? 2
This works? 3
This works? 4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at mytaskapp.DatabaseConnectTaskSelect.Connect(DatabaseConnectTaskSelect.java:65)
at mytaskapp.Display.TaskOutput(Display.java:175)
at mytaskapp.Display.StartScreen(Display.java:51)
at mytaskapp.MyTaskApp.main(MyTaskApp.java:16)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
My code is
public static void Connect()
{
Connection con = null;
Statement st = null;
ResultSet rs = null;
Statement stu = null;
ResultSet rsu = null;
PreparedStatement pst = null;
String url = "jdbc:mysql://localhost:3306/tasks";
String user = "root";
String password = "cinder";
try {
int index = 1;
TaskObject[] task = new TaskObject[index];
System.out.println("This works? 1");
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println(rs.getString(1));
}
System.out.println("This works? 2");
pst = con.prepareStatement("SELECT mainTaskString, timeCreated, dateCreated, timeDue, dateDue, "
+ "notes, project, catagory, taskLength, priority, remind1TimeDue, remind1DateDue, "
+ "remind2TimeDue, remind2DateDue FROM usertasks");
rsu = pst.executeQuery();
System.out.println("This works? 3");
while (rsu.next()) {
index = 1;
//task[index] = new TaskObject(index);
System.out.println("This works? 4");
task[index].setTask(rsu.getString("mainTaskString"));
System.out.println("This works? 4.1");
task[index].setDateCreated(rsu.getString("timeCreated") + " " + rsu.getString("dateCreated"));
System.out.println("This works? 4.2");
task[index].setDateDue(rsu.getString("timeDue") + " " + rsu.getString("dateDue"));
System.out.println("This works? 4.3");
task[index].setNotes(rsu.getString("notes"));
task[index].setProject(rsu.getString("project"));
task[index].setCatagory(rsu.getString("catagory"));
task[index].setTaskLength(rsu.getInt("taskLength"));
task[index].setPriority(rsu.getInt("priority"));
task[index].setRemind1DateDue(rsu.getString("remind1TimeDue") + " " + rsu.getString("remind1DateDue"));
task[index].setRemind2DateDue(rsu.getString("remind2TimeDue") + " " + rsu.getString("remind2DateDue"));
System.out.println("This works? 5");
System.out.println("Your Task: " + task[index].getTask() + "\n" + " Date Created: " + task[index].getDateCreated() + " Date Due: " + task[index].getDateDue() +
" Your Notes: " + task[index].getNotes() + "\n" + " Project: " + task[index].getProject() + " Catagory: " + task[index].getCatagory() + " The minutes needed to complete: " + task[index].getTaskLength() +
" Priority: " + task[index].getPriority() + " Reminder 1: " +
task[index].getRemind1DateDue() + " Reminder 2: " + task[index].getRemind2DateDue() + "\n");
System.out.println("This works? 6");
//index++;
}
You can define size of Array
only once
. So make sure you define it proper otherwise it will throwArrayIndexOutOfBoundsException
when you try to put values more then specified in the size.In your code you have specified size as
1
. So when you try to put more then 1 value in yourtask
array, you will get this exception.So make sure you specify enough size say 50.
But I would recommend to use
List
instead ofArray
, as it gives flexibility automatically increase size without specifying size. You can referArray or List in Java
.You can make following changes in your code
Declare
taskList
instead of task array.In
while
loopcreate object
of TaskObject,set values
into it and thenadd
it to taskList.