Saving and printing Images from a 2D array in Java











up vote
-1
down vote

favorite












I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.



I wrote it so the rows are the cards and the columns are the suits.



Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.



An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg



I don't know why my code isn't working.



I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:



card[i][j] = tk.getImage(url);


But something tells me that's not the only thing wrong...



Can someone help me? Thanks!



The source code:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class GameTemplate extends JPanel {

static Image card = new Image [13][4];

public static void main (String args) {

Toolkit tk = Toolkit.getDefaultToolkit();

// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3

// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12

int name = new int [13][4];

for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}

GameTemplate run = new GameTemplate ();
run.image(args);
}

public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}

}
}









share|improve this question
























  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Joe C
    Nov 10 at 22:27










  • A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
    – Matthew Smith
    Nov 10 at 22:35






  • 2




    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
    – Joe C
    Nov 10 at 22:44










  • getResource returns null if the resource can't be found. So tk.getImage is called with null as argument.which causes a NullPointerException along the way,
    – QBrute
    Nov 10 at 22:58















up vote
-1
down vote

favorite












I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.



I wrote it so the rows are the cards and the columns are the suits.



Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.



An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg



I don't know why my code isn't working.



I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:



card[i][j] = tk.getImage(url);


But something tells me that's not the only thing wrong...



Can someone help me? Thanks!



The source code:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class GameTemplate extends JPanel {

static Image card = new Image [13][4];

public static void main (String args) {

Toolkit tk = Toolkit.getDefaultToolkit();

// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3

// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12

int name = new int [13][4];

for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}

GameTemplate run = new GameTemplate ();
run.image(args);
}

public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}

}
}









share|improve this question
























  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Joe C
    Nov 10 at 22:27










  • A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
    – Matthew Smith
    Nov 10 at 22:35






  • 2




    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
    – Joe C
    Nov 10 at 22:44










  • getResource returns null if the resource can't be found. So tk.getImage is called with null as argument.which causes a NullPointerException along the way,
    – QBrute
    Nov 10 at 22:58













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.



I wrote it so the rows are the cards and the columns are the suits.



Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.



An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg



I don't know why my code isn't working.



I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:



card[i][j] = tk.getImage(url);


But something tells me that's not the only thing wrong...



Can someone help me? Thanks!



The source code:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class GameTemplate extends JPanel {

static Image card = new Image [13][4];

public static void main (String args) {

Toolkit tk = Toolkit.getDefaultToolkit();

// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3

// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12

int name = new int [13][4];

for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}

GameTemplate run = new GameTemplate ();
run.image(args);
}

public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}

}
}









share|improve this question















I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.



I wrote it so the rows are the cards and the columns are the suits.



Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.



An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg



I don't know why my code isn't working.



I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:



card[i][j] = tk.getImage(url);


But something tells me that's not the only thing wrong...



Can someone help me? Thanks!



The source code:



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class GameTemplate extends JPanel {

static Image card = new Image [13][4];

public static void main (String args) {

Toolkit tk = Toolkit.getDefaultToolkit();

// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3

// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12

int name = new int [13][4];

for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}

GameTemplate run = new GameTemplate ();
run.image(args);
}

public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}

}
}






java arrays image static






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 23:41

























asked Nov 10 at 22:25









Matthew Smith

118




118












  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Joe C
    Nov 10 at 22:27










  • A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
    – Matthew Smith
    Nov 10 at 22:35






  • 2




    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
    – Joe C
    Nov 10 at 22:44










  • getResource returns null if the resource can't be found. So tk.getImage is called with null as argument.which causes a NullPointerException along the way,
    – QBrute
    Nov 10 at 22:58


















  • Possible duplicate of What is a NullPointerException, and how do I fix it?
    – Joe C
    Nov 10 at 22:27










  • A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
    – Matthew Smith
    Nov 10 at 22:35






  • 2




    It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
    – Joe C
    Nov 10 at 22:44










  • getResource returns null if the resource can't be found. So tk.getImage is called with null as argument.which causes a NullPointerException along the way,
    – QBrute
    Nov 10 at 22:58
















Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27




Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27












A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35




A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35




2




2




It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44




It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44












getResource returns null if the resource can't be found. So tk.getImage is called with null as argument.which causes a NullPointerException along the way,
– QBrute
Nov 10 at 22:58




getResource returns null if the resource can't be found. So tk.getImage is called with null as argument.which causes a NullPointerException along the way,
– QBrute
Nov 10 at 22:58

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244028%2fsaving-and-printing-images-from-a-2d-array-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244028%2fsaving-and-printing-images-from-a-2d-array-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma