java - Closing ConfirmDialog from method -


i have class 2 methods. first method invoking confirm dialog , second thread listener waiting complete status. if listener executed, want close confirm dialog. possible?

my code:

public class newnetworkgame implements threadlistener {  readmsg read; network net; // dialog joptionpane canceldialog;  boolean accepted = false; boolean readstopped = false;  public newnetworkgame(network net) {     this.net = net;     read = new readmsg(this.net);     threadhandler rm = read;     rm.addlistener(this);     rm.start();      jpanel cancelconn = new jpanel();     cancelconn.add(new jlabel("waiting host response..."));      // showing dialog     int result = canceldialog.showconfirmdialog(null, cancelconn, "host response", joptionpane.cancel_option);      // client clicked on cancel option while thread still reading host response            if (result == joptionpane.cancel_option && !accepted && !readstopped) {         net.sendreject();        }      if (!readstopped) {         read.interrupt();     }  }  @override public void notifyofthreadcomplete(thread thread) {      readstopped = true;      if (net.getacceptmsg().equals(read.getstr())) {         accepted = true;     }      // closing dialog     canceldialog.setvalue(joptionpane.cancel_option);     // generates: java.lang.nullpointerexception }  } 

i'm getting java.lang.nullpointerexception canceldialog.setvalue(joptionpane.cancel_option) in listener handler. can how close confirm dialog right?

update solution. working code:

public class newnetworkgame implements threadlistener {  readmsg read; network net;  boolean accepted = false; boolean readstopped = false;  final jdialog dialog = new jdialog();   public newnetworkgame(network net) {     this.net = net;     read = new readmsg(this.net);     threadhandler rm = read;     rm.addlistener(this);     rm.start();       jpanel cancelconn = new jpanel();     cancelconn.add(new jlabel("waiting host response..."));     object[] options = {"abort"};      joptionpane.showoptiondialog(dialog, cancelconn, "host response", joptionpane.cancel_option, joptionpane.question_message, null, options, options[0]);              system.out.println("clicked");      if (!accepted && !readstopped) {         system.out.println("aborted");         net.sendreject();        }      if (!readstopped) {         read.interrupt();     }  }  @override public void notifyofthreadcomplete(thread thread) {      readstopped = true;      if (net.getacceptmsg().equals(read.getstr())) {         accepted = true;     }      dialog.setvisible(false);  }  } 

try sample line of code

    @override     public void notifyofthreadcomplete(thread thread) {          readstopped = true;          if (net.getacceptmsg().equals(read.getstr())) {             accepted = true;         }          // closing dialog          if(canceldialog != null)              {         canceldialog.setvalue(joptionpane.cancel_option);          }  } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -