java - Custom addToDisplay method returns exception in another class -


a newcomer here. having trouble method prints text jtextarea.

the addtodisplay method works fine in native gamewindow class. here is:

public class gamewindow extends jframe implements keylistener {     jtextarea displayarea;      public gamewindow() {         ...         this.addtodisplay(""); //works fine     }     public void addtodisplay(string newstring) {         displayarea.append("\n" + newstring);         displayarea.selectall();     } } 

when method called in class of same package, throws java.lang.nullpointerexception

public class duelist {     private gamewindow window;      public duelist(string n) {         ...     }      public void dueling(duelist opponent) {         ...         window.addtodisplay(""); //exception occurs here     } } 

please show me how rid of exception. if need more information, let me know in comments.

you have initialize gamewindow in duelist class before using

gamewindow window=new gamewindow(); 

for ex in constructor or wherever want before calling methods

public duelist(string n) {     window=new gamewindow();         ...     } 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -