I feel that's the most accurate title ever created and says exactly what I need. I am making a card game in Java and have loaded all the cards stats into a spreadsheet. I'm using the JXL api to read the spreadsheet and grab the stats for each card from its respective cell in the spreadsheet and store them in a Deck object and everything works really great there, but when I try to use the string that the getImage() returns, I keep getting a null pointer and I can't figure out why. I'll post everything I think is relevant, please let me know if more is needed.
The first part is from the class LoadCards that loads the spreadsheet and attaches the data to a Deck object. Note: i only have one attempt here as a test to make sure it's working and it's not.
public class LoadCards
{
public Deck LoadCards()
{
Workbook cardList = null;
try
{
cardList = Workbook.getWorkbook(new File("C:\\DC Card Game\\src\\DCCardGame\\resources\\CardList.xls"));
}
catch (IOException e)
{
String message = "The file titled CardList.xls was not found.";
String title = "File Not Found";
JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
} catch (BiffException ex)
{
String message = "File must have an extension of .xls";
String title = "Incompatible CardList File";
JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
}
Sheet typeSheet = cardList.getSheet(0);
Sheet equipmentSheet = cardList.getSheet(1);
String equipmentType = typeSheet.getCell(0,1).getContents();
Deck testCard= new Deck(equipmentSheet.getCell(0, 1).getContents(),equipmentType,Integer.parseInt(equipmentSheet.getCell(2,1).getContents()),Integer.parseInt(equipmentSheet.getCell(3,1).getContents()),Integer.parseInt(equipmentSheet.getCell(4,1).getContents()),equipmentSheet.getCell(5,1).getContents());
return testCard;
That last line builds an object for the Deck constructor which looks like this:
public Deck(String cardName, String type, int cost, int value, int power, String image)
{
super();
}
And Deck extends Card, where all the appropriate getters/setters are.
public Card(String cardName, String type, int cost, int value, int power, String image)
{
this.cardName = cardName;
this.type = type;
this.cost = cost;
this.value = value;
this.power = power;
this.image = image;
}
In my form class, this is my code I'm using to test if the image is working (hint: it's not.) It keeps throwing a NullPointerException
LoadCards test = new LoadCards();
test.LoadCards();
ImageIcon testImage = new ImageIcon(getClass().getResource(test.LoadCards().getImage()));
extraLineupSlot1.setIcon(testImage);
I need to figure out how I can correct this so that I can call the string from my spreadsheet that will represent a directory, the directory is stored in the image property of the Deck/Card object, and then I can call that property and change the icon associated with the different JLabels I've made.
It sounds like you're able to get the list of strings, but you're having trouble getting the corresponding named resource. In this related example, a
List<String>
of names shares a common index with aList<ImageIcon>
that serves as a cache of images stored as resources in a JAR. The methodgetImage(int index)
returns an existing image from the cache or loads the image viagetResource()
for future reference. You might compare your approach. See the info for embedded-resource for more examples.