{"id":13870,"date":"2022-10-05T16:37:02","date_gmt":"2022-10-05T11:07:02","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/"},"modified":"2022-10-05T16:37:02","modified_gmt":"2022-10-05T11:07:02","slug":"solved-awt-eventqueue-0-java-lang-nullpointerexception","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/","title":{"rendered":"[Solved] AWT-EventQueue-0&#8243; java.lang.NullPointerException"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-14007091\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"14007091\" data-parentid=\"14007041\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<ol>\n<li>Don&#8217;t make the <code>print<\/code> method static.  You need to call this after the user has made a file selection, passing in the file contents&#8230;<\/li>\n<li>Don&#8217;t create a new instance of the <code>PrintText<\/code> in the <code>print<\/code> this is not sharing the results of the file selection with the <code>print<\/code> method.<\/li>\n<li>Make sure you are honoring the Thread rules of Swing<\/li>\n<li>If you open it, you&#8217;re responsible for closing it.  Make sure you are closing you streams\/readers<\/li>\n<li>You might like to check to see if the user actually selected a file, but I&#8217;ll leave that up to you.<\/li>\n<\/ol>\n<p>Something more along the lines of&#8230;<\/p>\n<pre><code>public class PrintText implements Printable {\n\n    private static String mText;\n\n    \/\/ Below the code will allow the user to select a file and then print out the contents of the file\n    public static void main(String[] args) throws IOException {\n        new PrintText();\n    }\n\n    public PrintText() {\n        EventQueue.invokeLater(new Runnable() {\n            @Override\n            public void run() {\n                try {\n                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\n                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {\n                }\n\n                \/\/selects the file\n                JFileChooser chooser = new JFileChooser();\n                chooser.showOpenDialog(null);\n                File file = chooser.getSelectedFile();\n                String filename = file.getName();\n                \/\/System.out.println(\"You have selected: \" + filename);  testing to see if file seleected was right\n                String path = file.getAbsolutePath();\n\n                \/\/Reads contents of file into terminal \n                \/\/FileReader fr = new FileReader(\"filename\");\n                \/\/ FileReader fr = new FileReader(\"D:\/Documents\/\" + \"filename\")); \n\n                BufferedReader br = null;\n                try {\n                    br = new BufferedReader(new FileReader(path));\n                    List&lt;String&gt; list = new ArrayList&lt;String&gt;();\n                    while ((mText = br.readLine()) != null) {\n                        \/\/Displays the contents of the file in terminal\n                        System.out.println(mText);\n                        list.add(mText);\n                    }\n\n                    printer();\n                } catch (IOException exp) {\n                    exp.printStackTrace();\n                } finally {\n                    try {\n                        br.close();\n                    } catch (Exception e) {\n                    }\n                }\n                \/\/fr.close(); \n            }\n        });\n    }\n    \/\/private static final String mText = \n    \/\/    \"This is a test to see if this text will be printed \"; \/\/This works perfectly fine\n    AttributedString mStyledText = new AttributedString(mText);\n\n    \/**\n     * Print a single page containing some sample text.\n     *\/\n    public void printer() {\n\n        \/* Get the representation of the current printer and \n         * the current print job.\n         *\/\n        PrinterJob printerJob = PrinterJob.getPrinterJob();\n        \/* Build a book containing pairs of page painters (Printables)\n         * and PageFormats. This example has a single page containing\n         * text.\n         *\/\n        Book book = new Book();\n        book.append(this, new PageFormat());\n        \/* Set the object to be printed (the Book) into the PrinterJob.\n         * Doing this before bringing up the print dialog allows the\n         * print dialog to correctly display the page range to be printed\n         * and to dissallow any print settings not appropriate for the\n         * pages to be printed.\n         *\/\n        printerJob.setPageable(book);\n        \/* Show the print dialog to the user. This is an optional step\n         * and need not be done if the application wants to perform\n         * 'quiet' printing. If the user cancels the print dialog then false\n         * is returned. If true is returned we go ahead and print.\n         *\/\n        boolean doPrint = printerJob.printDialog();\n        if (doPrint) {\n            try {\n                printerJob.print();\n            } catch (PrinterException exception) {\n                System.err.println(\"Printing error: \" + exception);\n            }\n        }\n    }\n\n    \/**\n     * Print a page of text.\n     *\/\n    public int print(Graphics g, PageFormat format, int pageIndex) {\n        \/* We'll assume that Jav2D is available.\n         *\/\n        Graphics2D g2d = (Graphics2D) g;\n        \/* Move the origin from the corner of the Paper to the corner\n         * of the imageable area.\n         *\/\n        g2d.translate(format.getImageableX(), format.getImageableY());\n        \/* Set the text color.\n         *\/\n        g2d.setPaint(Color.black);\n        \/* Use a LineBreakMeasurer instance to break our text into\n         * lines that fit the imageable area of the page.\n         *\/\n        Point2D.Float pen = new Point2D.Float();\n        AttributedCharacterIterator charIterator = mStyledText.getIterator();\n        LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());\n        float wrappingWidth = (float) format.getImageableWidth();\n        while (measurer.getPosition() &lt; charIterator.getEndIndex()) {\n            TextLayout layout = measurer.nextLayout(wrappingWidth);\n            pen.y += layout.getAscent();\n            float dx = layout.isLeftToRight() ? 0 : (wrappingWidth - layout.getAdvance());\n            layout.draw(g2d, pen.x + dx, pen.y);\n            pen.y += layout.getDescent() + layout.getLeading();\n        }\n        return Printable.PAGE_EXISTS;\n    }\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved AWT-EventQueue-0&#8243; java.lang.NullPointerException <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Don&#8217;t make the print method static. You need to call this after the user has made a file selection, passing in the file contents&#8230; Don&#8217;t create a new instance of the PrintText in the print this is not sharing the results of the file selection with the print method. Make sure you are honoring &#8230; <a title=\"[Solved] AWT-EventQueue-0&#8243; java.lang.NullPointerException\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\" aria-label=\"More on [Solved] AWT-EventQueue-0&#8243; java.lang.NullPointerException\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323,927,621],"class_list":["post-13870","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java","tag-printing","tag-swing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] AWT-EventQueue-0&quot; java.lang.NullPointerException - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] AWT-EventQueue-0&quot; java.lang.NullPointerException - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Don&#8217;t make the print method static. You need to call this after the user has made a file selection, passing in the file contents&#8230; Don&#8217;t create a new instance of the PrintText in the print this is not sharing the results of the file selection with the print method. Make sure you are honoring ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-05T11:07:02+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] AWT-EventQueue-0&#8243; java.lang.NullPointerException\",\"datePublished\":\"2022-10-05T11:07:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\"},\"wordCount\":115,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\",\"printing\",\"swing\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\",\"name\":\"[Solved] AWT-EventQueue-0\\\" java.lang.NullPointerException - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-05T11:07:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] AWT-EventQueue-0&#8243; java.lang.NullPointerException\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] AWT-EventQueue-0\" java.lang.NullPointerException - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] AWT-EventQueue-0\" java.lang.NullPointerException - JassWeb","og_description":"[ad_1] Don&#8217;t make the print method static. You need to call this after the user has made a file selection, passing in the file contents&#8230; Don&#8217;t create a new instance of the PrintText in the print this is not sharing the results of the file selection with the print method. Make sure you are honoring ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/","og_site_name":"JassWeb","article_published_time":"2022-10-05T11:07:02+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] AWT-EventQueue-0&#8243; java.lang.NullPointerException","datePublished":"2022-10-05T11:07:02+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/"},"wordCount":115,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java","printing","swing"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/","url":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/","name":"[Solved] AWT-EventQueue-0\" java.lang.NullPointerException - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-05T11:07:02+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-awt-eventqueue-0-java-lang-nullpointerexception\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] AWT-EventQueue-0&#8243; java.lang.NullPointerException"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=13870"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13870\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=13870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=13870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=13870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}