java.lang.NullPointerException in Java Swing code -


can tell me why following code throwing null pointer exception? exception thrown @ line numberjtextfield.addmouselistener(new mymouseadapter(numberjtextfield)); in class inputjframe1.java.

import java.awt.*;  public class inputjframe1 extends javax.swing.jframe {     private javax.swing.jtextfield numberjtextfield;      public inputjframe1()     {               numberjtextfield.addmouselistener(new mymouseadapter(numberjtextfield));     }      private void initcomponents()      {         numberjtextfield = new javax.swing.jtextfield();          setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);          getcontentpane().add(numberjtextfield);     }      public static void main(string args[])     {         try {             (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) {                 if ("nimbus".equals(info.getname())) {                     javax.swing.uimanager.setlookandfeel(info.getclassname());                     break;                 }             }         } catch (classnotfoundexception ex) {         java.util.logging.logger.getlogger(inputjframe1.class.getname()).log(java.util.logging.level.severe, null, ex);         } catch (instantiationexception ex) {         java.util.logging.logger.getlogger(inputjframe1.class.getname()).log(java.util.logging.level.severe, null, ex);         } catch (illegalaccessexception ex) {         java.util.logging.logger.getlogger(inputjframe1.class.getname()).log(java.util.logging.level.severe, null, ex);         } catch (javax.swing.unsupportedlookandfeelexception ex) {         java.util.logging.logger.getlogger(inputjframe1.class.getname()).log(java.util.logging.level.severe, null, ex);         }          java.awt.eventqueue.invokelater(new runnable()                                     {                                         @override                                         public void run()                                         {                                             new inputjframe1().setvisible(true);                                         }                                     });     } }    import java.awt.color; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import javax.swing.jtextfield;  public class mymouseadapter extends mouseadapter {     private jtextfield jtextfield;      mymouseadapter(jtextfield jtextfield)     {         this.jtextfield=jtextfield;     }      @override     public void mouseclicked(mouseevent e)     {           jtextfield.setforeground(color.red);     } } 

stacktrace:

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ javaapplication8.inputjframe1.(inputjframe1.java:9) @ javaapplication8.inputjframe1$1.run(inputjframe1.java:73) @ java.awt.event.invocationevent.dispatch(invocationevent.java:251) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:727) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:688) @ java.awt.eventqueue$3.run(eventqueue.java:686) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:697) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)

don't remove initcomponents constructor. makes sure components initialized before using them, should first line of constructor.

public inputjframe1() {           initcomponents();      numberjtextfield.addmouselistener(new mymouseadapter(numberjtextfield)); } 

Comments