Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Applet printing exception

Thread view: 
Ramu - 04 Jan 2007 06:01 GMT
Hi All,
1)       I want to print my html page from applet.If i run my applet
from browser then my applet should be sigened but i am running my
applet from appletviewer then also i am getting exception

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
       at PrinterApplet.prep(PrinterApplet.java:80)
       at PrinterApplet.print(PrinterApplet.java:102)
       at PrinterApplet$1.actionPerformed(PrinterApplet.java:55)
       at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
49)
       at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2169)
       at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
       at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)

and i think my applet need not be signed for the page from which it is
loaded.

2)     how can i send my html content to the applet for
printing.anybody suggest me any examples or suggestions in this issue.
thanks in advance.
Andrew Thompson - 04 Jan 2007 10:06 GMT
> Hi All,
> 1)       I want to print my html page from applet.If i run my applet
[quoted text clipped - 3 lines]
> Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
>         at PrinterApplet.prep(PrinterApplet.java:80)

What does line 80 of PrintApplet.java say?

> and i think my applet need not be signed for the page from which it is
> loaded.

You think wrong.  It is the end user that suddenly finds
400 pages of 'special offers' dumped to the network printer
just because they visited your web-page, that this is
intended to protect.

And this has not even got to a security issue yet.
The code is 'just plain broken'.

Andrew T.
Ramu - 04 Jan 2007 10:22 GMT
hi,
This is my code snippet.could u find the mistake in this code.if i run
this applet i am getting exception and if i run this code as normal
java application printer is getting activated but i am unable to get
printout.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.io.FileInputStream;

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.*;
import org.pdfbox.*;
import java.util.Properties;

//<applet code=PrinterApplet width=500 height=500></applet>
public class PrinterApplet extends JApplet
{
//~ Instance fields==================================================
======

    private PrintRequestAttributeSet aset;
    DocFlavor DocFlavor;
    public Doc doc;
    DocPrintJob printerJob;
    JTextPane pane;

    //~ Methods ==================================================
==============

    /**
    * DOCUMENT ME!
    */
    public void init()
    {
        getContentPane().setLayout(new BorderLayout());

        pane = new JTextPane();
        pane.setSize(150, 100);
        pane.setContentType("text/html");
        pane.setText(
        "<html><center><b><big>Applet Test</big></b><br>"
        + "</center></html>");
        getContentPane().add(pane, "Center");

        JPanel buttons = new JPanel();
        buttons.setBackground(Color.white);

        JButton print = new JButton("Print");

        buttons.add(print);

        getContentPane().add(buttons, "South");

        print.addActionListener(new ActionListener() {public void
actionPerformed(ActionEvent e){print();}});
    }

    /**
    * DOCUMENT ME!
    */
    void prep()
    {

        URL url = null;
        aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(new Copies(1));

        try
        {
            FileInputStream fin =new FileInputStream("PrinterApplet.html");

        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PrintService pservices =
PrintServiceLookup.lookupDefaultPrintService();
        System.out.println(pservices.getName());

        doc = new SimpleDoc(fin,
javax.print.DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_8, null);
        try
        {
            System.out.println("DOC : \n " + doc.getPrintData());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /* Create a Print Job */
        printerJob = pservices.createPrintJob();
        //printerJob = ps.createPrintJob();
    }

    /**
    * DOCUMENT ME!
    */
    void print()
    {
        prep();
        System.out.println("Printer Name : " +
        printerJob.getPrintService());
        try
        {
            printerJob.print(doc, aset);
        }
        catch (PrintException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Done Printing.");

    }

}

> > Hi All,
> > 1)       I want to print my html page from applet.If i run my applet
[quoted text clipped - 18 lines]
>
> Andrew T.
Andrew Thompson - 04 Jan 2007 10:45 GMT
> This is my code snippet.
..
Its crap, please don't waste our bandwidth with 138
lines of uncompilable rubbish.  Post an SSCCE for
further help (from me).
http://www.physci.org/codes/sscce

(I had a longer response, but 'Google ate it', so
you got the short, sharp version)

Andrew T.
Andrew Thompson - 04 Jan 2007 10:48 GMT
> hi,

Please refrain from top-posting.  It is most confusing.

> This is my code snippet.

Please don't waste our bandwidth with snippets, since..

>...could u find the mistake in this code.if i run
> this applet i am getting exception and if i run this code as normal
> java application ...
...
The code as posted is not compilable in any place
but your environtment due to the ..
 a) import of import org.pdfbox.*;
..and other reasons it cannot be comipled are..
 b) line wraps intorduced in posting long code lines to usenet
 c) 2+ missing code parts (I.E. starting with the failure
 to declare 'SimpleDoc', nor. declare and instantiate 'fin'
..and after having kludged the 'snippet' to fix those errors,
I then find it ..
 d) it has no main()!

I suggest you stop wasting our bandwidth, and your time,
by posting an SSCCE of code that fails.  For description
and hints, see..
http://www.physci.org/codes/sscce

So.. coming back to..
>...could u find the mistake in this code.

Do you mean.
"Could you find the mistake in this code?"
(note the Upper Case first letter, correct spelling
of 'you', and closing '?' - which denotes a question)

No.  I will look no further at it.

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.