java - Unable to print the lines -


i using java program print line entered in text field using printer showing null pointer exception

my code in public int print(graphics graphics,pageformat pf,int pageindex) method is

 public int print(graphics graphics,pageformat pf,pageindex pageindex) { if(pageindex>0)  return no_such_page; string s=tf.gettext(); graphics2d g=(graphics2d)graphics; g.translate(pageindex.getimageablex(),pageindex.getimageabley()); g.drawstring(s,50,20); return page_exists;  } 

my printer job method is

// have implemented printable interface b.addactionlistener(new actionlistener() {     public void actionperformed(actionevent ae)     {       printer pr=new printer(); //printer class name         printerjob pj=printerjob.getprinterjob();         pj.setprintable(pr);          if(pj.printdialog==true)         {            try            {                pj.print();            }            catch(exception e)            {                system.out.print(e);            }         }       } }); 

my ui code

 jbutton b; //global variable  jtextfield tf; //global variable public void comp() {     jframe f=new jframe();     f.setlayout(new flowlayout());      b=new jbutton("print");       tf=new jtextfield(10);       f.add(b);       f.add(tf);    } 

so when click on print button showing null pointer exception please me tomorrow exhibition... in advance

as have stated in comments:

string s=tf.gettext(); 

is source of nullpointer exception.

there number of possibilities why happening. without seeing actual classes impossible give definite answer.

it possible method gets executed before tf assigned. possible class attached different instance of printer (which has tf = null).

you're leaving out important details in question

as you're requestion example read text field: here go.

import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener;  public class testbutton extends jframe  {     private jtextfield textfield;     private jbutton button;     private jtextfield outfield;      public testbutton() {         dimension dimension = new dimension(400,400);         setsize(dimension);         setdefaultcloseoperation(jframe.exit_on_close);         setlocationrelativeto(null);          setlayout(new flowlayout());         this.textfield = new jtextfield(10);         this.button = new jbutton("dostuff");         this.outfield = new jtextfield(10);          add(textfield);         add(button);         add(outfield);          button.addactionlistener(new actionlistener()         {             public void actionperformed(actionevent ae)             {                 outfield.settext(textfield.gettext());                 textfield.settext("");             }         });          pack();         setvisible(true);     }      public static void main(string[] argv) {          eventqueue.invokelater(new runnable() {                 @override                 public void run() {                     testbutton testbutton = new testbutton();                 }             });      } } 

i think issue related creating new instance of printable inside inner class. if outer class printable, means constructed new instance variables. might need pass this it

in above code list:

jtextfield tf; //global variable

however if not public static final , not global variable. (this not ideal way expose variable, saying)

if tf instance variable, think see problem. pass new printer() pj.setprintable method. new printer new / fresh instance.

if class owns jtextfield tf printer , owner of printer job method, then need do:

pj.setprintable(this); 

if class owns jtextfield tf different class owner of printer job method, then need do:

pj.settextfield( ... find text field , put in ...) 

in latter case need create settextfield method

my intuition tells me in first category have tf variable , code compiles. goes wrong @ moment create new printer() instance, , doing create completlely new copy variables set null (comp() method sets these non-null values, not new buttons).

hope helps. if had copied in stripped version of class file instead of copied parts of methods, might've been easier find / have meant lot less guessing on , other people's part.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -