私は、JTextArea に 1 日の割り当てを入力する割り当てプランナーを作成しています。JTextAreas を静的にしたので、クラス外でアクセスできました。コードをクリーンアップするために、for ループを使用し、JTextAreas を静的インスタンス変数配列に入れることにしました。しかし、配列を介して JTextArea にアクセスしようとすると、NullPointerException が発生します。JTextArea の宣言の例を次に示します。
static JTextArea MondayEng;
配列 (サブジェクトごとに 1 つ) は次のように宣言されます。
static JTextArea[] Eng = new JTextArea[]{
MondayEng,
TuesdayEng,
WednesdayEng,
ThursdayEng,
FridayEng
};
そしてJTextArea[][]
、配列を含む があります。
static JTextArea[][] subjs = new JTextArea[][] {Eng, Hist, Math, Sci, Lang};
JTextAreas は、次のように JFrame (PWin と呼ばれる) 内でインスタンス化されます。
MondayEng = new JTextArea();
MondayEng.setBounds(664, 68, 153, 100);
contentPane.add(MondayEng);
次のように JTextAreas の内容を取得しようとします。
for (int i = 0; i < PWin.subjs.length; i++) { // Iterate through subjects
String sub = subjects[i]; // Store the current subject in a variable
for (int y = 0; y < days.length; y++) { // Iterate through days in week
String day = days[y]; // Store the current day in a variable
String str = PWin.subjs[i][y].getText(); // Get the text of the JTextArea currently selected, plop it in a string
これは、ファイルを保存するときの例外です:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PSave.<init>(PSave.java:19)
at PWin$1.actionPerformed(PWin.java:262)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6373)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6138)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Component.dispatchEventImpl(Component.java:4735)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
at java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
どんな助けでも大歓迎です!