Reputation: 351
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++;
}
Upvotes: 1
Views: 2509
Reputation: 182
int index = 1;
TaskObject[] task = new TaskObject[index];
This will create the array of the object of type TaskObject with the size 1. That means it can have only task[0]. If you give task[1] it is array out of bound.
So in the while loop it should be (according to your exp)
index = 0;
System.out.println("This works? 4");
task[index].setTask(rsu.getString("mainTaskString"));
If your query is returning fixed number of rows then it is advisable to use array else use arraylist.
Upvotes: 0
Reputation: 4692
You can define size of Array only once
. So make sure you define it proper otherwise it will throw ArrayIndexOutOfBoundsException
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 your task
array, you will get this exception.
So make sure you specify enough size say 50.
But I would recommend to use List
instead of Array
, as it gives flexibility automatically increase size without specifying size. You can refer Array or List in Java
.
You can make following changes in your code
Declare taskList
instead of task array.
List<TaskObject> taskList = new ArrayList<TaskObject>();
In while
loop create object
of TaskObject, set values
into it and then add
it to taskList.
TaskObject taskObject = null;
while (rsu.next()) {
taskObject = new TaskObject();
taskObject.setTask(rsu.getString("mainTaskString"));
//set remaining value
//Add taskObject in List
taskList.add(taskObject);
}
Upvotes: 2