Reputation: 505
Hello everyone I was trying to add an array of variable length to a JWindow but I am getting a run time error. Want help from here ..
Here is my code :
package components;
import java.util.Calendar;
public class FullDay extends javax.swing.JWindow {
public FullDay() {
initComponents();
}
private void initComponents() {
int i;
//to get the no of days of the current month.
Calendar cal = Calendar.getInstance();
int maxdays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
getContentPane().setLayout(new java.awt.FlowLayout());
//Creating an array of radiobuttons having size equal to the no of days of the current //month
for(i=0; i<maxdays; i++) {
jRadioButton[maxdays] = new javax.swing.JRadioButton();
getContentPane().add(jRadioButton[i]);
}
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FullDay().setVisible(true);
}
});
}
private javax.swing.JRadioButton jRadioButton[];
}
But the error I am getting is :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at components.FullDay.initComponents(FullDay.java:18)
at components.FullDay.<init>(FullDay.java:8)
at components.FullDay$1.run(FullDay.java:30)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Upvotes: 2
Views: 2631
Reputation: 3825
You may want to initialize the i-th field of the array, instead of always initializing the last one:
jRadioButton[i] = new javax.swing.JRadioButton();
instead of:
jRadioButton[maxdays] = new javax.swing.JRadioButton();
Also, as the previous answers stated, create the array itself outside of the loop.
Upvotes: 0
Reputation: 1026
you need to initialise your array of JRadioButton
JRadioButton jRadioButton[] = new JRadioButton[someValue];
Upvotes: 1
Reputation: 57381
Check whether the jRadioButton
array is created before assigning.
Upvotes: 1