Loading Panel with Image in jFrame: Component must be non-null

401 views Asked by At

Hello I'm a newbie here,

I want to add pictures in my JFrame. I used the Grouplayout and tried to load my Image with help of this JPanel-Class. But when I use ".addComponent(Image)" it gave me the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Component must be non-null at javax.swing.GroupLayout$ComponentSpring.(Unknown Source) at javax.swing.GroupLayout$ComponentSpring.(Unknown Source) at javax.swing.GroupLayout$Group.addComponent(Unknown Source) at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source) at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source) at javax.swing.GroupLayout$Group.addComponent(Unknown Source) at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source) at infpp.OceanLife.OceanLifeGUI.initializeGUI(OceanLifeGUI.java:237) at infpp.OceanLife.OceanLifeGUI.(OceanLifeGUI.java:50) at infpp.OceanLife.OceanLifeController.start(OceanLifeController.java:39) at infpp.OceanLife.OceanLifeMain.main(OceanLifeMain.java:22)

MyCode:

private Images image;


        GroupLayout.ParallelGroup drawHorizontal = drawLayout.createParallelGroup(Alignment.LEADING);

        drawHorizontal.addComponent(image);


        GroupLayout.ParallelGroup drawVertical = drawLayout.createParallelGroup(Alignment.LEADING);

        drawVertical.addComponent(image);

Edit:

ImageClass

    public class Image extends JPanel{
OceanInterface ocean;

/**
 * All Images that display the ocean
 */

public static BufferedImage oceanImage, 
            fishImage1, fishImage2, fishImage3,
            plantImage1,plantImage2, bubbleImage1, 
            bubbleImage2, bubbleImage3, 
            stoneImage1, stoneImage2, sharkImage;

public Image() {
   try {                
       oceanImage = ImageIO.read(new File("media/ocean.png"));
   } catch (IOException ex) {
       // If there is an error:
       final JOptionPane optionPane
                       = new JOptionPane();
       JOptionPane.showMessageDialog(
                       optionPane,
                       "Fail.",
                       "ERROR",
                       JOptionPane.ERROR_MESSAGE);
       System.exit(0);
   }
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(oceanImage, 0, 0, null); // see javadoc for more info on the parameters            
}

}

Whole Code of GUI:

