unable to access variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    unable to access variables

    I was creating a swing applet if i make a seperate class of action listener i was unable to access extended class variables.Can someone explain my mistake and modify the code without merging two classes.


    Code:
    import javax.swing.*;
    import java.awt.*;
    public class Clock extends JApplet{
    JFrame frame;
    JPanel panel;
    JButton button;
    public void init(){
    frame = new JFrame();
    frame.setSize(700,600);
    panel = new JPanel();
    button = new JButton();
    button.addActionListener(new listener());     //if i use seperate class listener instead of writing code here itself and try to access the variables of this class null pointer exception occurs
    button.setText("aaaaaaa");
    panel.add(button);
    frame.add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
    Code:
    import java.awt.event.*;
    class listener extends Clock implements ActionListener {
    public void actionPerformed(ActionEvent e){
    button.setText("bbbbbbbbbbb");
    }
    }
  • rajujrk
    New Member
    • Aug 2008
    • 107

    #2
    Hi jinnejeevansai,

    Make that variable as protected, default Access Modifier is "default" and it will be accessed within the package. When you extending the class and trying to access the class variable, use protected as always.

    Thanks
    Rajkumar

    Comment

    Working...