0

i try create a window but i got the following error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at CreateProject.Window.main(Window.java:20) enter code here

public static void main(String[] args) {
    JFrame frame = new JFrame("gtrged");
  JLabel[] labels=new JLabel[6];

  for (int i=0;i<10;i++)
  {
        labels[i]=new JLabel("Column" + i);
    }

  JTextField[] txt = new JTextField[3];
  JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints cst = new GridBagConstraints();
    JScrollBar vbar=new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 500);
    int f=0;

    for(int i =0 ; i<6 ;i++)
    {
         cst.fill = GridBagConstraints.HORIZONTAL;
         cst.gridx = 0;
         cst.gridy = f;//
         cst.gridwidth = 2;
            panel.add(labels[i],cst);
      for(int j=0 ; j<3 ; j++)
          {       f=f+1;
                 int ks=0;
            cst.fill = GridBagConstraints.HORIZONTAL;
            cst.gridx = 0;
            cst.gridy = f;//
            cst.gridwidth = 2;
                panel.add(txt[ks],cst);
             ks++;
      }
      f=f+1;

    }

    frame.getContentPane().add(vbar, BorderLayout.EAST);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1300,700);
    frame.getContentPane().add(panel);
    frame.setVisible(true);
}

4 Answers4

1

In this piece of code, you are trying to set up 10 labels, but you only make an array of size 6, so it can't create the extra labels.

  for (int i=0;i<10;i++)
  {
        labels[i]=new JLabel("Column" + i);
    }

Take a careful look at the error message, it tells you exactly what is going wrong, and on what line - learning to read this correctly will save you hours of headaches later.

Ewald
  • 5,691
  • 2
  • 27
  • 31
  • i change it but now it gives error Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at CreateProject.Window.main(Window.java:43) – Ayza Ali Khan Apr 09 '15 at 06:23
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Apr 09 '15 at 08:35
1

Here is your code

 for (int i=0;i<10;i++)
{
    labels[i]=new JLabel("Column" + i);
}

You go through 10 times. Your array is declared only to the size of six

JLabel[] labels=new JLabel[6];
3kings
  • 838
  • 2
  • 13
  • 28
  • i change it but now it gives error Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at CreateProject.Window.main(Window.java:43) – Ayza Ali Khan Apr 09 '15 at 06:23
0
for (int i=0;i<10;i++)

You have to change it to

 for(int i = 0; i < 6; i++)

or you have to change the array

JLabel[] labels=new JLabel[10];

the problem is: It try to accesses null memory location.
the following link might help.

http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-arrayindexoutofboundsexception-how-to-handle-array-index-out-of-bounds-exception/

3kings
  • 838
  • 2
  • 13
  • 28
user3710320
  • 333
  • 2
  • 4
  • 9
  • i change it but now it gives error Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at CreateProject.Window.main(Window.java:43) – Ayza Ali Khan Apr 09 '15 at 06:22
  • Can you add some code and stack-trace of the error – user3710320 Apr 09 '15 at 13:54
  • no i does not add the code ,but when i try this code without creating jtextfields,it works fine ,i have now issue that its a right way of creating jtextfield or not – Ayza Ali Khan Apr 09 '15 at 17:23
0

The array labels has size 6, but you try to iterate from 0 to 9, so when you do labels[6] is throws the exception.

JLabel[] labels=new JLabel[6];
for (int i=0;i<10;i++) {
    labels[i]=new JLabel("Column" + i); // this will throw an exception when i == 6
}

Try to use the array length when you can:

for (int i = 0; i < labels.length; i++) {
    labels[i] = new JLabel("Column" + i);
}

This will help preventing a possible ArrayIndexOutOfBoundsException.

Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
  • i change it but now it gives error Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at CreateProject.Window.main(Window.java:43) – Ayza Ali Khan Apr 09 '15 at 03:25