Applet printing exception

R

Ramu

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.
 
A

Andrew Thompson

Ramu said:
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)

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.
 
R

Ramu

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.");

}

}
 
A

Andrew Thompson

Ramu said:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top