Note that there are some explanatory texts on larger screens.

plurals
  1. POJLabel won't display image - NullPointerException
    primarykey
    data
    text
    <p>this is my first Java GUI program, and really only my second java program, so take it easy on me :) My program is a result of a lot of googling and reading java docs. My problem is that I have a sprite sheet of 52 cards, and am attempting to save these cards individually to a Buffered Image array using subImage, and just for testing purposes, display all 52 in a window. The File is in the correct directory I made sure of that. I believe that my problem lies with my use of Jlabels, or simply a foolish mistake. Anyways, here is my class that does the sprite sheet splitting </p> <pre><code> package gui; import java.awt.GridLayout; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; public class crdimgs extends JPanel {/** * */ static final long serialVersionUID = 1L; public final int width = 10; public final int height = 20; public int rows = 13; public int cols = 5; public BufferedImage image; File cardimg = new File("Cards.jpg"); BufferedImage cards[]; public void loadsplit(File loadimage){ try{ image = ImageIO.read(loadimage); } catch(Exception error){ System.out.print("error"); } cards = new BufferedImage[cols*rows]; } public crdimgs() { loadsplit(cardimg); setLayout(new GridLayout(rows, cols, 1, 1)); int x = 0; int y = 0; int subimg = 0; for( int i = 0; i &lt; rows; i++) { JPanel panel = new JPanel(); cards[subimg] = new BufferedImage(width, height, 5); cards[subimg] = image.getSubimage(x, y, width, height); panel.add(new JLabel(new ImageIcon(cards[subimg]))); add(panel); x+=width; subimg++; } y+=height; x=0; } } } </code></pre> <p>And my Main class</p> <pre><code>package gui; import javax.swing.JFrame; import java.awt.Color; public class cards extends JFrame { private static final long serialVersionUID = 1L; public cards(){ setTitle("Poker"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(1000, 700); setLocationRelativeTo(null); this.getContentPane().setBackground(Color.GREEN); setVisible(true); setResizable(false); add(new crdimgs()); } public static void main(String[] args){ new cards(); } } </code></pre> <p>Errors I receive at the moment are:</p> <pre><code>errorException in thread "main" java.lang.NullPointerException at gui.crdimgs.&lt;init&gt;(crdimgs.java:53) at gui.cards.&lt;init&gt;(cards.java:22) at gui.cards.main(cards.java:28) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload