Hi guys, I'm trying to create an array with the length according to the number of rows in my database. So here is my code. I getting "array out of bound".

import java.sql.*;
import javax.swing.*;

public class prg extends javax.swing.JFrame(){

String nameC = "HDB";
String nameCC = "";
int count=0;


public BookPgr(String theUser, int theCount) {
        initComponents();
        connect();
        count = theCount;

        }
Info[] List = new Info[count];

public void connect() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:DB");
            st = con.createStatement();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error connection to DB\n" + ex.getMessage());
        }
    }


public void StoreinArray() {
    try {
            String SQLCMD = "Select * from Table1 ";
            rs = st.executeQuery(SQLCMD);

            while (rs.next())
            {

                for(int x = 0; x<=countBHDB; x++){

                    //name for object array
                    String now = Integer.toString(x);
                    nameCC = now+nameC;

                    Info nameCC = new Info ("t", "d", "c");
                    List[x] = nameCC;
                }
            }
            rs.close();

        } catch (Exception ex) {
            System.out.println("Error " + ex);
        }
    }
}




class Info {
    String Name;
    String Detail;
    String Ability;

    Info(String theName, String theDetail, String theAbility) {
        Name = theName;
        Detail = theDetail;
        Ability = theAbility;
    }
}

where do you set the value for the countBHDB variable? are you sure you don't
mean to do x < countBHDB?

add a printstatement, printing the length of the array List, and the value of countBHDB, that might explain a bit what's going wrong.

x<=countBHDB;

Please post the actual code that gives the error - the mish-mash above won't do anything - and then post the exact complete content of all error messages - including compiler errors - and line numbers

Edited 4 Years Ago by JamesCherrill

import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;

public class test1 extends javax.swing.JFrame {

    Connection con;
    Statement st;
    ResultSet rs;

    String Ability;
    int rowCount;

    String nameC = "HDB";
    String nameCC = "";

    public test1() {
        initComponents();
        connect();
        countRow();
        AddInfo();
    }

    Info[] List = new Info[rowCount];

    public void countRow() {
        try {
            String SQL = "Select * from Table1";
            rs = st.executeQuery(SQL);

            while (rs.next())
            {
                rowCount++;
            }

        } catch (Exception ex) {
            System.out.println("Error count " + ex);
        }
    }

    public void AddInfo() {
        try {

            String SQLCMD = "Select Ability from Table1";
            rs = st.executeQuery(SQLCMD);

            while (rs.next()) //go through each row that your query returns
            {

                Ability = rs.getString("Ability");

                for (int x = 0; x <= rowCount; x++) {

                    String add = Integer.toString(x);
                    nameCC = add + nameC;

                    DesignerInfo nameCC = new DesignerInfo("t", "d", Ability);
                    List[x] = nameCC;
                }
            }

        } catch (Exception ex) {
            System.out.println("Error " + ex);
        }
    }

    public void connect() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:javaMPDB");
            st = con.createStatement();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error connection to DB\n" + ex.getMessage());
        }
    }
    }


    class Info {

    String Name;
    String Detail;
    String Ability;

    Info(String theName, String theDetail, String theAbility) {
        Name = theName;
        Detail = theDetail;
        Ability = theAbility;
    }
}

I cleaned up the code. The error I'm getting is: Error java.lang.ArrayIndexOutOfBoundsException: 0

OK, don't post the full text of error messages including line numbers. We've all got loads of time to double-guess them, so here's my best take:

Info[] List = new Info[rowCount];

creates a new array when the instance is initialised, using the itital value of rowCount, which is zero. So it's an array with no elements.

countRow() sets rowCount whenever it's called, if it's called (you still didn't post all the code, so we don't know), but that can't be before the instance is initialised. so it has no effect on the size of the array.

One could also question why a class that does SQL database stuff extends JFrame - I hope you're not mixing up GUI and database in one big humongous class!

Hey thanks for the reply. I get what you mean. I forget to mention its SQL and Database together. I cant seem to get the rowCount intialized.

I am using Netbeans GIU builder so the rest of the code is less important.That is why I posted the main parts.

This article has been dead for over six months. Start a new discussion instead.