-2
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;


public class RPSGraphics implements  ActionListener
{

  int gamesWon = 0;
  int gamesLost = 0;
  int gamesTied = 0;

  JPanel titlePanel, scorePanel, buttonPanel;
  JLabel playerLabel, compLabel, playerScore, compScore, tieScore;
  JButton rocketsButton, panthersButton, spartansButton, resetButton;

  public JPanel createContentPane ()
  {

JPanel totalGUI = new JPanel();
totalGUI.setLayout(null);

titlePanel = new JPanel();
titlePanel.setLayout(null);
titlePanel.setLocation(10, 0);
titlePanel.setSize(250, 30);
totalGUI.add(titlePanel);

playerLabel = new JLabel("You:");
playerLabel.setLocation(0, 0);
playerLabel.setSize(120, 30);
playerLabel.setHorizontalAlignment(0);
playerLabel.setForeground(Color.red);
titlePanel.add(playerLabel);

compLabel = new JLabel("Computer:");
compLabel.setLocation(130, 0);
compLabel.setSize(120, 30);
compLabel.setHorizontalAlignment(0);
compLabel.setForeground(Color.blue);
titlePanel.add(compLabel);

scorePanel = new JPanel();
scorePanel.setLayout(null);
scorePanel.setLocation(10, 40);
scorePanel.setSize(260, 30);
totalGUI.add(scorePanel);

playerScore = new JLabel(""+gamesWon);
playerScore.setLocation(0, 0);
playerScore.setSize(120, 30);
playerScore.setHorizontalAlignment(0);
scorePanel.add(playerScore);

compScore = new JLabel(""+gamesLost);
compScore.setLocation(130, 0);
compScore.setSize(120, 30);
compScore.setHorizontalAlignment(0);
scorePanel.add(compScore);

compScore = new JLabel(""+gamesTied);
compScore.setLocation(65, 0);
compScore.setSize(120, 30);
compScore.setHorizontalAlignment(0);
scorePanel.add(tieScore);

//BUTTONS
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(10, 80);
buttonPanel.setSize(260, 70);
totalGUI.add(buttonPanel);

rocketsButton = new JButton("Rockets");
rocketsButton.setLocation(0, 0);
rocketsButton.setSize(120, 30);
rocketsButton.addActionListener(this);
buttonPanel.add(rocketsButton);

panthersButton = new JButton("Panthers");
panthersButton.setLocation(130, 0);
panthersButton.setSize(120, 30);
panthersButton.addActionListener(this);
buttonPanel.add(panthersButton);

spartansButton = new JButton("Spartans");
spartansButton.setLocation(65, 0);
spartansButton.setSize(120, 30);
spartansButton.addActionListener(this);
buttonPanel.add(spartansButton);

//resetButton = new JButton("New Game");
//resetButton.setLocation(0, 40);
//resetButton.setSize(250, 30);
//resetButton.addActionListener(this);
//buttonPanel.add(resetButton);

totalGUI.setOpaque(true);
return totalGUI;
  }

 public void actionPerformed(ActionEvent e) 
 {
   Scanner scan = new Scanner(System.in);
   Random generator = new Random(); 
   int gamesPlayed = 0;
   //1 = Rockets
   //2 = Panthers
   //3 = Spartans


   while (gamesPlayed < 5)
   {
     int compChoice = generator.nextInt(3) + 1;
     if(e.getSource() == rocketsButton && compChoice == 3) //player: Rock, comp: Scissors
     {
       gamesWon = gamesWon + 1;
       playerScore.setText(""+ gamesWon);
       gamesPlayed++;
     }

     else if(e.getSource() == rocketsButton && compChoice == 2) //player:Rock, comp: Paper
     {
       gamesLost = gamesLost + 1;
       compScore.setText(""+gamesLost);
       gamesPlayed++;
     }

     else if(e.getSource() == rocketsButton && compChoice == 1) //player:Rock, comp: Rock
     {
       gamesTied = gamesTied + 1;
       tieScore.setText(""+gamesTied);
       gamesPlayed++;
     }

     else if(e.getSource() == panthersButton && compChoice == 3) //player: Paper, comp: Scissors
     {
       gamesLost = gamesLost + 1;
       compScore.setText(""+gamesLost);
     }

     else if(e.getSource() == panthersButton && compChoice == 2) //player: Paper, comp: Paper
     {
       gamesTied = gamesTied + 1;
       tieScore.setText(""+gamesTied);
       gamesPlayed++;
     }

     else if(e.getSource() == panthersButton && compChoice == 1) //player: Paper, comp: Rock
     {
       gamesWon = gamesWon + 1;
       playerScore.setText(""+gamesWon);
       gamesPlayed++;
     }

     else if(e.getSource() == spartansButton && compChoice == 3) //player: Scissors, comp: Scissors
     {
       gamesTied = gamesTied + 1;
       tieScore.setText(""+gamesTied);
       gamesPlayed++;
     }

     else if(e.getSource() == spartansButton && compChoice == 2) //player: Scissors, comp: Paper
     {
       gamesWon = gamesWon + 1;
       playerScore.setText(""+gamesWon);
       gamesPlayed++;
     }

     else if(e.getSource() == spartansButton && compChoice == 1) //player: Scissors, comp: Rock
     {
       gamesLost = gamesLost + 1;
       compScore.setText(""+gamesLost);
       gamesPlayed++;
     }
   }

    //else if(e.getSource() == resetButton)
    //{
        //playerScore = 0;
        //compScore = 0;
        //tieScore = 0;
        //gamesWon.setText(""+playerScore);
        //gamesLost.setText(""+compScore);
        //gamesTied.setText(""+tieScore);
    //}
   }

     private static void createAndShowGUI()
     {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("[=] JButton Scores! [=]");


    RPSGraphics demo = new RPSGraphics();
    frame.setContentPane(demo.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(280, 190);
    frame.setVisible(true);
     }

    public static void main(String[] args) 
    {
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          createAndShowGUI();
        }
      });
    }

 }

The error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at RPSGraphics.createContentPane(RPSGraphics.java:70)
at RPSGraphics.createAndShowGUI(RPSGraphics.java:202)
at RPSGraphics.access$0(RPSGraphics.java:194)
at RPSGraphics$1.run(RPSGraphics.java:213)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Oct 20 '14 at 23:25
  • Start by reading your stack trace, it clearly tells you the exact line you are having problems with, the first entry that points to your code is `scorePanel.add(tieScore);` and cursory glance over your code will show that `tieScore` is `null`. This would make a great exercise in learning how to use a debugger... – MadProgrammer Oct 20 '14 at 23:29

1 Answers1

0

The NPE points to this line (70):

scorePanel.add(tieScore);

obviously, you haven't initialized tieScore

Lolo
  • 4,277
  • 2
  • 25
  • 24