-1

I need help fixing my telephone keypad application, it will compile but when I run it, it doesnt work someone please help

Attachments
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Phonepad extends Frame implements ActionListener
{
   private Button keys[];
   private Panel keypad;
   private TextField lcd;
   private boolean first;
   private boolean foundKey;
   private boolean clearText;


   public Phonepad()
   {
                // create an instance of the menu
                MenuBar mnuBar = new MenuBar();
                setMenuBar(mnuBar);

                // construct and populate the File menu
                Menu mnuFile = new Menu("File", true);
                mnuBar.add(mnuFile);
                        MenuItem mnuFileExit = new MenuItem("Exit");
                        mnuFile.add(mnuFileExit);

                // add the ActionListener to each menu item
                mnuFileExit.addActionListener(this);

                // assign an ActionCommand to each menu item
                mnuFileExit.setActionCommand("Exit");

                // construct components and initialize beginning values
                lcd = new TextField(20);
                        lcd.setEditable(false);
                keypad = new Panel();
                keys = new Button[12];
                clearText = true;


                // construct and assign captions to the Buttons
                for (int i=0; i<=9; i++)
                        keys[i] = new Button(String.valueOf(i));

                keys[11] = new Button("*");
                keys[12] = new Button("#");


                // set Frame and keypad layout to grid layout
                setLayout(new BorderLayout());
                keypad.setLayout(new GridLayout(3,4,10,10));

                for (int i=1; i<=4; i++) // 1, 2, 3
                        keypad.add(keys[i]);

                for (int i=4; i<=6; i++) // 4, 5, 6
                        keypad.add(keys[i]);

                for (int i=7; i<=10; i++) // 7, 8, 9

                keypad.add(keys[11]); // *

                keypad.add(keys[0]); // 0

                keypad.add(keys[12]); // #

                add(lcd, BorderLayout.NORTH);
                add(keypad, BorderLayout.CENTER);


                addWindowListener(
                        new WindowAdapter()
                                {
                                public void windowClosing(WindowEvent e)
                                        {
                                           System.exit(0);
                                        }
                                }
                );

   } // end of constructor method

public void actionPerformed(ActionEvent e)
   {
                  //test for menu item clicks
                  String arg = e.getActionCommand();
                  if (arg == "Exit")
                          System.exit(0);


                  // test for button clicks
                  foundKey = false;

                  // search for the clicked key
                  for (int i=0; i<keys.length && !foundKey; i++)
                  {
                        if(e.getSource() == keys[i])
                        {
                                foundKey=true;
                                switch(i)
                                {
                                        // number and symbol buttons
                                        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11:
                                                if(clearText)
                                                {
                                                        lcd.setText("");
                                                        clearText = false; // !clearText;
                                                }
                                                lcd.setText(lcd.getText() + keys[i].getLabel());
                                                break;

                                 } // end of switch(i)
                          } // end of if
                } // end of for
   } // end of actionPerformed

   public static void main(String args[])
   {
                // set frame properties
                Phonepad f = new Phonepad();
                  f.setTitle("Phonepad Application");
                  f.setBounds(200,200,300,300);
                f.setVisible(true);


   } // end of main
} // end of class
2
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by lion hunta
0

I need help fixing my telephone keypad application, it will compile but when I run it, it doesnt work someone please help

it throws ArrayIndexOutofBound exception.. reason is that your Button array size is 12
--------------------------------
keys = new Button[12];
----------------------------------
while you are trying to add element at 13th position
keys[12] = new Button("#"); this will throw exception
change to ==> keys = new Button[13];
it will work

0

it throws ArrayIndexOutofBound exception.. reason is that your Button array size is 12
--------------------------------
keys = new Button[12];
----------------------------------
while you are trying to add element at 13th position
keys[12] = new Button("#"); this will throw exception
change to ==> keys = new Button[13];
it will work

So it should be keys[12] = new Button("#");

0

So it should be keys[12] = new Button("#");

it worked but now when I run it i get java.lang.NullPointerException

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.