【发布时间】:2013-09-21 20:05:16
【问题描述】:
我正在开发一个简单的系统,该系统从通过一系列按钮事件选择的数组中收集元素,将其复制到一个名为“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;
在创建此设置时,我传递了一个对应于主题和单元号的数字,然后用于复制相关数组,就像这样^^
Unit 和 Subject 是这样传递的:
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)
【问题讨论】:
-
oneRay及其元素是否已初始化? -
我需要查看所有代码和整个异常报告。
-
如果你得到 NPE 显然没有初始化。发布您的相关代码。
-
你在switch case之前初始化oneRay为null了吗?
标签: java swing nullpointerexception do-while