Ryan Fitzmaurice
Ryan Fitzmaurice

Reputation: 1

How to fix "Exception in thread "main" java.lang.NullPointerException"

I'm writing a java program, messed around with it trying to make a new panel and somehow screwed it up and won't let me run the program. I rolled back to the last point in which it was working but still no good. I don't know where I went wrong. From the error I can only assume that I messed u somewhere between the main method and the makeGUI method, but I can't see anything wrong.

The error I got was;

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl<Container.java:1090>
at java.awt.Container.add<Container.java:966>
at assignment2g2.buildGUI<assignment2g2.java:47>
at assignment2g2.<init><assignment2g2.java:31>
at assignment2g2.main<assignment2g2.java:124>

and My code is ...

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.MouseInputListener;

import java.awt.*;
import java.awt.event.*;

public class assignment2g2 extends JFrame implements MouseInputListener {

JPanel mousePanel, actionsPanel;
JLabel mouseLabel;
JLabel topLeft,topMid,topRightMid,topRight,midLeft,midMid,midRightMid,midRight,btmLeft,btmMid,btmRightMid,btmRight;
ImageIcon circle = new ImageIcon("circle.jpg");
ImageIcon square = new ImageIcon("square.jpg");
ImageIcon triangle = new ImageIcon("triangle.jpg");

//JButton button1=new JButton("?");
//JButton button2=new JButton("?");

public assignment2g2() {


    buildGUI();
    buildActionsPanel();
    mousePanel.add(actionsPanel);
    getContentPane().add(mousePanel);

    setSize(800,400);
    setVisible(true);

}

public void buildGUI() {



    mousePanel = new JPanel();
    mousePanel.setLayout(new BorderLayout());
    mousePanel.add(mouseLabel,BorderLayout.SOUTH);
    mousePanel.addMouseListener(this);
    mousePanel.addMouseMotionListener(this);

    mouseLabel = new JLabel();
    mouseLabel.setBorder(new LineBorder(Color.BLACK));
    mouseLabel.setForeground(Color.RED);
    mouseLabel.setText("Please select an answer");

    actionsPanel = new JPanel();
    actionsPanel.setLayout(new GridLayout(3,3));


}

public void buildActionsPanel() {

    topLeft = new JLabel();

    topMid = new JLabel();
    //topMid.setIcon(circle);
    topMid.setText("circle");


    topRightMid = new JLabel();

    topRight = new JLabel();

    midLeft = new JLabel();
    midLeft.setBorder(new LineBorder(Color.BLACK));
    midLeft.addMouseListener(this);
    midLeft.setText("Square");

    midMid = new JLabel();
    midMid.setBorder(new LineBorder(Color.BLACK));
    midMid.addMouseListener(this);
    midMid.setText("Triangle");

    /*midRightMid = new JLabel();
    midRightMid.setBorder(new LineBorder(Color.BLACK));
    midRightMid.addMouseListener(this);
    midRightMid.setText("Circle");*/

    midRight = new JLabel();
    midRight.setBorder(new LineBorder(Color.BLACK));
    midRight.addMouseListener(this);
    midRight.setText("Circle");

    btmLeft = new JLabel();

    btmMid = new JLabel();

    btmRightMid = new JLabel();

    btmRight= new JLabel();




    actionsPanel.add(topLeft);
    actionsPanel.add(topMid);
    //actionsPanel.add(topRightMid);
    actionsPanel.add(topRight);
    actionsPanel.add(midLeft);
    actionsPanel.add(midMid);
    //actionsPanel.add(midRightMid);
    actionsPanel.add(midRight);
    actionsPanel.add(btmLeft);
    actionsPanel.add(btmMid);
    //actionsPanel.add(btmRightMid);
    actionsPanel.add(btmRight);


}


public static void main(String[] args) {
    new assignment2g2();
}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}






public void mousePressed(MouseEvent e) {
    mouseLabel.setText("Mouse Pressed Event");
    //if(e.getSource() instanceof JLabel) {
    //  JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", JOptionPane.INFORMATION_MESSAGE);
    //}     


    if(e.getSource()==midRight) {
        if((topMid.getText()).matches("circle")){
            JOptionPane.showMessageDialog(null, "Correct", "Correct", JOptionPane.INFORMATION_MESSAGE);
            //topMid.setIcon(square);
            topMid.setText("square");
        }
        else{
            JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    if(e.getSource()==midMid) {
        if((topMid.getText()).matches("triangle")){
            JOptionPane.showMessageDialog(null, "Correct", "Correct", JOptionPane.INFORMATION_MESSAGE);
            //topMid.setIcon(square);
            topMid.setText("circle");
        }
        else{
            JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect",        JOptionPane.INFORMATION_MESSAGE);
        }
    }
    if(e.getSource()==midLeft) {
        if((topMid.getText()).matches("square")){
            JOptionPane.showMessageDialog(null, "Correct", "Correct", JOptionPane.INFORMATION_MESSAGE);
            //topMid.setIcon(square);
            topMid.setText("triangle");
        }
        else{
            JOptionPane.showMessageDialog(null, "Incorrect", "Incorrect", JOptionPane.INFORMATION_MESSAGE);
        }
    }

}

public void mouseReleased(MouseEvent e) {   
}

public void mouseDragged(MouseEvent e) {    
}

public void mouseMoved(MouseEvent e) {  
}

}

If someone could help me out, it would be great. Sorry in advance, I know the code is in a bit of a mess :L

Upvotes: 0

Views: 1993

Answers (2)

Paul Okeke
Paul Okeke

Reputation: 1414

You are adding mouseLabel to the mousePanel when mouseLabel hasn't being initialized. This is why you have a nullPointerException.

Instead You should do this

public void buildGUI() {



mousePanel = new JPanel();
mousePanel.setLayout(new BorderLayout());
mousePanel.addMouseListener(this);
mousePanel.addMouseMotionListener(this);

mouseLabel = new JLabel();
mouseLabel.setBorder(new LineBorder(Color.BLACK));
mouseLabel.setForeground(Color.RED);
mouseLabel.setText("Please select an answer");

mousePanel.add(mouseLabel,BorderLayout.SOUTH);/*Add The mousePanel Object after it has being initialized*/

actionsPanel = new JPanel();
actionsPanel.setLayout(new GridLayout(3,3));
}

I hope This Helps Bro.

Upvotes: 0

Happy Cupz Cupz
Happy Cupz Cupz

Reputation: 181

is because you assign

mousePanel.add(mouseLabel,BorderLayout.SOUTH);

before initializing the mouseLabel.

mouseLabel = new JLabel();

Upvotes: 1

Related Questions