0

I am trying to build an application for which i need to display number of check boxes that depends on the number of input.

For example: If user inputs 100, so i will create 100 check boxes.

Currently in example i am trying to make an array of JCheckboxes of fixed length. But it is giving Null Pointer Exception.

I need to store the value of check boxes because i will perform action accordingly whether a particular check box is selected or not after the user click Button.

   JCheckBox [] chk=new JCheckBox[10];
         for(int i=0;i<10;i++)
         {
             gbc.fill=GridBagConstraints.HORIZONTAL;
             gbc.gridx=0;
             gbc.gridy=i+1;
             JCheckBox xp=chk[i];
             jp.add(xp,gbc);
         }




Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at UI.<init>(UI.java:93)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Prakhar Gurawa
  • 373
  • 3
  • 13

1 Answers1

0

I was trying this code and now it works fine.

 JCheckBox [] chk=new JCheckBox[100];
         for(int i=0;i<100;i++)
         {
             gbc.fill=GridBagConstraints.HORIZONTAL;
             gbc.gridx=0;
             gbc.gridy=i+1;
             chk[i]=new JCheckBox("HAHAHA");
             jp.add(chk[i],gbc);
         }

I just initialized them and not creating individual checkboxes.

Prakhar Gurawa
  • 373
  • 3
  • 13