polaris
polaris

Reputation: 339

error when ActionPerformed is activated

I have JPanel with a button which when press should send me to another screen, however I keep getting an error. I tried googling it but couldn't any information about it.

full code

import javax.swing.JFrame;
import javax.swing.JTextField; 
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import javax.swing.ButtonGroup;//to put radio buttons together

public class Graphics extends JFrame {//this class is to display the GUI on the screen

private JButton enter;//enter on login
private JButton exit; //exit program
private JButton button1; //enter on menu
private JButton button2; //enter on withdraw
private JButton button3;  //enter on deposit
private JButton back;//button to return to main menu from other options

private JRadioButton balance; //menu for operation selection
private JRadioButton withdraw;
private JRadioButton deposit;
private ButtonGroup buttonMenu;//to put radio buttons together

private JTextField loginBox; //to enter username
private JPasswordField password; 
private JTextField input1;//to enter withdraw amount
private JTextField input2;//deposit amount

private JPanel loginPanel;//first screen
private JPanel menuPanel;
private JPanel withdrawPanel;
private JPanel depositPanel;
private JPanel balancePanel;
private JPanel setPanel; //to hold the other panels
private CardLayout cl;//to set the different panels

private int login1;
private int password1;
private double login2;
private double password2;


ATM atmObject = new ATM();

public Graphics(){ //constructor

super("My ATM");

//ATM object = new ATM();//object to call methods in ATM
                       //to access information stored

CardLayout cl = new CardLayout();
setPanel =  new JPanel();//panel to store the other panels
setPanel.setLayout(cl);//create the different set of windows

/***define and initialize all buttons, labels etc.***/
JLabel login = new JLabel("Login"); //to put next to login field
JLabel passcode = new JLabel("Password");   

loginBox = new JTextField(6);//to enter login
password = new JPasswordField(6);//to enter password
input1 = new JTextField(6);
input2 = new JTextField(6);

enter = new JButton("Enter");
back = new JButton("GO Back");

exit = new JButton("Exit");
button1 = new JButton("Enter");//button for main menu
button2 = new JButton("Enter");//button for withdraw
button3 = new JButton("Enter");//button for deposit

balance =  new JRadioButton("balance inquiry", false);//menu for operation selection
withdraw = new JRadioButton("withdraw from account", false);
deposit =  new JRadioButton("deposit to account", false);
/***end of definitions***/

/*****set-up login screen****/
loginPanel = new JPanel();

loginPanel.add(login);//label
loginPanel.add(loginBox);//textbox
loginPanel.add(passcode);//label
loginPanel.add(password);//input password
loginPanel.add(enter);

loginPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(loginPanel, "LoginScreen");//add login screen to panel and set 
                                          //items orientation to the left
//here add code to check password on file
//obtain password as set of characters
//int cool = Integer.parseInt(loginBox.getText()); in a single step
//String inputA = loginBox.getText();//obtain input from user as a string
//JTextField inputB = new JTextField();

//login1 = Integer.parseInt(inputA); //convert it to int so it can be used

//char[] passcode2 = password.getPassword(); //create array of chars to get password
//password1 = Integer.parseInt(String.valueOf(passcode2));//changing characters to int

/****setup main menu***/
menuPanel = new JPanel();
buttonMenu = new ButtonGroup();
menuPanel.add(balance);
menuPanel.add(withdraw);
menuPanel.add(deposit);
//make all radio buttons connected
buttonMenu.add(balance);
buttonMenu.add(withdraw);
buttonMenu.add(deposit);

menuPanel.add(button1);//add entebr button
menuPanel.add(exit);

menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(menuPanel, "MainMenu");//add window to display

/***setup balance inquiry screen***/

balancePanel = new JPanel();
JLabel balance = new JLabel("the balance in your account is :");
JTextField balanceDisplay = new JTextField(5);//to display balance
balanceDisplay.setEditable(false);

//here code to call balance from ATM
balancePanel.add(balance);
balancePanel.add(balanceDisplay);
balancePanel.add(back);
balancePanel.add(exit);

balancePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(balancePanel, "Blance");//add window to display
/**end of balance inquiry**/

/****setup screen to withdraw money***/
withdrawPanel = new JPanel();
JLabel message = new JLabel("enter amount to withdraw");
withdrawPanel.add(message);//display message
withdrawPanel.add(input1);
withdrawPanel.add(button2);
withdrawPanel.add(back);
withdrawPanel.add(exit);

withdrawPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
//here code to call method to withdraw money
setPanel.add(withdrawPanel);//add window to display

/***setup screen to deposit***/

depositPanel = new JPanel();
JLabel message2 = new JLabel("Enter amount to deposit");
depositPanel.add(message2);
depositPanel.add(input2);
depositPanel.add(back);
depositPanel.add(exit);

depositPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(depositPanel, "Deposit");
/***end deposit panel***/


getContentPane().add(setPanel);

//create event handler and make buttons events
theHandler handler = new theHandler();//builds an action listener object
enter.addActionListener(handler);
exit.addActionListener(handler);
back.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);

//add the frame to the window


//boolean access = atmObject.Info(id, password1);
//int i;
//  if(access == true){
    //enter.addActionListener(handler);//if login is correct call the event handler


 }
 //class inside class
 //inner class inherits everything from outer class
