2013-03-21 84 views
2

大家好,我試圖給JWindow添加一個可變長度的數組,但我得到一個運行時錯誤。想從這裏幫助..將可變長度的JRadioButtons數組添加到JWindow中

這裏是我的代碼:

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[]; 
} 

但我得到的錯誤是:jRadioButton陣列

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) 

回答

0

的陣列,您可能需要初始化數組的第i個場,而不是總是初始化最後一個:

jRadioButton[i] = new javax.swing.JRadioButton(); 

代替:

jRadioButton[maxdays] = new javax.swing.JRadioButton(); 

另外,正如前面的答案所述,創建數組本身之外的循環。

+0

Hay Steltem請make這個程序工作。我嘗試了一切傢伙。 nt甚至在你的建議後工作... – Mavrick 2013-03-21 10:15:42

1

檢查是否被分配之前創建的。

+0

乾草斯坦尼斯拉夫請讓這個程序工作。我嘗試了一切傢伙。 – Mavrick 2013-03-21 10:13:35

+2

乾草Mavrick請支付我讓這個程序工作或嘗試比一切傢伙更多。 – StanislavL 2013-03-25 05:55:10

1

你需要初始化你一個JRadioButton

JRadioButton jRadioButton[] = new JRadioButton[someValue]; 
+0

請嘗試以下JRadioButton jRadioButton [] =新的JRadioButton [maxdays]; for(i = 0; i 2013-03-21 10:18:17

+3

@ user2194480如果你不能使用這個(或任何其他)答案來自己修復你的代碼,你必須退後一步並重新學習/學習一些java的基礎知識;-) – kleopatra 2013-03-21 11:05:23