1

我正在制作一个作业计划器,其中一个人在 JTextArea 中输入当天的作业。我将 JTextAreas 设为静态,因此我可以在课堂外访问它们。为了清理我的代码,我决定使用 for 循环并将 JTextAreas 放入静态实例变量数组中。但是,当我尝试通过数组访问 JTextArea 时,我得到了 NullPointerException。下面是一个 JTextArea 声明的例子:

static JTextArea MondayEng;

数组(每个主题一个)声明如下:

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)

任何帮助将不胜感激!

4

3 回答 3

2

您的问题似乎是在您创建数组时,JTextAreas 尚未构建。Java 是按值传递的,因此当您传入稍后要构造的 JTextAreas 的名称时,它与传递 null 相同。

于 2012-05-02T18:03:39.070 回答
1

你需要替换这个:

MondayEng = new JTextArea();
MondayEng.setBounds(664, 68, 153, 100);
contentPane.add(MondayEng);

和:

Eng[0] = new JTextArea();
Eng[0].setBounds(664, 68, 153, 100);
contentPane.add(Eng[0]);

否则,数组Eng仍然包含nulls,而您只是在创建一个新JTextArea对象并MondayEng引用该对象而不是指向所Eng[0]指向的对象。

但是,更正确的答案是在将JTextAreas 传递给数组之前构建它们。就像是:

MondayEng = new JTextArea();
MondayEng.setBounds(664, 68, 153, 100);
contentPane.add(MondayEng);

// ...

static JTextArea[] Eng =  new JTextArea[]{
    MondayEng,
    TuesdayEng,
    WednesdayEng,
    ThursdayEng,
    FridayEng
};
于 2012-05-02T18:17:52.120 回答
1

由于 JTextArea[][] subjs 是一个二维数组,因此您需要注意正确生成数组。您创建的方式是一维数组。理想情况下,您可能需要创建类似

JTextArea[][] subs = new JTextArea[][]{ {Eng, Hist, Math, Sci, Lang} };
于 2012-05-02T18:49:17.967 回答