0

我正在开发一个简单的系统,该系统从通过一系列按钮事件选择的数组中收集元素,将其复制到一个名为“oneRay”的新数组中,然后循环遍历其元素以测试用户的知识。如果用户从提示中正确识别出答案,则该部分中二维数组的第一个元素变为“”,并使用以下代码跳过:

        do{
        pick = random.nextInt(oneRay.length);
    }while(!(oneRay[pick][0].equals("")));

继续返回上述异常。以下代码将随机创建为相关类的成员:

Random random = new Random()

那么我可能做错了什么?任何帮助将不胜感激。

更新:是的,元素是通过一系列 switch 语句初始化的,这些语句将选定的数组正确地指向 oneRay

错误报告很大,因为它是在我尝试构建 swing UI 时启动的,所以它左右抛出故障

这是我用于初始化的内容:

switch(subject){
    case 1:
        switch(unit){
        case 1: oneRay = new String[Master.SciChemArray_IntroductionANDFirstChapter.length][2];
           for(int i = 0; (Master.SciChemArray_IntroductionANDFirstChapter.length) > i; i++){
               oneRay[i][0] = Master.SciChemArray_IntroductionANDFirstChapter[i][0];oneRay[i][1] = Master.SciChemArray_IntroductionANDFirstChapter[i][1];}
        default: System.exit(0); break;
        } break;
    case 2:
        switch(unit){
        case 1: 
            oneRay = new String[Master.MathaRay_Part2.length][2];
           for(int i = 0; (Master.MathaRay_Part2.length) > i; i++){
               oneRay[i][0] = Master.MathaRay_Part2[i][0]; oneRay[i][1] = Master.MathaRay_Part2[i][1];} break;
        default: System.exit(0); break;
        } break;

创建此设置时,我传递了一个与主题和单元号相对应的数字,然后将其用于复制相关数组,就像这样^^

单元和主题是这样传递的:

         mathstatb1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent l) {
            new SwingImplementation(2, 1);
        }}); 

从而一个按钮发送主题 2 和单元 1

如果有人真的想要它们,这里是错误,现在在底部:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at wbh.SwingImplementation.<init>(SwingImplementation.java:60)
at wbh.matstatMenu$1.actionPerformed(matstatMenu.java:23)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
4

2 回答 2

0

尝试确保所有 oneRay 数组值都正确初始化。

以下行似乎最适合 NullPointer。如果您尝试为 null 调用 .equals() 方法,则会抛出 NullPointer。

oneRay[pick][0].equals("");
于 2013-09-21T20:15:28.740 回答
-1

如果您粘贴整个代码会更清楚。在当前代码中,我看到以下内容可能会引发 nullpointer :

!(oneRay[pick][0].equals(""))

将其更改为

!("".equals(oneRay[pick][0]))
于 2013-09-21T20:08:27.900 回答