Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Edited code does not run correctly

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Edited code does not run correctly

    Edited:

    thank you i was not calling a method properly, code shown below, this is working, it calls file reader this reads the contents of the file fine but i am now getting error messages :

    java.lang.NullPointerException
    	at java.text.AttributedString.<init>(AttributedString.java:127)
    	at PrintText.<init>(PrintText.java:14)
    	at PrintText.main(PrintText.java:18)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at java.text.AttributedString.<init>(AttributedString.java:127)
    	at PrintText.print(PrintText.java:109)
    	at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1973)
    	at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1461)
    	at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1277)
    	at PrintText.printer(PrintText.java:98)
    	at PrintText$1.run(PrintText.java:52)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:682)
    	at java.awt.EventQueue$3.run(EventQueue.java:680)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


    I think it has to do with this line of code :

            AttributedString mStyledText = new AttributedString(mText);

    why am i getting this error ?


    code is below :

    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.print.*;
    import java.text.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.List;
    import java.util.ArrayList;

    public class PrintText implements Printable {

    private static String mText;


    // Below the code will allow the user to select a file and then print out the contents of the file
    public static void main(String[] args) throws IOException {
    new PrintText();
    }

    public PrintText() {
    EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    }

    //selects the file
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File file = chooser.getSelectedFile();
    String filename = file.getName();
    //System.out.println("You have selected: " + filename); testing to see if file seleected was right
    String path = file.getAbsolutePath();

    //Reads contents of file into terminal
    //FileReader fr = new FileReader("filename");
    // FileReader fr = new FileReader("D:/Documents/" + "filename"));

    BufferedReader br = null;
    try {
    br = new BufferedReader(new FileReader(path));
    List<String> list = new ArrayList<String>();
    while ((mText = br.readLine()) != null) {
    //Displays the contents of the file in terminal
    System.out.println(mText);
    list.add(mText);
    }

    printer();
    } catch (IOException exp) {
    exp.printStackTrace();
    } finally {
    try {
    br.close();
    } catch (Exception e) {
    }
    }
    //fr.close();
    }
    });
    }
    //private static final String mText =
    // "This is a test to see if this text will be printed "; //This works perfectly fine
    //AttributedString mStyledText = new AttributedString(mText);
    /**
    * Print a single page containing some sample text.
    */
    public void printer() {

    /* Get the representation of the current printer and
    * the current print job.
    */
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    /* Build a book containing pairs of page painters (Printables)
    * and PageFormats. This example has a single page containing
    * text.
    */
    Book book = new Book();
    book.append(this, new PageFormat());
    /* Set the object to be printed (the Book) into the PrinterJob.
    * Doing this before bringing up the print dialog allows the
    * print dialog to correctly display the page range to be printed
    * and to dissallow any print settings not appropriate for the
    * pages to be printed.
    */
    printerJob.setPageable(book);
    /* Show the print dialog to the user. This is an optional step
    * and need not be done if the application wants to perform
    * 'quiet' printing. If the user cancels the print dialog then false
    * is returned. If true is returned we go ahead and print.
    */
    boolean doPrint = printerJob.printDialog();
    if (doPrint) {
    try {
    printerJob.print();
    } catch (PrinterException exception) {
    System.err.println("Printing error: " + exception);
    }
    }
    }

    /**
    * Print a page of text.
    */
    public int print(Graphics g, PageFormat format, int pageIndex) {
    AttributedString mStyledText = new AttributedString(mText);
    /* We'll assume that Jav2D is available.
    */
    Graphics2D g2d = (Graphics2D) g;
    /* Move the origin from the corner of the Paper to the corner
    * of the imageable area.
    */
    g2d.translate(format.getImageableX(), format.getImageableY());
    /* Set the text color.
    */
    g2d.setPaint(Color.black);
    /* Use a LineBreakMeasurer instance to break our text into
    * lines that fit the imageable area of the page.
    */
    Point2D.Float pen = new Point2D.Float();
    AttributedCharacterIterator charIterator = mStyledText.getIterator();
    LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
    float wrappingWidth = (float) format.getImageableWidth();
    while (measurer.getPosition() < charIterator.getEndIndex()) {
    TextLayout layout = measurer.nextLayout(wrappingWidth);
    pen.y += layout.getAscent();
    float dx = layout.isLeftToRight() ? 0 : (wrappingWidth - layout.getAdvance());
    layout.draw(g2d, pen.x + dx, pen.y);
    pen.y += layout.getDescent() + layout.getLeading();
    }
    return Printable.PAGE_EXISTS;
    }
    }


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Edited code does not run correctly

    this does not work properly only the file reader bit does, no printer options is ever displayed
    Where does the code call any printer methods?

    Do you know how to call a method?
    http://docs.oracle.com/javase/tutori...arguments.html

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Edited code does not run correctly

    Thanks this was the issue, i did update the original question as i am now getting errors, but it seems to have vanished

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Edited code does not run correctly

    java.lang.NullPointerException
    at java.text.AttributedString.<init>(AttributedString .java:127)
    at PrintText.<init>(PrintText.java:14)
    There is a variable with a null value when line 14 is executed.
    Look at line 14 and find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid, non-null value.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Edited code does not run correctly

    Thank you, the code at line 14 was in the wrong place, i have now put this into the print method, however i am now getting this error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at java.text.AttributedString.<init>(AttributedString.java:127)
    	at PrintText.print(PrintText.java:109)
    	at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1973)
    	at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1461)
    	at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1277)
    	at PrintText.printer(PrintText.java:98)
    	at PrintText$1.run(PrintText.java:52)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:682)
    	at java.awt.EventQueue$3.run(EventQueue.java:680)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

  6. #6
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Edited code does not run correctly

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.text.AttributedString.<init>(AttributedString .java:127)
    at PrintText.print(PrintText.java:109)
    Now look at line 109 the same way you looked at line 14 in the previous error.

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. My code is not working correctly
    By shavek911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2012, 06:49 PM
  3. How do I embed my code correctly?
    By jwise95 in forum The Cafe
    Replies: 1
    Last Post: April 22nd, 2011, 10:36 AM
  4. How to run this code?
    By jackdear44 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 2nd, 2010, 12:37 AM
  5. Code not printing correctly
    By Movies32 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2010, 03:59 PM