Hi, this is my first time posting to stackoverflow and I'm kinda new with java so bear with me and I'm sorry if I'm posting a repeating or a vague question.
So, I'm trying to make a card based drinking game with custom made cards to try out with some friends. Right now the GUI shows just fine, but after I shuffle the deck and try to draw a card (change the label next to the remaining deck to a icon of the card from my map of cards and card icons.), I get NullPointerException. Can someone help/explain me what i'm doing wrong there?
Here's the stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TablePanel$LabelListener.mousePressed(JuomaPeli.java:219)
at java.desktop/java.awt.Component.processMouseEvent(Unknown Source)
at java.desktop/javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.desktop/java.awt.Component.processEvent(Unknown Source)
at java.desktop/java.awt.Container.processEvent(Unknown Source)
at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.EventQueue.access$500(Unknown Source)
at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
Here's part of the code where the error occurs:
public void mousePressed(MouseEvent e) {
JLabel label = (JLabel) e.getSource();
if (label == labelRemainingDeck){
Icon icon = label.getIcon();
if (icon == null) {
return;
} else if (icon == cardBackIcon) {
Card card = remainingDeck[0];
if (card == null){
return;
} else{
labelDealtCard.setIcon(cardIconMap.get(card)); //this is the line 219
removeCard(); //method that returns remainingDeck without the first card
}
} else{
label.setIcon(rulesIcon2);
}
} else if (label == labelDealtCard){
Icon icon = label.getIcon();
if (icon == null){
return;
} else if (icon == rulesIcon){
labelDealtCard.setIcon(rulesIcon2);
} else if (icon == rulesIcon2){
labelDealtCard.setIcon(rulesIcon);
} else
return;
} else{
return;
}
}
I'm not sure if I made the cardIconMap correctly so I will post the createCardFaces class too.
class CreateCardFaces {
public static Map<Card, Icon> createCardIconMap() throws IOException {
Map<Card, Icon> cardIconMap = new HashMap<Card, Icon>();
File dir = new File("/res"); //res folder contains all the card images of .png format
File[] dirListing = dir.listFiles();
if (dirListing != null){
for (File f : dirListing)
{
for (int rankInt = 0; rankInt < 187; rankInt++) //there's 187 custom cards
{
BufferedImage cardImage = ImageIO.read(f);
Rank rank = Rank.values()[rankInt];
cardIconMap.put(new Card(rank), new ImageIcon(cardImage));
}
}
return cardIconMap;
} else {
return null;
}
}
}