private class theHandler implements ActionListener{//this class handles the events
                                             //waits for an event to happen and execute the code

    public void actionPerformed(ActionEvent event){//actionPerformed is a built-in method
                                                  //ActionEvent is also built-in
        //String string = "";

        boolean info = false;//to check if the login and password are correct

        if(event.getSource()== enter){//getSource = where the event occurred 
            JOptionPane.showMessageDialog(null, "Hello");

            //String inputA = loginBox.getText();//obtain input from user as a string

        //  login1 = Integer.parseInt(inputA); //convert it to int so it can be used

            //char[] passcode2 = password.getPassword(); //create array of chars to get password
            //password1 = Integer.parseInt(String.valueOf(passcode2));//changing characters to int


            //info = atmObject.Info(login1, password1);

        //  if(info==true)

            cl.show(setPanel, "MainMenu");

        }


    }
}

I placed the popup to see if the button was the problem, but the popup shows when the button is pressed. So the event handling is working for the button. The message I keep getting when I press the button.

Graphics$theHandler.actionPerformed
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)  
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

It's all like this. I have a similar code for another program but it doesn't give me any errors. Why does it say unknown source?

this is my main class

import javax.swing.JFrame;

 public class Display {//this class is to call the other methods


  public static void main(String[] args) {


    Graphics atmObject = new Graphics();
    atmObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    atmObject.setSize(350, 100);
    atmObject.setVisible(true);

}
}

I have another class, however I'm not using it in my code yet. I created an object for my other ATM class but it's not being used in the code yet, so it shouldn't interfere with the code.

Upvotes: 1

Views: 1308

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347244

Without more information, it's impossible to be 100%, but I believe your are shadowing your variables.

My assumption is based on the following...

// Here, you create a local copy of the CardLayout
CardLayout cl = new CardLayout();
// But here it seems you are creating a class instance of the setPanel...
setPanel =  new JPanel();//panel to store the other panels
setPanel.setLayout(cl);//create the different set of windows

JButton enter = new JButton("Enter");
JPanel loginPanel = new JPanel();

loginPanel.add(enter);

And...

// You create a instance of some handler (presumably a implementation of ActionListener)
// which suggests that the handler is not (at least) an anonymous inner class...
theHandler handler = new theHandler();//builds an action listener object
enter.addActionListener(handler)
loginPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(loginPanel, "LoginScreen");

And finally...

if(event.getSource()== enter){//getSource = where the event occurred 
    JOptionPane.showMessageDialog(null, "Hello");
    // You can reference "cl", which suggests that the program
    // can compile, meaning the "cl" must be declared some where
    // and based on the method you are accessing, is probably
    // a CardLayout...which hasn't being initialised...
    cl.show(setPanel, "MainMenu"); <-----here is the problem

Update based on the whole code

Okay, in you Graphics class, you are declaring an cl instance variable of type CardLayout....

public class Graphics extends JFrame {//this class is to display the GUI on the screen

    /*...*/
    private CardLayout cl;//to set the different panels

Then in your constructor...

public Graphics(){ //constructor
    /*...*/
    CardLayout cl = new CardLayout();

You are declaring it AGAIN...this means the the cl in the constructor is only valid and viable within the constructor. Once the constructor exists, the reference is no longer valid. This leaves the instance variable cl uninitialised...OR null

THEN...in your actionPerformed method, you are referencing the instance variable cl which is null...

private class theHandler implements ActionListener{//this class handles the events
                                         //waits for an event to happen and execute the code
    public void actionPerformed(ActionEvent event){//actionPerformed is a built-in method
                                                  //ActionEvent is also built-in
        /*...*/
        // Then you try and reference the instance variable which is null
        cl.show(setPanel, "MainMenu");

    }
}

In you constructor, change...

    CardLayout cl = new CardLayout();

To...

    cl = new CardLayout();

Also...

  • Java classes use CamelCase convention. I recommend that you take a look at Code Conventions for the Java Programming Language. It will make your code much more readable.
  • Graphics is already a defined class in java.awt. I HIGHLY recommend that you avoid using it yourself as it will only confuse you and others in the long run...

Upvotes: 1

camickr
camickr

Reputation: 324128

Graphics$theHandler.actionPerformed
  1. Class names should start with an upper case character. "theHandler" should be "TheHandler" (Well actually it should be a better descriptive name).

  2. Looks like your class name is "Graphics". This is already a JDK class name. Use a different name.

Doubt these suggestions will solve your problem. For that we would need a SSCCE.

Upvotes: 1

Related Questions