1

I would like to display the flags as button using GUI.

however, my code is not working, the expected output should look something like this enter image description here

the images of the flag are saved in the same file with file names being "USA.png", "Spain.png" etc.

this is the code i have now

import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Olympic 
{
    private int NO;
    private String country;
    private double[] score;
    private int rank;
    
    public Olympic(String country)
    {
        this.country = country;
    }
    
    public Olympic (Olympic oly)
    {
        this(oly.country);
    }
    
    public int getRank()
    {
        return rank;
    }
    
    public String getName()
    {
        return country;
    }
}

class OlympicFrame extends JFrame
{
    private JButton[] jbArray = new JButton[12];
    
    private final String[] countryArray = {"USA", "Spain", "China", "Japan", "Italy", "Germany", 
    "France", "Brazil", "Netherland", "Poland", "Russia", "Ukraine"};
    
    private ArrayList<Olympic> alist = new ArrayList <Olympic> ();
    
    //private JLabel rank = new JLabel();
    
    
    public OlympicFrame()
    {
        super ("Olympic 2020");
        setLayout (new GridLayout (4, 3));
        
        constructAList();
        

        
        for (int i = 0; i<jbArray.length; i++)
        {
            getContentPane().add(jbArray[i]);
        }
        

        jbArray = new JButton [countryArray.length];
        
        for (int i = 0; i<jbArray.length; i++)
        {
            jbArray[i] = new JButton(alist.get(i).getName());
            ImageIcon ic = new ImageIcon (alist.get(i).getName() +".png");
            
            jbArray[i].setIcon(ic);

        }

    }
    
    private void constructAList()
    {
        for (int i = 0; i<countryArray.length; i++)
        {
            alist.add(new Olympic(countryArray[i]));
        }
    }
    

}

class aaa
{
    public static void main (String [] args)
    {
        OlympicFrame of = new OlympicFrame();
        of.setSize (300, 100);
        of.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        of.setVisible (true);
    }
}

i have been receiving this error message

Exception in thread "main" java.lang.NullPointerException
        at java.awt.Container.addImpl(Container.java:1095)
        at java.awt.Container.add(Container.java:419)
        at OlympicFrame.<init>(ask.java:58)
        at aaa.main(ask.java:90)
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Curiosa Globunznik Nov 04 '20 at 06:10

2 Answers2

0

the problem is with your code order you added the buttons to contentpane before initializing them, change them as below:

jbArray = new JButton [countryArray.length];

for (int i = 0; i<jbArray.length; i++)
{
    jbArray[i] = new JButton(alist.get(i).getName());
    ImageIcon ic = new ImageIcon (alist.get(i).getName() +".png");
    
    jbArray[i].setIcon(ic);

}

for (int i = 0; i< jbArray.length; i++)
{
    getContentPane().add(jbArray[i]);
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
  • oh! haha thank you for the help –  Nov 04 '20 at 05:36
  • i have another question, will you be able to help? https://stackoverflow.com/questions/64676382/java-gui-show-ranking-in-sequence –  Nov 04 '20 at 09:19
0

Your jbArray[i] is assigning in line 66 but you are trying to access it on line 58. You will need to move the first for loop below the second for loop

Thudani Hettimulla
  • 754
  • 1
  • 12
  • 32
  • i have another question, will you be able to help? https://stackoverflow.com/questions/64676382/java-gui-show-ranking-in-sequence –  Nov 04 '20 at 09:19