"AWT-EventQueue-0" java.lang.NullPointerException Error With Jpanel
here is a GUI class for a card game i made and wen i try to add the bottuns in the option frame it throw that error here is the GUI, anyone have any idea?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import sun.audio.*;
import java.io.*;
publicclass ScopaGUIextends JFrameimplements ScopaUI{
privatestaticfinalint TABLE_SIZE = 10;
privatestaticfinalint PICKUP_SIZE = 6;
privatestaticfinalint COMP_SIZE = 3;
private Game theGame;
private JPanel main, top, score, playAgain, bottom, options;
private CardPanel table, hand, pickup, compHand;
private CardLabel[] label =new CardLabel[TABLE_SIZE];
private CardLabel[] pLabel =new CardLabel[PICKUP_SIZE];
private CardLabel[] cLabel =new CardLabel[COMP_SIZE];
private CardButton[] button =new CardButton[3];
private NewButton newButton;
private ScoreLabel humanScoreLabel, computerScoreLabel, you, me, optLanguage, optCounting;
private Messages message;
private TitledBorder tableBorder, pickupBorder, handBorder, compHandBorder, scoreBorder, optionsBorder;
//private boolean soundEffects;
class CardButtonextends JButtonimplements ActionListener{
private Card theCard;
private ImageIcon blankIcon =new ImageIcon("images/blankCard.jpg");
class MouseControlextends MouseAdapter{
private Cursor HAND =new Cursor(Cursor.HAND_CURSOR);
private Cursor ARROW =new Cursor(Cursor.DEFAULT_CURSOR);
publicvoid mouseEntered(MouseEvent e){
CardButton b = (CardButton) e.getSource();
if (b.theCard !=null) b.setCursor(HAND);
}
publicvoid mouseExited(MouseEvent e){ ((CardButton) e.getSource()).setCursor(ARROW);}
}
public CardButton (){
super();
setCard(null);
addActionListener(this);
addMouseListener(new MouseControl());
}
publicvoid setCard (Card c){
theCard = c;
String[] suitName ={"swords","clubs","cups","coins"};
if (c ==null){
setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
setIcon(blankIcon);
}else{
setBorder(BorderFactory.createRaisedBevelBorder());
setIcon(new ImageIcon("images/" +
suitName[c.getSuit()] +"/" + c.getRank() +".jpg"));
}
}
publicvoid actionPerformed (ActionEvent e){
if (theCard !=null){
clearPickup();
theGame.humanPlays(theCard);
}
}
}
class CardLabelextends JLabel{
private Card theCard;
public CardLabel (){
super();
setCard(null);
}
publicvoid setCard (Card c){
theCard = c;
String[] suitName ={"swords","clubs","cups","coins"};
if (c ==null){
setBorder(null);
setIcon(new ImageIcon("images/blankCard.jpg"));
}else{
setBorder(BorderFactory.createRaisedBevelBorder());
setIcon(new ImageIcon("images/" +
suitName[c.getSuit()] +"/" + c.getRank() +".jpg"));
}
}
publicvoid setOppCard (Card c){
theCard = c;
if (c ==null){
setBorder(null);
setIcon(new ImageIcon("images/blankCard.jpg"));
}else{
setBorder(BorderFactory.createRaisedBevelBorder());
setIcon(new ImageIcon("images/backOfCard.jpg"));
}
}
}
class LanguageButtonGroupextends ButtonGroupimplements ActionListener{
private JRadioButton english, sicilian, italian, hebrew;
public LanguageButtonGroup (int language, JPanel p){
english =new JRadioButton("English");
sicilian =new JRadioButton("Siciliano");
italian =new JRadioButton("Italiano");
hebrew =new JRadioButton("Hebrew");
add(english); p.add(english);
add(sicilian); p.add(sicilian);
add(italian); p.add(italian);
add(hebrew); p.add(hebrew);
english.addActionListener(this);
sicilian.addActionListener(this);
italian.addActionListener(this);
hebrew.addActionListener(this);
switch (language){
case Messages.ENGLISH: english.setSelected(true);break;
case Messages.SICILIAN: sicilian.setSelected(true);break;
case Messages.ITALIAN: italian.setSelected(true);break;
case Messages.HEBREW: hebrew.setSelected(true);break;
}
}
publicvoid actionPerformed (ActionEvent e){
Messages m;
JRadioButton b = (JRadioButton) e.getSource();
if (b == english) m =new Messages(Messages.ENGLISH);
elseif (b == sicilian) m =new Messages(Messages.SICILIAN);
elseif (b == hebrew) m =new Messages(Messages.HEBREW);
else m =new Messages(Messages.ITALIAN);
((ScopaGUI) b.getTopLevelAncestor()).setMessage(m);
}
}
class NewButtonextends JButtonimplements ActionListener{
public NewButton (){
super();
addActionListener(this);
}
publicvoid actionPerformed (ActionEvent e){
clearPickup();
theGame.reset();
}
}
class CardPanelextends JPanel{
public CardPanel (TitledBorder title,int width,int height, CardLabel[] cards){
super();
this.setBorder(BorderFactory.createCompoundBorder(title, BorderFactory.createEmptyBorder(0,0,5,0)));
this.setPreferredSize(new Dimension(width, height));
for (int i = 0; i < cards.length; i++){
cards[i] =new CardLabel();
this.add(cards[i]);
}
}
public CardPanel (TitledBorder title,int width,int height, CardButton[] cards){
super();
this.setBorder(BorderFactory.createCompoundBorder(title, BorderFactory.createEmptyBorder(0,0,5,0)));
this.setPreferredSize(new Dimension(width, height));
for (int i = 0; i < cards.length; i++){
cards[i] =new CardButton();
this.add(cards[i]);
}
}
}
class ScoreLabelextends JLabel{
public ScoreLabel (int size, String text){
super();
this.setFont(new Font("Serif", Font.BOLD, size));
this.setHorizontalAlignment(JLabel.CENTER);
this.setVerticalAlignment(JLabel.TOP);
this.setText(text);
}
}
class ScoreDetailLabelextends JLabel{
public ScoreDetailLabel (String text){
super(text);
this.setHorizontalAlignment(JLabel.CENTER);
this.setFont(new Font("Serif", Font.PLAIN, 18));
this.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
}
public ScopaGUI (){
super("Scupa, Scopa, Sweep!");
}
publicvoid setMessage (Messages m){
message = m;
theGame.setMessage(m);
Card.setMessage(m);
tableBorder.setTitle(message.TABLE);
pickupBorder.setTitle(message.PICKING);
compHandBorder.setTitle(message.COMP);
handBorder.setTitle(message.HAND);
scoreBorder.setTitle(message.SCORE);
optionsBorder.setTitle(message.OPTION);
newButton.setText(message.NEW_GAME);
you.setText(message.YOU);
me.setText(message.I);
repaint();
}
publicvoid runUI (Messages m,boolean debug){
message = m;
initPanels();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(main);
setResizable(false);
theGame =new Game(this, m);
if (debug) theGame.setDebugMode();
theGame.reset();
clearPickup();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
privatevoid initPanels (){
tableBorder = BorderFactory.createTitledBorder(message.TABLE);
table =new CardPanel(tableBorder, 1000, 200, label);
handBorder = BorderFactory.createTitledBorder(message.HAND);
hand =new CardPanel(handBorder, 500, 200, button);
pickupBorder = BorderFactory.createTitledBorder(message.PICKING);
pickup =new CardPanel(pickupBorder, 600, 200, pLabel);
compHandBorder = BorderFactory.createTitledBorder(message.COMP);
compHand =new CardPanel(compHandBorder, 500, 200, cLabel);
score =new JPanel();
score.setLayout(new GridLayout(2,2));
score.setPreferredSize(new Dimension(300,0));
scoreBorder = BorderFactory.createTitledBorder(message.SCORE);
score.setBorder(scoreBorder);
score.add(you =new ScoreLabel(24, message.YOU));
score.add(me =new ScoreLabel(24, message.I));
score.add(humanScoreLabel =new ScoreLabel(48,"0"));
score.add(computerScoreLabel =new ScoreLabel(48,"0"));
playAgain =new JPanel();
playAgain.setLayout(new BoxLayout(playAgain, BoxLayout.PAGE_AXIS));
playAgain.setBorder(BorderFactory.createEmptyBorder(15,15,15,0));
newButton =new NewButton();
newButton.setText(message.NEW_GAME);
newButton.setPreferredSize(new Dimension(130, 30));
playAgain.add(newButton);
new LanguageButtonGroup(message.LANGUAGE, playAgain);
top =new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));
top.add(pickup);
top.add(score);
top.add(options);
top.add(Box.createHorizontalGlue());
top.add(playAgain);
bottom =new JPanel();
bottom.setLayout(new BoxLayout(bottom, BoxLayout.LINE_AXIS));
bottom.add(hand);
bottom.add(compHand);
options =new JPanel();
options.setLayout(new GridLayout(2,2));
options.setPreferredSize(new Dimension(250, 0));
optionsBorder = BorderFactory.createTitledBorder(message.OPTION);
options.setBorder(optionsBorder);
//options.add(optLanguage = new ScoreLabel(12, message.OPTLANGUAGE));
//options.add(optCounting = new ScoreLabel(12, message.OPTCOUNTING));
options.add(playAgain);//this is whr throws error
main =new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
main.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
main.add(top);
main.add(table);
main.add(bottom);
}
publicvoid shuffling (){
displayTable(theGame.getTable());
}
publicvoid dealing (){
displayHumanHand(theGame.getHuman().getHand());
displayComputerHand(theGame.getComputer().getHand());
}
publicvoid announce (String s){
JOptionPane.showMessageDialog(null, s," ", JOptionPane.INFORMATION_MESSAGE);
}
publicvoid displayTable (CardList cards){
Card c;
int tableSize = 0;
for (; !cards.isEmpty(); cards = cards.tail()){
c = cards.head();
label[tableSize++].setCard(c);
}
for (int i = tableSize; i < TABLE_SIZE; i++) label[i].setCard(null);
repaint();
}
publicvoid displayHumanHand (CardList cards){
Card c;
int handSize = 0;
for (; !cards.isEmpty(); cards = cards.tail()){
c = cards.head();
button[handSize++].setCard(c);
}
for (int i = handSize; i < 3; i++) button[i].setCard(null);
repaint();
}
publicvoid displayComputerHand (CardList cards){
Card c;
int compHandSize = 0;
for (; !cards.isEmpty(); cards = cards.tail()){
c = cards.head();
cLabel[compHandSize++].setOppCard(c);
}
for (int i = compHandSize; i < COMP_SIZE; i++) cLabel[i].setOppCard(null);
repaint();
}
publicvoid clearPickup (){
for (int i = 0; i < PICKUP_SIZE; i++) pLabel[i].setCard(null);
repaint();
}
public CardList choosePickUp (List pickUps){
String[] choices =new String[pickUps.length()];
String s;
List l = pickUps;
for (int i = 0; i < choices.length; i++){
choices[i] = l.head().toString();
l = l.tail();
}
do{
s = (String) JOptionPane.showInputDialog(null,message.CHOOSE,message.CHOOSE,
JOptionPane.QUESTION_MESSAGE, null,
choices, choices[0]);
}while (s ==null);
l = pickUps;
for (int i = 0; i < choices.length; i++){
if (choices[i].equals(s))return (CardList) l.head();
l = l.tail();
}
returnnull;
}
publicvoid displayPickUp (CardList cards){
clearPickup();
int i = 0;
for (CardList l = cards; !l.isEmpty(); l = l.tail())
pLabel[i++].setCard(l.head());
repaint();
}
publicvoid displayScoreDetail (Player h, Player c){
int hScopas = h.getScopas();
int cScopas = c.getScopas();
int hCoins = h.coins();
int cCoins = c.coins();
int hCards = h.cards();
int cCards = c.cards();
int[] hBest = h.bestSevens();
int[] cBest = c.bestSevens();
Font loser =new Font("Serif", Font.PLAIN, 18);
Font winner =new Font("Serif", Font.BOLD, 20);
ScoreDetailLabel you =new ScoreDetailLabel(message.YOU);
ScoreDetailLabel me =new ScoreDetailLabel(message.I);
ScoreDetailLabel card =new ScoreDetailLabel(message.CARD);
ScoreDetailLabel coin =new ScoreDetailLabel(message.COIN);
ScoreDetailLabel seven =new ScoreDetailLabel(message.SEVEN);
ScoreDetailLabel sevenCoin =new ScoreDetailLabel(message.SEVENCOIN);
ScoreDetailLabel scopas =new ScoreDetailLabel(message.SCOPA);
ScoreDetailLabel hCard =new ScoreDetailLabel(String.valueOf(hCards));
ScoreDetailLabel cCard =new ScoreDetailLabel(String.valueOf(cCards));
ScoreDetailLabel hCoin =new ScoreDetailLabel(String.valueOf(hCoins));
ScoreDetailLabel cCoin =new ScoreDetailLabel(String.valueOf(cCoins));
ScoreDetailLabel hSeven =new ScoreDetailLabel(sevenNames(hBest));
ScoreDetailLabel cSeven =new ScoreDetailLabel(sevenNames(cBest));
ScoreDetailLabel h7Coin =new ScoreDetailLabel(" ");
ScoreDetailLabel c7Coin =new ScoreDetailLabel(" ");
ScoreDetailLabel hScopa =new ScoreDetailLabel(String.valueOf(hScopas));
ScoreDetailLabel cScopa =new ScoreDetailLabel(String.valueOf(cScopas));
if (hCards > cCards) hCard.setFont(winner);
if (cCards > hCards) cCard.setFont(winner);
if (hCoins > cCoins) hCoin.setFont(winner);
if (cCoins > hCoins) cCoin.setFont(winner);
if (h.sevens() > c.sevens()) hSeven.setFont(winner);
if (c.sevens() > h.sevens()) cSeven.setFont(winner);
if (c.has7Coins()){ c7Coin.setText("!!!"); c7Coin.setFont(winner);}
if (h.has7Coins()){ h7Coin.setText("!!!"); h7Coin.setFont(winner);}
JPanel p =new JPanel(new GridLayout(6,3));
p.setBorder(BorderFactory.createEmptyBorder(0,0,20,100));
p.add(new ScoreDetailLabel(" ")); p.add(you); p.add(me);
p.add(card); p.add(hCard); p.add(cCard);
p.add(coin); p.add(hCoin); p.add(cCoin);
p.add(seven); p.add(hSeven); p.add(cSeven);
p.add(sevenCoin); p.add(h7Coin); p.add(c7Coin);
p.add(scopas); p.add(hScopa); p.add(cScopa);
JOptionPane.showMessageDialog(null, p, message.RESULT, JOptionPane.INFORMATION_MESSAGE);
}
publicvoid displayScore (Player h, Player c){
humanScoreLabel.setText(String.valueOf(h.getScore()));
computerScoreLabel.setText(String.valueOf(c.getScore()));
}
private String sevenNames (int[] best){
String result ="";
String[] name ={ message.FACE,""," 2"," 3"," 4"," 5", message.ACE,""," 6","",""," 7"};
for (int i = Card.SWORDS; i <= Card.COINS; i++)
result += best[i] == -100 ? message.NONE : name[best[i]];
return result;
}
}

