Java Swing Null Pointer Exception do-while

时间:2013-09-21 20:05:16

标签: java swing nullpointerexception do-while

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

        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)

2 个答案:

答案 0 :(得分:0)

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

以下行似乎最容易出现NullPointer。如果你试图调用.equals()方法为null,它会抛出NullPointer。

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

答案 1 :(得分:-1)

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

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

将其更改为

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