Change jLabel image to image corresponding with Array Path?










0














I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:



switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();




However when I compile this with the rest of my code, I get this:



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)


How do I get this to work?










share|improve this question





















  • Rather then use a relative pass, use an absolute pack from the top of your package ie /resources/images/etc. Also remember, using getResource denotes that the images are within the classpath/embedded in the Jar
    – MadProgrammer
    Nov 12 at 21:11










  • @MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
    – Ethan Leitch
    Nov 12 at 21:22










  • Your images need to be placed in your src directory (under some folder structure), then you can reference them via that folder structure using getResource
    – MadProgrammer
    Nov 12 at 21:30















0














I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:



switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();




However when I compile this with the rest of my code, I get this:



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)


How do I get this to work?










share|improve this question





















  • Rather then use a relative pass, use an absolute pack from the top of your package ie /resources/images/etc. Also remember, using getResource denotes that the images are within the classpath/embedded in the Jar
    – MadProgrammer
    Nov 12 at 21:11










  • @MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
    – Ethan Leitch
    Nov 12 at 21:22










  • Your images need to be placed in your src directory (under some folder structure), then you can reference them via that folder structure using getResource
    – MadProgrammer
    Nov 12 at 21:30













0












0








0







I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:



switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();




However when I compile this with the rest of my code, I get this:



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)


How do I get this to work?










share|improve this question













I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:



switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();




However when I compile this with the rest of my code, I get this:



Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)


How do I get this to work?







java arrays jpanel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 20:45









Ethan Leitch

1




1











  • Rather then use a relative pass, use an absolute pack from the top of your package ie /resources/images/etc. Also remember, using getResource denotes that the images are within the classpath/embedded in the Jar
    – MadProgrammer
    Nov 12 at 21:11










  • @MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
    – Ethan Leitch
    Nov 12 at 21:22










  • Your images need to be placed in your src directory (under some folder structure), then you can reference them via that folder structure using getResource
    – MadProgrammer
    Nov 12 at 21:30
















  • Rather then use a relative pass, use an absolute pack from the top of your package ie /resources/images/etc. Also remember, using getResource denotes that the images are within the classpath/embedded in the Jar
    – MadProgrammer
    Nov 12 at 21:11










  • @MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
    – Ethan Leitch
    Nov 12 at 21:22










  • Your images need to be placed in your src directory (under some folder structure), then you can reference them via that folder structure using getResource
    – MadProgrammer
    Nov 12 at 21:30















Rather then use a relative pass, use an absolute pack from the top of your package ie /resources/images/etc. Also remember, using getResource denotes that the images are within the classpath/embedded in the Jar
– MadProgrammer
Nov 12 at 21:11




Rather then use a relative pass, use an absolute pack from the top of your package ie /resources/images/etc. Also remember, using getResource denotes that the images are within the classpath/embedded in the Jar
– MadProgrammer
Nov 12 at 21:11












@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22




@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22












Your images need to be placed in your src directory (under some folder structure), then you can reference them via that folder structure using getResource
– MadProgrammer
Nov 12 at 21:30




Your images need to be placed in your src directory (under some folder structure), then you can reference them via that folder structure using getResource
– MadProgrammer
Nov 12 at 21:30

















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',
autoActivateHeartbeat: false,
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%2f53269825%2fchange-jlabel-image-to-image-corresponding-with-array-path%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
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269825%2fchange-jlabel-image-to-image-corresponding-with-array-path%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党