0

Here what i am trying to achive is to print a anchor pane by getting the default printer and here is the code.

public class printnodeController {
@FXML
private AnchorPane print;
public void print (){

    Printer printer = Printer.getDefaultPrinter();
    PageLayout pageLayout
        = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, 
Printer.MarginType.HARDWARE_MINIMUM);
    PrinterAttributes attr = printer.getPrinterAttributes();
    PrinterJob job = PrinterJob.createPrinterJob();
    double scaleX
        = pageLayout.getPrintableWidth() / 
print.getBoundsInParent().getWidth();
    double scaleY
        = pageLayout.getPrintableHeight() / 
print.getBoundsInParent().getHeight();
    Scale scale = new Scale(scaleX, scaleY);
    print.getTransforms().add(scale);

    if (job != null && job.showPrintDialog(print.getScene().getWindow())) {
      boolean success = job.printPage(pageLayout, print);
      if (success) {
        job.endJob();

      }
    }
    print.getTransforms().remove(scale);

}
}

but i am getting this error:

    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at 

  com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at 



  com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.NullPointerException
    at application.printnodeController.print(printnodeController.java:25)
    at application.Main.main(Main.java:47)
    ... 11 more
    Exception running application application.Main

i had tried searching but didnt find any solution ; please help new to this site Thankyou in advance.

rVvi
  • 11
  • 1
  • Take care of the Java naming conventions. Classnames should start with uppercase character – Jens Aug 30 '17 at 06:25
  • Look into line **25** and check why the object you try to invoke a method on is *null*. That is all there is to this. And hint: when you do not know how to deal with NPEs - then consider studying some java basics first. Creating JavaFX UIs is an advanced topic ... not really suited for people who dont know how to deal with NPEs. – GhostCat Aug 30 '17 at 06:26
  • Which line is `printnodeController.java:25`? – Jens Aug 30 '17 at 06:26
  • Maybe there is no Defaultprinter – Jens Aug 30 '17 at 06:27
  • the default printer is XPS Document writer – rVvi Aug 30 '17 at 08:21

0 Answers0