0

I'll cut to the chase, I have a NullPointerException and I can't think of a way to fix it. The cause is an ImageIcon array which is obviously empty at the point I want to use it in my code. I can't really grasp the order on how the code is getting exectued here, thus I can't fix the exception.

Down below is the class which is supposed to fill the array with something, right after the super statement.

package mySharkAttacks;

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

public class mySharkAttacksPanel extends JPanel {

String[] regionStrings = {"WorldWide", "USA", "Australia", "Bahamas", "Cayman Islands"};
ImageIcon[] regionImages = new ImageIcon[regionStrings.length];

public mySharkAttacksPanel() {
    super(new BorderLayout());

    //Load the pet regionImages and create an array of indexes.
    //regionImages = new ImageIcon[regionStrings.length];

    Integer[] intArray = new Integer[regionStrings.length];
    for (int i = 0; i < regionStrings.length; i++) {
        intArray[i] = new Integer(i);
        regionImages[i] = createImageIcon("regionImages/" + regionStrings[i] + ".gif");
        if (regionImages[i] != null) {
            regionImages[i].setDescription(regionStrings[i]);
        }
    }

    //Create the combo box.
    JComboBox regionList = new JComboBox(intArray);
    //ComboBoxRenderer renderer= new ComboBoxRenderer();
    //renderer.setPreferredSize(new Dimension(200, 130));
    regionList.setRenderer(new ComboBoxRenderer(regionImages, regionStrings));
    regionList.setMaximumRowCount(3);

    //Lay out the demo.
    add(regionList, BorderLayout.PAGE_START);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}

 /** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = mySharkAttacksPanel.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
            return null;
    }
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("mySharkAttacks");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new mySharkAttacksPanel();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}
}

And here is the class that is supposed to use this ImageIcon array which I parse as a parameter.

package mySharkAttacks;

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

public class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    private Font normalFont;

ImageIcon[] localCurrentImage;
String[] localCurrentString;

public ComboBoxRenderer(ImageIcon[] currentImage, String[] currentString) {
    setOpaque(true);
    setHorizontalAlignment(CENTER);
    setVerticalAlignment(CENTER);


    for (int i = 0; i < currentImage.length; i++) 
    {
        localCurrentImage[i] = currentImage[i];
    }

    for (int i = 0; i < currentString.length; i++) 
    {
        localCurrentString[i] = currentString[i];
    }

}

/*
 * This method finds the image and text corresponding
 * to the selected value and returns the label, set up
 * to display the text and image.
 */
public Component getListCellRendererComponent(
                                   JList list,
                                   Object value,
                                   int index,
                                   boolean isSelected,
                                   boolean cellHasFocus) {


    //Get the selected index. (The index param isn't
    //always valid, so just use the value.)
    int selectedIndex = ((Integer)value).intValue();

    if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }

    //Set the icon and text.  If icon was null, say so.
    ImageIcon icon = localCurrentImage[selectedIndex];
    String pet = localCurrentString[selectedIndex];
    setIcon(icon);
    if (icon != null) {
        setText(pet);
        setFont(list.getFont());
    } else {
        setNormalFont(pet + " (no image available)",
                    list.getFont());
    }

    return this;
}

//Set the font and text when no image was found.
protected void setNormalFont(String uhOhText, Font normalFont) {
    if (normalFont == null) { //lazily create this font
        normalFont = normalFont.deriveFont(Font.ITALIC);
    }
    setFont(normalFont);
    setText(uhOhText);
}

}

Sorry for this wall of code, but I doubt I could visualize it just with words.

Charles Nough
  • 353
  • 1
  • 3
  • 15
  • What line produces the NPE? Can you share a stacktrace please? – Mureinik Oct 23 '16 at 12:32
  • 1
    Your `localCurrentImage` array field inside `ComboBoxRenderer` class is not instantiated, that's why when you call `localCurrentImage[i] = currentImage[i]` inside `ComboBoxRenderer` class constructor it gives you `NullPointerException`. – Naruto Biju Mode Oct 23 '16 at 12:51
  • @NarutoBijuMode Oh, this kind of mistake. Oh well, thank you very much! – Charles Nough Oct 23 '16 at 12:55

0 Answers0