Hi, I'm trying to create a highscore database for my game, and would like to start off with a view high score frame. When the user clicks this frame will pop up, but for some reason I can't set the player and score information to my JLabels.

I'm not sure if I should use 2d array instead. Beginner here.

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;

public class HighScoreManager extends JFrame {
    HighScore[] scores;
    int scoreCount, scorePosition;
    Container hiS;
    JLabel lblTitle, lblFirst, lblFirstPlayer, lblFirstScore,
            lblSecond, lblSecondPlayer, lblSecondScore,
            lblThird, lblThirdPlayer, lblThirdScore,
            lblFourth, lblFourthPlayer, lblFourthScore,
            lblFifth, lblFifthPlayer, lblFifthScore;
    JPanel northPanel, centerPanel;

    public HighScoreManager() {

        hiS = this.getContentPane();
        hiS.setLayout(new BorderLayout());

        northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());

        lblTitle = new JLabel("Hi-Scores");

        hiS.add(northPanel, BorderLayout.NORTH);

        //Center panel
        centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(5, 3));

        lblFirst = new JLabel("1.");
        lblFirstPlayer = new JLabel("");
        lblFirstScore = new JLabel("");

        lblSecond = new JLabel("2.");
        lblSecondPlayer = new JLabel("");
        lblSecondScore = new JLabel("");

        lblThird = new JLabel("3.");
        lblThirdPlayer = new JLabel("");
        lblThirdScore = new JLabel("");

        lblFourth = new JLabel("4.");
        lblFourthPlayer = new JLabel("");
        lblFourthScore = new JLabel("");

        lblFifth = new JLabel("5.");
        lblFifthPlayer = new JLabel("");
        lblFifthScore = new JLabel("");


        hiS.add(centerPanel, BorderLayout.CENTER);

        scores = new HighScore[5];
        scoreCount = 0;
        try {
            File file = new File("hiScore.txt");
            Scanner input = new Scanner(file);
            // Iterate through the data in the data file and create objects
            while(input.hasNext()) {
                String player = input.next();
                int score = input.nextInt();
                System.out.println(player + score);

                scores[scoreCount] = new HighScore(player, score);

                scoreCount++; // increment the value because we actually created a car
            input.close(); // close the input stream as we have finished reading the data

            // set the position to be zero
            scorePosition = 0;

        catch(Exception e) {
            //JOptionPane.showMessageDialog(this, "File Could Not be Opened");

    public void showScore() {
        // pupulate the text boxes with the values of the first car in the array.
        if(scoreCount != -1) {
            lblFirstPlayer.setText( scores[scorePosition].getPlayer() );
         else   {
            lblFirstPlayer.setText( scores[scorePosition].getPlayer() );

Line 98 is a BIG mistake. If you have a problem reading the scores file you have chosen NOT to be told anything about it! In every catch block of a new program you should request a full print of all the info available on your error.

catch(Exception e) {

Make sure you have this in all your catches, and run th etest again.

I changed the catch exception and I get this error:


I was able to get the read and printed to the console but could not set the text to the JLabels.

That's just one part of the error message. This first line tells you what the error was, the subsequent lines tell you where. You need both for debugging.
It's usually a waste of time to continue testing until you have fixed the known Execption(s).

This article has been dead for over six months. Start a new discussion instead.