Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

    For my tool I need to locate all of my database list on once JFrame and I succeeded to handle them individually.
    And I want to reduce the code and used for loop replacing duplicate code for all databases required.

    the code is below. Please check and advise why I am getting the mentioned error.

    import javax.swing.*;

    import java.sql.*;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.border.*;
    import java.io.*;
    public class OurTool extends JFrame implements MouseListener
    {
    public static void main(String args[])
    {
    OurTool f=new OurTool();
    f.setSize(2000,1000);
    f.setResizable(false);
    f.setVisible(true);


    f.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent w)
    {
    System.exit(0);
    }
    });
    }
    JButton[] dbBut= new JButton[2];
    String dbList[] = {"DB1","DB2"};
    int dbIndex,butTop;

    public OurTool()
    {
    Container Con = getContentPane();
    Con.setLayout(null);

    for (dbIndex=1,butTop=10;dbIndex<=2;dbIndex++,butTop+= 105)
    {
    dbBut[dbIndex]= new JButton(dbList[dbIndex]);
    Con.add(dbBut[dbIndex]);
    dbBut[dbIndex].setSize(200,100);
    dbBut[dbIndex].setBounds(5,butTop,200,100);
    dbBut[dbIndex].addMouseListener(this);
    }

    }

    public void mousePressed(MouseEvent ME)
    {
    }

    public void mouseClicked(MouseEvent ME)
    {
    JButton B1=(JButton) ME.getSource();

    for (dbIndex=1;dbIndex<=2;dbIndex++)
    {
    if (B1==(JButton) dbBut[dbIndex])
    {
    try
    {
    connectToMyDB(dbList[dbIndex]);
    }catch(SQLException e){}
    catch(ClassNotFoundException e){}
    }
    }
    }
    public void mouseReleased(MouseEvent ME)
    {

    }
    public void mouseExited(MouseEvent ME)
    {

    }
    public void mouseEntered(MouseEvent ME)
    {

    }
    public void connectToMyDB(String mydb) throws ClassNotFoundException,SQLException
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection cn=DriverManager.getConnection("jdbcdbc:mydb","user1","pwd1");
    System.out.println("connected to database..");
    Statement st=cn.createStatement();
    ResultSet rs=st.executeQuery("select * from tab1;");
    while(rs.next())
    {
    System.out.println(rs.getString("Time")+" "+rs.getString("Message"));
    }

    rs.close();
    st.close();
    cn.close();
    }
    }

    --- Update ---

    I changed JButton declaration and am out of ArrayIndexOutOfBoundException now..and got "NullPointerException" now.

    new code is:

    import javax.swing.*;

    import java.sql.*;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.border.*;
    import java.io.*;
    public class OurTool extends JFrame implements MouseListener
    {
    public static void main(String args[])
    {
    OurTool f=new OurTool();
    f.setSize(2000,1000);
    f.setResizable(false);
    f.setVisible(true);


    f.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent w)
    {
    System.exit(0);
    }
    });
    }
    JButton dbBut[];
    String dbList[] = {"DB1","DB2"};
    int dbIndex,butTop;

    public OurTool()
    {
    Container Con = getContentPane();
    Con.setLayout(null);

    for (dbIndex=1,butTop=10;dbIndex<=2;dbIndex++,butTop+= 105)
    {
    dbBut[dbIndex]= new JButton(dbList[dbIndex]);
    Con.add(dbBut[dbIndex]);
    dbBut[dbIndex].setSize(200,100);
    dbBut[dbIndex].setBounds(5,butTop,200,100);
    dbBut[dbIndex].addMouseListener(this);
    }

    }

    public void mousePressed(MouseEvent ME)
    {
    }

    public void mouseClicked(MouseEvent ME)
    {
    JButton B1=(JButton) ME.getSource();

    for (dbIndex=1;dbIndex<=2;dbIndex++)
    {
    if (B1==(JButton) dbBut[dbIndex])
    {
    try
    {
    connectToMyDB(dbList[dbIndex]);
    }catch(SQLException e){}
    catch(ClassNotFoundException e){}
    }
    }
    }
    public void mouseReleased(MouseEvent ME)
    {

    }
    public void mouseExited(MouseEvent ME)
    {

    }
    public void mouseEntered(MouseEvent ME)
    {

    }
    public void connectToMyDB(String recDB) throws ClassNotFoundException,SQLException
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection cn=DriverManager.getConnection("jdbcdbc:mydb","user1","pwd1");
    System.out.println("connected to database..");
    Statement st=cn.createStatement();
    ResultSet rs=st.executeQuery("select * from tab1;");
    while(rs.next())
    {
    System.out.println(rs.getString("Time")+" "+rs.getString("Message"));
    }

    rs.close();
    st.close();
    cn.close();
    }
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

    Hi. Please put your code in code tags next time.

    Search your code for loose values and null. Any value that isn't preset/determined is at hazard for an NPE. I would suggest trying the good old..
    try {
    } catch (Exception e) {
        System.out.println("e");
    }

    When you stop getting tons of errors and your exception is printed out, you might be that much closer to finding your null value

    --- Update ---

    Hi. Please put your code in code tags next time.

    Search your code for loose values and null. Any value that isn't preset/determined is at hazard for an NPE. I would suggest trying the good old..
    try {
    // whatever
    } catch (Exception e) {
        System.out.println("e");
    }

    When you stop getting tons of errors and your exception is printed out, you might be that much closer to finding your null value

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

    Hi pavan, in the first posted example, the array is declared with: JButton[] dbBut= new JButton[2];
    This gives the array two(2) slots, numbered 0 and 1. Then in the loop: for (dbIndex=1,butTop=10;dbIndex<=2;dbIndex++,butTop+= 105)
    This loop will run for <=2, which is 0, 1, and 2. There is no third slot in the array with index number 2. This off-by-one-error is common and understanding the concept of index numbering is necessary.

    In the second posted sample the array is not initialized as it was in the first example, remember: JButton[] dbBut= new JButton[2];

    Browse this tutorial for details, but do try to understand why both examples failed.

Similar Threads

  1. Exception in thread "main" java.lang.NoSuchMethodError: main?
    By Sakharam01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 10th, 2013, 09:06 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 04:40 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 12:22 PM

Tags for this Thread