java - JTextField from separate class -


can tell me why keep getting null pointer exception this. have jtextfield in 1 class set , method , in second class want assign jtextfield value string variable. missing here?

heres first class

public class gui extends jpanel {      private final jlabel startloclabel;     private final jlabel endloclabel;     public string startstringcity1;     public string endstringcity;      private final string startlocstring = "enter starting location: ";     private final string endlocstring = "enter ending location: ";      public jtextfield startloctextfield = new jtextfield(5);     public jtextfield endloctextfield = new jtextfield(5);      private final jbutton runbutton;      public gui(container pane)     {         final jframe mainframe = new jframe("gps");         startloclabel = new jlabel(startlocstring);         endloclabel = new jlabel(endlocstring);         runbutton = new jbutton("run");         jpanel labelpane = new jpanel();         labelpane.add(startloclabel, borderlayout.line_start);         labelpane.add(startloctextfield, borderlayout.line_end);         labelpane.add(endloclabel);         labelpane.add(endloctextfield);         labelpane.add(runbutton);         runbutton.addactionlistener(new actionlistener()         {             @override             public void actionperformed(actionevent e)             {                 graph graph = new graph();                 graph.graph();                 setstartcity(startloctextfield.gettext());                 setendcity(endloctextfield.gettext());             }//end actionperformed         });//end action listener;         //main frame gui         mainframe.add(labelpane, borderlayout.page_start);         mainframe.pack();         mainframe.setvisible(true);         mainframe.setdefaultcloseoperation(jframe.exit_on_close);         mainframe.setlocationrelativeto(null);     }      public void setstartcity(string city1)     {         startstringcity1 = city1;      }      public string getstartcity()     {         return startstringcity1;      }      public void setendcity(string endcity)     {         endstringcity = endcity;      }      public string getendcity()     {         return endstringcity;      } } 

and second

public class graph {      public gui gui;     private string getstartcity;      public void graph()     {         getstartcity = gui.getstartcity();         system.out.println(getstartcity);     } } 

here stack trace

java.lang.nullpointerexception     @ minigps.graph.graph(graph.java:42)     @ minigps.gui$1.actionperformed(gui.java:90)     @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022)     @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2346)     @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402)     @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259)     @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252)     @ java.awt.component.processmouseevent(component.java:6525)     @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3322)     @ java.awt.component.processevent(component.java:6290)     @ java.awt.container.processevent(container.java:2234)     @ java.awt.component.dispatcheventimpl(component.java:4881)     @ java.awt.container.dispatcheventimpl(container.java:2292)     @ java.awt.component.dispatchevent(component.java:4703)     @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4898)     @ java.awt.lightweightdispatcher.processmouseevent(container.java:4533)     @ java.awt.lightweightdispatcher.dispatchevent(container.java:4462)     @ java.awt.container.dispatcheventimpl(container.java:2278)     @ java.awt.window.dispatcheventimpl(window.java:2739)     @ java.awt.component.dispatchevent(component.java:4703)     @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:751)     @ java.awt.eventqueue.access$500(eventqueue.java:97)     @ java.awt.eventqueue$3.run(eventqueue.java:702)     @ java.awt.eventqueue$3.run(eventqueue.java:696)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:86)     @ java.awt.eventqueue$4.run(eventqueue.java:724)     @ java.awt.eventqueue$4.run(eventqueue.java:722)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75)     @ java.awt.eventqueue.dispatchevent(eventqueue.java:721)     @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201)     @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93)     @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82) 

based upon code posted (which presume full code) in graph class:

getstartcity = gui.getstartcity(); 

when gui ever instantiated? call graph method (note, methods should start lowercase) result in npe. perhaps meant set explicitly:

        @override         public void actionperformed(actionevent e)         {             graph graph = new graph();             graph.gui = gui.this;             graph.graph();             setstartcity(startloctextfield.gettext());             setendcity(endloctextfield.gettext());         } 

that might solve npe in code posted, there other issues here, such

gui.getstartcity(); 

the method getstartcity() returns startstringcity1, @ time called value null. perhaps, other answer pointed out, meant set before calling graph method (did mention methods should begin lowercase letter?)


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -