user interface - GUI Loop Issue Java -


i novice trying use basic gui functions make simple question , answer program first 56 elements practice.

however when run code, errors during running of code randomly.

a text box appear no dialogue message name of text box, , x button exit. when press x goes next line in code, other times quits out. hard me reproduce error, since seems happen randomly.

my main method posted below:

import java.math.*; import java.text.decimalformat; import java.io.*; import java.util.*; import javax.swing.joptionpane; import java.util.random;  //this lab study periodc table of elements simple gui output public class practicefiftyeight { public static void main(string[] args) {      string input;      boolean answer;      random myran = new random();      int randint = 0;      //random num 0 - 56      string[] arrayelements = {"hydrogen","helium","lithium","beryllium","boron","carbon","nitrogen","oxygen","fluorine","neon","sodium","mangnesium","aluminium","silicon","phosphorus","sulfur","chlorine","argon","potassium","calcium","scandium","titanium","vanadium","chronium","manganese","iron","cobalt","nickel","copper","zinc",     "gallium","germanium","arsenic","selenium","bromine","krypton","rubidium","strontium","yttrium","zirconium","niobium","molybdenum","technetium","ruthenium","rhodium","palladium","silver","cadmium","indium","tin","antimony","tellurium","iodine","xenon","cesium","barium"};      string[] arrayeleabriv = {"h","he","li","be","b","c","n","o","f","ne","na","mg","al","si","p","s","cl","ar","k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as",     "se","br","kr","rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe","cs","ba"};      int repeat;                  {         randint = myran.nextint(56);          joptionpane.showmessagedialog(null, " sizes of arrays " + arrayelements.length + " " + arrayeleabriv.length);          joptionpane.showmessagedialog(null, " symbol of " + arrayelements[randint]);          input = joptionpane.showinputdialog(null," enter element symbol ( 1 - 56 ) of");          answer = input.equalsignorecase(arrayeleabriv[randint]);          if(answer)         {         joptionpane.showmessagedialog(null, " correct " + arrayelements[randint] + " represented " + arrayeleabriv[randint] );         }         else         {             joptionpane.showmessagedialog(null, " wrong!!!!! " + arrayelements[randint] + " represented " + arrayeleabriv[randint] + " !!!!! " );         }         repeat = joptionpane.showconfirmdialog(null, "press yes repeat, no quit ", "please confirmm", joptionpane.yes_no_option);          }while(repeat == joptionpane.yes_option);     } } 

you should call swing components edt.

and there place nullpointerexception have been thrown, if user chose cancel.

the following code should work without freezing.

import java.text.decimalformat; import javax.swing.swingutilities; import javax.swing.joptionpane; import java.util.random;  //this lab study periodc table of elements simple gui output public class practicefiftyeight {     final static string[] arrayelements = {"hydrogen","helium","lithium","beryllium","boron","carbon","nitrogen","oxygen","fluorine","neon","sodium","magnesium",                                             "aluminium","silicon","phosphorus","sulfur","chlorine","argon","potassium","calcium","scandium","titanium","vanadium",                                             "chromium","manganese","iron","cobalt","nickel","copper","zinc","gallium","germanium","arsenic","selenium","bromine",                                             "krypton","rubidium","strontium","yttrium","zirconium","niobium","molybdenum","technetium","ruthenium","rhodium",                                             "palladium","silver","cadmium","indium","tin","antimony","tellurium","iodine","xenon","cesium","barium"};      final static string[] arrayeleabriv = {"h","he","li","be","b","c","n","o","f","ne","na","mg","al","si","p","s","cl","ar","k","ca","sc","ti","v","cr","mn","fe","co","ni","cu","zn","ga","ge","as",                 "se","br","kr","rb","sr","y","zr","nb","mo","tc","ru","rh","pd","ag","cd","in","sn","sb","te","i","xe","cs","ba"};      public static void main(string[] args) {                      swingutilities.invokelater(new runnable() {             public void run() {                  string input;                  boolean answer;                  random myran = new random();                  int randint = 0;                  //random num 0 - 56                                     int repeat;                  {                     randint = myran.nextint(56);                      joptionpane.showmessagedialog(null, " sizes of arrays " + arrayelements.length + " " + arrayeleabriv.length);                      joptionpane.showmessagedialog(null, " symbol of " + arrayelements[randint]);                      input = joptionpane.showinputdialog(null," enter element symbol ( 1 - 56 ) of");                      if(input != null) { // if user press cancel, input null                         answer = input.equalsignorecase(arrayeleabriv[randint]);                          if(answer) {                             joptionpane.showmessagedialog(null, " correct " + arrayelements[randint] + " represented " + arrayeleabriv[randint] );                         } else {                             joptionpane.showmessagedialog(null, " wrong!!!!! " + arrayelements[randint] + " represented " + arrayeleabriv[randint] + " !!!!! " );                         }                     } else {                         system.out.println("input null: " + input);                     }                      repeat = joptionpane.showconfirmdialog(null, "press yes repeat, no quit ", "please confirmm", joptionpane.yes_no_option);                  } while(repeat == joptionpane.yes_option);             }         });     } } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -