java.lang.ArrayIndexOutOfBoundsException对象数组

时间:2015-06-09 05:37:03

标签: java sql arrays object exception

您好我正在尝试创建一个程序,从数据库中收集任务并显示它们以进行编辑。有一个问题我不确定如何解决。

我想用数据库中的信息加载几个任务对象。该错误仅在程序实际运行时显示。

这是错误

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)

我的代码

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++;
        }

2 个答案:

答案 0 :(得分:2)

您可以定义数组only once的大小。因此,请确保您正确定义它,否则当您尝试将值大于指定大小的值时,它将抛出ArrayIndexOutOfBoundsException

在您的代码中,您指定的大小为1。因此,当您尝试在task数组中放入超过1个值时,您将获得此异常。

因此,请确保指定的足够大小为50。

但我建议使用List代替Array,因为它可以灵活地自动增加尺寸而不指定尺寸。您可以参考Array or List in Java

您可以在代码中进行以下更改

声明taskList而不是任务数组。

List<TaskObject> taskList = new ArrayList<TaskObject>();

在TaskObject的while循环create object中,set values进入它,然后add进入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);
}

答案 1 :(得分:0)

    int index = 1;            
    TaskObject[] task = new TaskObject[index];

这将创建TaskObject类型的对象数组,大小为1.这意味着它只能有task [0] 。如果您提供任务[1],则数组超出范围

所以在while循环中它应该是(根据你的exp)

  index = 0;
        System.out.println("This works? 4");
        task[index].setTask(rsu.getString("mainTaskString"));

如果您的查询返回固定行数,则建议使用array else使用arraylist。