I need to create an array of buttons, each having its own Card from another program in which I called Deck. After instantiating the cards and then instantiating the buttons, I keep on getting null pointer exception, which I do not know where it is coming from. Please feel free to ask if you don't understand what I'm talking about; I am relatively new to programming.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TenCard extends JFrame implements ActionListener
{
// a window with borders, title bar, buttons and panel.
private static JFrame f;
// panel that will hold the contents
private JPanel p;
// declare deck and displayed cards
private Deck myDeck; //Deck
private Card[] playerCards1;
private Card[] playerCards2;
/**********components**********/
//JButtons
private JButton[] cardbuttons1;
private JButton[] cardbuttons2;
private JButton sortbutton;
private JButton shufflebutton;
private JButton flipbutton;
//Textfields
private JTextField text1;
private JTextField text2;
//Menu
private JMenuBar menuBar;
private JMenuItem item1, item2;
/*****ending the components*****/
/**
* constructor that instatiates
* the frame, panel, layout, etc and
* adds two cards to the panel
*/
public TenCard()
{
f = new JFrame("High Card Game");
// create the content panel
p = new JPanel();
// set the layout
GridLayout layout = new GridLayout(2,3);
// instantiate game and assign cards
myDeck = new Deck();
myDeck.shuffle();
Card playerCards1[]= new Card[5];
for (int i=0; i<5; i++)
{
playerCards1[i]= myDeck.dealOneCard();
}
Card playerCards2[]= new Card[5];
for (int i=0; i<5; i++)
{
playerCards2[i]= myDeck.dealOneCard();
}
// set layout
p.setLayout(layout);
addCardsToPanel();
addFieldToContentPanel();
// add the content to the window
f.setContentPane(p);
}
/**
* Create and add three buttons to content panel
*/
public void addCardsToPanel()
{
// instantiate buttons with images
sortbutton = new JButton("sort cards");
shufflebutton = new JButton("shuffle cards");
flipbutton = new JButton("flip cards");
JButton cardbuttons1[] = new JButton[5];
for (int i = 0; i < 5; i++)
{
cardbuttons1[i] = new JButton("player 1, card" +i, playerCards1[i].getBackImage());
}
JButton cardbuttons2[] = new JButton[5];
for (int i = 0; i < 5; i++)
{
cardbuttons2[i] = new JButton("player 2, card" +i, playerCards2[i].getBackImage());
}
// add buttons
p.add(sortbutton);
p.add(shufflebutton);
p.add(flipbutton);
}