package infpp.OceanLife;


    import java.awt.Color;
    import java.awt.Dimension;
    import java.util.LinkedList;

    import javax.swing.BorderFactory;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;



    public class OceanLifeGUI extends JFrame{

/**
 * Ocean the you want to control and get displayed
 */
public OceanInterface ocean;

private Image image;



/**
 * OceanGraphic object which contains the oceans drawing
 */
private OceanLifeGraphic oceanGraphic;


/**
 * Method to create a new GUI with a given Ocean
 *
 * @param oc - OceanInterface you want to control and get displayed
 */
public OceanLifeGUI(OceanInterface oc) {

    // Set the ocean
    this.ocean = oc;

    // Initialize the objects of the GUI
    initializeGUI();
}

private void initializeGUI(){

    // Create new JFrame
    JFrame OceanLife = new JFrame();

    // Default exit
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    // Create Panels (layout, oceanDraw, oceanControl)
    JPanel oceanLayout = new JPanel();
    JPanel oceanDraw = new JPanel();
    JPanel oceanControl = new JPanel();

    // Layoutmanager
    GroupLayout layout = new GroupLayout(oceanLayout);
    GroupLayout drawLayout = new GroupLayout(oceanDraw);
    GroupLayout controlLayout = new GroupLayout(oceanControl);

    // Set Layout
    oceanDraw.setLayout(drawLayout);
    oceanControl.setLayout(controlLayout);

    // Creates Buttons
    JButton loadButton = new JButton("Load");
    JButton saveButton = new JButton("Save");
    JButton quitButton = new JButton(" Quit ");
    JButton startButton = new JButton("Start");
    JButton stepButton = new JButton("Step ");
    JButton stopButton = new JButton("Stop ");

    JButton addButton = new JButton("ADD");
    addButton.setPreferredSize(new Dimension (200,20));
    JButton deselectButton = new JButton("DESELECT");
    JButton removeButton = new JButton("REMOVE");
    JButton clearButton = new JButton("CLEAR");

    // Create Labels
    JLabel gameControlLabel = new JLabel("Gamecontrol");
    JLabel objectControlLabel = new JLabel("Objectcontrol");
    JLabel xLabel = new JLabel("x");
    JLabel yLabel = new JLabel("y");

    // Create Textfields
    JTextField xInput = new JTextField("0");
    JTextField yInput = new JTextField("0");

    // Create ComboBox
    JComboBox objectSelectBox = new JComboBox(new String[] { "Fish", "Plant", "Stone", "Bubble" });

    // Create List
    JList objectSelectList = new JList(new String[] { "Fish", "Plant", "Stone", "Bubble" });

    JScrollPane objectSelectScroll = new JScrollPane(objectSelectList);




    // Control Panel
    GroupLayout.ParallelGroup controlHorizontal = controlLayout.createParallelGroup();

        GroupLayout.ParallelGroup controlPanel = controlLayout.createParallelGroup(Alignment.CENTER);

        // GamecontrolLabel (1)

        // GamecontrolButtons (2)
        GroupLayout.SequentialGroup gameControlButtons = controlLayout.createSequentialGroup();

            GroupLayout.ParallelGroup columnLeft = controlLayout.createParallelGroup();
                columnLeft.addComponent(loadButton);
                columnLeft.addComponent(startButton);

            GroupLayout.ParallelGroup columnMiddle = controlLayout.createParallelGroup();
                columnMiddle.addComponent(saveButton);
                columnMiddle.addComponent(stepButton);

            GroupLayout.ParallelGroup columnRight = controlLayout.createParallelGroup();
                columnRight.addComponent(quitButton);
                columnRight.addComponent(stopButton);

            gameControlButtons.addGroup(columnLeft);
            gameControlButtons.addGap(5,5,5);
            gameControlButtons.addGroup(columnMiddle);
            gameControlButtons.addGap(5,5,5);
            gameControlButtons.addGroup(columnRight);

        // ObjectcontrolLabel (3)

        // ObjectSelect and ADD (4)
        GroupLayout.SequentialGroup objectSelect = controlLayout.createSequentialGroup();
            objectSelect.addComponent(objectSelectBox);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(xLabel);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(xInput);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(yLabel);
            objectSelect.addGap(5,5,5);
            objectSelect.addComponent(yInput);

        // ADD-Button (5)

        // ObjectSelectList with ScrollPanel (6)

        // DESELECT-Button (7)

        // REMOVE-Button (8)

        // CLEAR-Button (9)

        // Call controlPanel-Components
        controlPanel.addComponent(gameControlLabel);
        controlPanel.addGroup(gameControlButtons);
        controlPanel.addComponent(objectControlLabel);
        controlPanel.addGroup(objectSelect);
        controlPanel.addComponent(addButton);
        controlPanel.addComponent(objectSelectScroll);
        controlPanel.addComponent(deselectButton);
        controlPanel.addComponent(removeButton);
        controlPanel.addComponent(clearButton);


  // Call Horizontal-Groups
   controlHorizontal.addGroup(controlPanel);



   GroupLayout.SequentialGroup controlVertical = controlLayout.createSequentialGroup();

        GroupLayout.ParallelGroup firstRow = controlLayout.createParallelGroup();
            firstRow.addComponent(loadButton);
            firstRow.addComponent(saveButton);
            firstRow.addComponent(quitButton);

        GroupLayout.ParallelGroup secondRow = controlLayout.createParallelGroup();
            secondRow.addComponent(startButton);
            secondRow.addComponent(stepButton);
            secondRow.addComponent(stopButton);


        GroupLayout.ParallelGroup thirdRow = controlLayout.createParallelGroup(Alignment.BASELINE);
            thirdRow.addComponent(objectSelectBox);
            thirdRow.addComponent(xLabel);
            thirdRow.addComponent(xInput);
            thirdRow.addComponent(yLabel);
            thirdRow.addComponent(yInput);

        GroupLayout.ParallelGroup fourthRow = controlLayout.createParallelGroup(Alignment.BASELINE);
            fourthRow.addComponent(addButton, 200,200,200);




        // Call Vertical-Groups
        controlVertical.addComponent(gameControlLabel);
        controlVertical.addGap(5,5,5);
        controlVertical.addGroup(firstRow);
        controlVertical.addGap(5,5,5);
        controlVertical.addGroup(secondRow);
        controlVertical.addGap(200,200,200);
        controlVertical.addComponent(objectControlLabel);
        controlVertical.addGap(5,5,5);
        controlVertical.addGroup(thirdRow);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(addButton);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(objectSelectScroll);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(deselectButton);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(removeButton);
        controlVertical.addGap(5,5,5);
        controlVertical.addComponent(clearButton);
        controlVertical.addGap(5,5,5);






    // OceanDraw

    GroupLayout.ParallelGroup drawHorizontal = drawLayout.createParallelGroup(Alignment.LEADING);

        drawHorizontal.addComponent(image);


        GroupLayout.ParallelGroup drawVertical = drawLayout.createParallelGroup(Alignment.LEADING);

        drawVertical.addComponent(image);





    // Layout
    GroupLayout.ParallelGroup layoutHorizontal = layout.createParallelGroup();

        layoutHorizontal.addComponent(oceanDraw);
        layoutHorizontal.addComponent(oceanControl);

    GroupLayout.SequentialGroup layoutVertical = layout.createSequentialGroup();

        layoutVertical.addComponent(oceanControl);
        layoutVertical.addComponent(oceanDraw);






    controlLayout.setHorizontalGroup(controlHorizontal);
    controlLayout.setVerticalGroup(controlVertical);

    drawLayout.setHorizontalGroup(drawHorizontal);
    drawLayout.setVerticalGroup(drawVertical);

    layout.setHorizontalGroup(layoutHorizontal);
    layout.setVerticalGroup(layoutVertical);



    // Add Panels (OceanDraw, OceanControl) to jFrame (OceanLife)
    OceanLife.add(oceanLayout);
    //OceanLife.add(oceanDraw);

    oceanDraw.setBackground(Color.black);
    oceanDraw.setPreferredSize(new Dimension (1000,600));


    // Pack?
    OceanLife.pack();
    OceanLife.setVisible(true);
    OceanLife.setResizable(false);
    OceanLife.setSize(1220, 650);
    }

}

1

There are 1 answers

0
Blip On BEST ANSWER

Your problem is that you have declared the variable image but did not initialise it. So the variable is null when you are using the variable image in drawHorizontal.addComponent(image);.

So, my suggestion is that you initialise the variable image like :

image = new Image();

before the line drawHorizontal.addComponent(image);