{"id":15281,"date":"2022-10-11T01:35:48","date_gmt":"2022-10-10T20:05:48","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/"},"modified":"2022-10-11T01:35:48","modified_gmt":"2022-10-10T20:05:48","slug":"solved-the-images-event-handler-shows-error-in-java-gui","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/","title":{"rendered":"[Solved] The image&#8217;s event handler shows error in Java GUI"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-34955653\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"34955653\" data-parentid=\"34953856\" data-score=\"1\" 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<p>Why are you creating new instance of <code>JButton<\/code> in <code>roll<\/code>? You just need to change the icon of the buttons which are already displayed on the screen<\/p>\n<p>This&#8230;<\/p>\n<pre><code>public void roll() {\n    value = nextValue() ;\n    if (value==1){\n        Icon i=new ImageIcon(getClass().getResource(\"1.png\"));\n        c= new JButton(i);\n        add(c);\n    } else if(value==2){\n        Icon im=new ImageIcon(getClass().getResource(\"2.png\"));\n        c= new JButton(im);\n        add(c);\n    }\n\n    repaint() ;\n}\n<\/code><\/pre>\n<p>should be&#8230;<\/p>\n<pre><code>public void roll() {\n    value = nextValue() ;\n    Icon i = null;\n    if (value==1){\n        i=new ImageIcon(getClass().getResource(\"1.png\"));\n    } else if(value==2){\n        i=new ImageIcon(getClass().getResource(\"2.png\"));\n    }\n    c.setIcon(i);\n}\n<\/code><\/pre>\n<p><code>setIcon<\/code> is a bound field, which means that it will automatically generate a <code>repaint<\/code> request of it&#8217;s own.<\/p>\n<p>If the images are not been updated then it&#8217;s likely that your images aren&#8217;t been loaded, you can test this in two ways, first, you can set the text of  the buttons<\/p>\n<pre><code>public void roll() {\n    value = nextValue() ;\n    c.setText(Integer.toString(value));\n    Icon i = null;\n    if (value==1){\n        i=new ImageIcon(getClass().getResource(\"1.png\"));\n    } else if(value==2){\n        i=new ImageIcon(getClass().getResource(\"2.png\"));\n    }\n    c.setIcon(i);\n\n    repaint() ;\n}\n<\/code><\/pre>\n<p>and second, you should be using <code>ImageIO.read<\/code> to load the resources, for example&#8230;<\/p>\n<pre><code>import java.awt.BorderLayout;\nimport java.awt.Color;\nimport java.awt.FlowLayout;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.awt.image.BufferedImage;\nimport java.io.IOException;\nimport java.net.URL;\nimport java.util.Random;\nimport java.util.logging.Level;\nimport java.util.logging.Logger;\nimport javax.imageio.ImageIO;\nimport javax.swing.Icon;\nimport javax.swing.ImageIcon;\nimport javax.swing.JButton;\nimport javax.swing.JFrame;\n\npublic class Background extends JFrame {\n\n    private Random ran;\n    private int value;\n    private JButton r;\n    private JButton c;\n\n    public Background() {\n        super(\"title\");\n        ran = new Random();\n        value = nextValue();\n        setLayout(new FlowLayout());\n\n        r = new JButton(\"ROLL \");\n        r.setForeground(Color.WHITE);\/\/ndryshon ngjyren e shkrimit\n        r.setBackground(Color.YELLOW);\n        add(r);\n\n        try {\n            BufferedImage die = ImageIO.read(getClass().getResource(\"1.png\"));\n            c = new JButton(new ImageIcon(die));\n        } catch (IOException ex) {\n            ex.printStackTrace();\n        }\n\n        add(c);\n\n        thehandler hand = new thehandler(this);\/\/konstruktori i handler merr nje instance te Background\n        r.addActionListener(hand);\n        c.addActionListener(hand);\n    }\n\n    private int nextValue() {\n        return Math.abs(ran.nextInt()) % 6 + 1;\n    }\n\n    public void roll() {\n        value = nextValue();\n        c.setText(Integer.toString(value));\n        URL path = null;\n        if (value == 1) {\n            path = getClass().getResource(\"1.png\");\n        } else if (value == 2) {\n            path = getClass().getResource(\"2.png\");\n        }\n        try {\n            BufferedImage die = ImageIO.read(path);\n            c.setIcon(new ImageIcon(die));\n        } catch (IOException ex) {\n            ex.printStackTrace();\n        }\n    }\n\n    public int getValue() {\n        return value;\n    }\n\n    private class thehandler implements ActionListener {\n\n        private Background m;\n\n        thehandler(Background thisone) {\n            m = thisone;\n        }\n\n        public void actionPerformed(ActionEvent event) {\n            m.roll();\n        }\n    }\n\n    public static void main(String[] args) {\n        Background d = new Background();\n        d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        d.getContentPane().setBackground(Color.GREEN);\n        d.setSize(700, 500);\n        d.setVisible(true);\n    }\n}\n<\/code><\/pre>\n<p>This my throw a <code>NullPointerException<\/code>, which would mean that Java can&#8217;t find your images.  Based on your code, you images must be stored within the same package as your class<\/p>\n<p>Have a look at <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/docs.oracle.com\/javase\/tutorial\/2d\/images\/loadimage.html\">Reading\/Loading an Image<\/a> for more details about <code>ImageIO<\/code><\/p>\n<p>The problem with this&#8230;<\/p>\n<pre><code>r.roll();\/\/ERROR  \n<\/code><\/pre>\n<p>is <code>JButton<\/code> has not <code>roll<\/code> method<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved The image&#8217;s event handler shows error in Java GUI <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Why are you creating new instance of JButton in roll? You just need to change the icon of the buttons which are already displayed on the screen This&#8230; public void roll() { value = nextValue() ; if (value==1){ Icon i=new ImageIcon(getClass().getResource(&#8220;1.png&#8221;)); c= new JButton(i); add(c); } else if(value==2){ Icon im=new ImageIcon(getClass().getResource(&#8220;2.png&#8221;)); c= new JButton(im); &#8230; <a title=\"[Solved] The image&#8217;s event handler shows error in Java GUI\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/\" aria-label=\"More on [Solved] The image&#8217;s event handler shows error in Java GUI\">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":[2338,1192,323,1014,621],"class_list":["post-15281","post","type-post","status-publish","format-standard","hentry","category-solved","tag-awt","tag-image","tag-java","tag-random","tag-swing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] The image&#039;s event handler shows error in Java GUI - 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-the-images-event-handler-shows-error-in-java-gui\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] The image&#039;s event handler shows error in Java GUI - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Why are you creating new instance of JButton in roll? You just need to change the icon of the buttons which are already displayed on the screen This&#8230; public void roll() { value = nextValue() ; if (value==1){ Icon i=new ImageIcon(getClass().getResource(&quot;1.png&quot;)); c= new JButton(i); add(c); } else if(value==2){ Icon im=new ImageIcon(getClass().getResource(&quot;2.png&quot;)); c= new JButton(im); ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-10T20:05:48+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-the-images-event-handler-shows-error-in-java-gui\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] The image&#8217;s event handler shows error in Java GUI\",\"datePublished\":\"2022-10-10T20:05:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/\"},\"wordCount\":165,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"awt\",\"image\",\"java\",\"random\",\"swing\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/\",\"name\":\"[Solved] The image's event handler shows error in Java GUI - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-10T20:05:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] The image&#8217;s event handler shows error in Java GUI\"}]},{\"@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] The image's event handler shows error in Java GUI - 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-the-images-event-handler-shows-error-in-java-gui\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] The image's event handler shows error in Java GUI - JassWeb","og_description":"[ad_1] Why are you creating new instance of JButton in roll? You just need to change the icon of the buttons which are already displayed on the screen This&#8230; public void roll() { value = nextValue() ; if (value==1){ Icon i=new ImageIcon(getClass().getResource(\"1.png\")); c= new JButton(i); add(c); } else if(value==2){ Icon im=new ImageIcon(getClass().getResource(\"2.png\")); c= new JButton(im); ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/","og_site_name":"JassWeb","article_published_time":"2022-10-10T20:05:48+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-the-images-event-handler-shows-error-in-java-gui\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] The image&#8217;s event handler shows error in Java GUI","datePublished":"2022-10-10T20:05:48+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/"},"wordCount":165,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["awt","image","java","random","swing"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/","url":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/","name":"[Solved] The image's event handler shows error in Java GUI - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-10T20:05:48+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-the-images-event-handler-shows-error-in-java-gui\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] The image&#8217;s event handler shows error in Java GUI"}]},{"@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\/15281","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=15281"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/15281\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=15281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=15281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=15281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}