Salve a tutti, sono nuva di questo forum e soprattutto sono alle prime armi con la programmazione java.
Sto cercando di creare un piccolo giochino,un quiz, che utilizza le interfaccie grafiche e l'architettura adeventi,e stò trovando delle difficoltà, perchè quando lo avvio parte tranquillamente ma quando inserisco il nome utente per iniziare il gioco il programma si blocca e mi da svariati errori. Vi lascio qui sotto i codici delle clssi S-Quiz e Question, ma è solo in quest'ultimo che il compilatore mi trova gli errori.![]()
Questi sono gli errori che riscontro:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at Questions.MakeQandA(Question.java:142)
at SQuiz.GameStart(SQuiz.java:160)
at SQuiz.newGame(SQuiz.java:156)
at SQuiz$3.actionPerformed(SQuiz.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton. java:374)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Bas icMenuItemUI.java:829)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(BasicMenuItemUI.java:873)
at java.awt.Component.processMouseEvent(Component.jav a:6108)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:587 3)
at java.awt.Container.processEvent(Container.java:210 5)
at java.awt.Component.dispatchEventImpl(Component.jav a:4469)
at java.awt.Container.dispatchEventImpl(Container.jav a:2163)
at java.awt.Component.dispatchEvent(Component.java:42 95)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4461)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4125)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4055)
at java.awt.Container.dispatchEventImpl(Container.jav a:2149)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:42 95)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 604)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)
Vi ringraio per l'attenzione e spero di essere stata chiara!![]()
codice:import java.util.Random; import java.util.StringTokenizer; import java.io.*; /** *The class that contain the questions */ class Questions { Player player; private int totQ; int qNum[] = new int[15]; String questions[][]; String answer[][]; String exactly[][]; FileInputStream db, dbNum, dbTemp; PrintStream dbTemp2; /** *The constructor of class Questions */ Questions() { int k = 1, o; //Update dbNum.txt and take the total questions. for (k = 1 ; k < 16 ; k++) { try { dbTemp = new FileInputStream("/home/piccettina/workspace/Databases/database" + Integer.toString(k) + ".txt"); do { o = dbTemp.read(); if (o != -1) { if ((char) o == ';') totQ++; } } while (o != -1); } catch (IOException ioe) {} } } /** *The constructor of class Questions */ Questions(Player p) { int k = 1, o; byte y; int count[] = new int[15]; player = p; //Update dbNum.txt for (k = 1 ; k < 16 ; k++) { try { dbTemp = new FileInputStream("/home/piccettina/workspace/Databases/Databases/database" + Integer.toString(k) + ".txt"); do { o = dbTemp.read(); if (o != -1) { if ((char) o == ';') count[k - 1]++; } } while (o != -1); } catch (IOException ioe) {} } try { dbTemp2 = new PrintStream("/home/piccettina/workspace/Databases/dbNum.txt"); for (k = 0 ; k < count.length ; k++) { dbTemp2.printf("%d", count[k]); if (k != (count.length - 1)) dbTemp2.printf("%c", ','); } } catch (IOException ioe) {} //Update qNum based on number on dbNum.txt try { dbNum = new FileInputStream("/home/piccettina/workspace/Databases/dbNum.txt"); int i = 0, a = 0; String str = ""; StringTokenizer stNum; do { i = dbNum.read(); if (i != -1) str += (char) i; } while (i != -1); stNum = new StringTokenizer(str, ",", false); while (stNum.hasMoreTokens() && stNum.countTokens() > 1) { qNum[a] = Integer.parseInt(stNum.nextToken()); a++; } dbNum.close(); } catch (IOException ioe) { } catch (NumberFormatException nfe) {} questions = new String[15][qNum[player.getCurrentQ() - 1]]; answer = new String[15][qNum[player.getCurrentQ() - 1] * 4]; exactly = new String[15][qNum[player.getCurrentQ() - 1]]; } /** *This function make a casual number adapt to take a casual question. *@return casual int number */ final long getCasualNumber() { Random a = new Random(); try { return a.nextInt(qNum[player.getCurrentQ() - 1]); } catch (ArrayIndexOutOfBoundsException aioobe) { return -1; } catch (IllegalArgumentException iae) { try { return a.nextInt(qNum[player.getCurrentQ() - 1] + 1); } catch (ArrayIndexOutOfBoundsException aioobe) { return -1; } } } /* public static int getPreviousQ(int level) { int pq = 0; //Previous Questions for (int a = 0 ; a < level ; a++) pq += qNum[a]; return pq; }*/ /** *This function make the questions and the relatives answers. *@params q - the "questions" object. */ void MakeQandA(Questions q) { int file =1; int i = 0; int numST = 0; int qu = 0, an = 0, ex = 0; //questions, answer, exactly String totStr = "", tempStr = ""; StringTokenizer st; //Open the files while (file < 16) { qu = 0; an = 0; ex = 0; totStr = ""; tempStr = ""; numST = 0; i = 0; try { db = new FileInputStream("/home/piccettina/workspace/Databases/database" + Integer.toString(file) +".txt"); } catch (FileNotFoundException fnfe) { System.out.println("Error: " + fnfe); } //Read the file and copy it into totStr String variable. do { try { i = db.read(); if (i != -1) totStr += (char) i; } catch (IOException ioe) {} } while (i != -1); //Use StringTokenizer to take the question and the relatives answers st = new StringTokenizer(totStr, ";-_\n", false); while (st.hasMoreTokens() && st.countTokens() > 1) { switch (numST) { case 0: q.questions[file+1][qu++] = st.nextToken(); numST++; break; case 1: case 2: case 3: case 4: q.answer[file - 1][an++] = st.nextToken(); numST++; break; case 5: q.exactly[file - 1][ex++] = st.nextToken(); numST = 0; break; } } try { db.close(); } catch (IOException ioe) {} file++; } } public int getTotQ() { return totQ; }]

Rispondi quotando