A void function says "parameter p is not used" and giving a null pointer exception

I am working on a deck loading and shuffling program as a jumping off point to code a playable card game, but the function I am using to get the face of each card is returning a null pointer exception on the first if statement. split would put anything before ’ ’ at the zero’th position if i’m not mistaken and the code seems to be getting past the similar code for the card backs fine (that is calling to the one’th position in its array).

void getCardFace(String s, PImage p){
  String[] ary = new String[2];
  ary = split(s,' ');
   if(ary[0]=="GenNuis"){
     p = GenNuisImg;
   }else if(ary[0]=="ShamGna"){
     p = ShamGnaImg;
   }else if(ary[0]=="BrotCly"){
     p = BrotClyImg;
   }else if(ary[0]=="HeavBol"){
     p = HeavBolImg;
   }else if(ary[0]=="MageBol"){
     p = MageBolImg;
   }else if(ary[0]=="SubSham"){
     p = SubShamImg;
   }else if(ary[0]=="OpenRad"){
     p = OpenRadImg;
   }else if(ary[0]=="BrokMic"){
     p = BrokMicImg;
   }else if(ary[0]=="ConfTra"){
     p = ConfTraImg;
   }else if(ary[0]=="MagAdap"){
     p = MagAdapImg;
   }else if(ary[0]=="Verney"){
     p = VerneyImg;
   }else if(ary[0]=="GalvSta"){
     p = GalvStaImg;
   }else if(ary[0]=="HullTit"){
     p = HullTitImg;
   }else if(ary[0]=="ShiHotl"){
     p = ShiHotlImg;
   }else if(ary[0]=="ShepMag"){
     p = ShepMagImg;
   }else if(ary[0]=="HomeInv"){
     p = HomeInvImg;
   }else if(ary[0]=="Prod"){
     p = ProdImg;
   }else if(ary[0]=="WizProd"){
     p = WizProdImg;
   }else if(ary[0]=="LucTrad"){
     p = LucTradImg;
   }else if(ary[0]=="ConfCra"){
     p = ConfCraImg;
   }else if(ary[0]=="VernEss"){
     p = VernEssImg;
   }else if(ary[0]=="OmnScho"){
     p = OmnSchoImg;
   }else if(ary[0]=="ForgHor"){
     p = ForgHorImg;
   }else if(ary[0]=="SpelFoc"){
     p = SpelFocImg;
   }else if(ary[0]=="MagGath"){
     p = MagGathImg;
   }else if(ary[0]=="BubCaul"){
     p = BubCaulImg;
   }else if(ary[0]=="QueQbie"){
     p = QueQbieImg;
   }else if(ary[0]=="RedBOak"){
     p = RedBOakImg;
   }else if(ary[0]=="OchrGou"){
     p = OchrGouImg;
   }else if(ary[0]=="BloBerr"){
     p = BloBerrImg;
   }
}

I didn’t know how to format the code properly when I first posted this

edit: i just fixed it

Welcome to the forum,

In Java string variables must not be compared using the == operator because it doesn’t compare the sequence of characters inside the strings rather it tests to see if they are the same string object

The following shows how to compare strings in your code

  if(ary[0].equals("GenNuis")){
     p = GenNuisImg;
   }else if(ary[0].equals("ShamGna")){
     p = ShamGnaImg;
   ...

This performs a case sensitive test if you are not concerned about whether it uses upper or lower case then use the equalsIgnoreCase instead.

Hello @Aribobari

Some references:

== (equality) / Reference / Processing.org

Troubleshooting · processing/processing4 Wiki · GitHub

:)

Alternatively, we can instead call equals() from the literal string:

  final String imageName = ary[0];

  if ("GenNuis".equals(imageName)) {
     p = GenNuisImg;
   } else if ("ShamGna".equals(imageName)) {
     p = ShamGnaImg;
   ...

Doing so we guard ourselves against a NullPointerException when invoking method equals(); b/c a String literal can’t be null. :light_bulb:

But a more manageable solution is associating each PImage value to its corresponding image name String key using a HashMap container for it:

import java.util.Map;

static final String EXT = ".png", IMAGE_FILENAMES[] = {
  "GenNuis", "ShamGna", "BrotCly", "HeavBol", "MageBol", 
  "SubSham", "OpenRad", "BrokMic", "ConfTra", "MagAdap", 
  "Verney", "GalvSta", "HullTit", "ShiHotl", "ShepMag", 
  "HomeInv", "Prod", "WizProd", "LucTrad", "ConfCra", 
  "VernEss", "OmnScho", "ForgHor", "SpelFoc", "MagGath", 
  "BubCaul", "QueQbie", "RedBOak", "OchrGou", "BloBerr"
};

static final int CAPACITY = (int) (IMAGE_FILENAMES.length / .75) + 1;
final Map<String, PImage> images = new HashMap<String, PImage>(CAPACITY);

void setup() {
  // ...

  loadAllImagesIntoHashMap();
}

void loadAllImagesIntoHashMap() {
  images.clear();
  for (final String s : IMAGE_FILENAMES) images.put(s, loadImage(s + EXT));
}

PImage getCardFace(String imageName) {
  final int spaceIndex = (imageName = trim(imageName)).indexOf(' ');
  if (spaceIndex > 0) imageName = imageName.substring(0, spaceIndex);
  return images.get(imageName);
}

Now your function getCardFace() is much more compact and its imageName parameter can handle a String containing the name for the PImage, regardless it contains other elements separated by a space. :man_mechanic: