Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need Help with java coding error "Exception in thread "main" java.lang.NullPointerException"

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Need Help with java coding error "Exception in thread "main" java.lang.NullPointerException"

    //needed for swing classes for all of the graphical components created in the GUI
     import javax.swing.*; 
     
    //needed for the actionlistener class for events such as clicking a button in the application
     import java.awt.*;
     
    /*
    Needed for for ActionListener Interface for buttons performing a certain action  such as   calculating
    */
     import java.awt.event.*; 
     
    // this for your calculation to come out in a dollar format 
     import java.text.DecimalFormat;
     
        // the class header which marks the start of a class definition
      public class Package_Selector_GUI extends JFrame
        //extends JFrame class to inherit members from the JFrame class
     
    {
     
        private JPanel packagePanel;
        //private JPanel phonePanel;
        //private JPanel optionPanel;
        //private JTextField PhoneTextField;// to hold the Phone input
        private    JTextField PackageTextField; // to hold the Package input
        //private JTextField OptionTextField;  // to hold the Option input
        private JMenuBar menuBar;             // the menu bar
        private JMenu FileMenu;           // the file menu
        private JMenu PhoneMenu;         // the phone menu
        private JMenu PackageMenu;        // the package menu
        private JMenu OptionMenu;        // the option menu
        private JMenuItem exitItem;      // the to exit
        private JRadioButtonMenuItem Package1; //to select 300minutes
            private JRadioButtonMenuItem Package2; // to select 800minutes
        private JRadioButtonMenuItem Package3; // to select 1500minutes
        private JRadioButtonMenuItem Model_100; // to select phone option1
        private JRadioButtonMenuItem Model_110 ; // to select phone option2
        private JRadioButtonMenuItem Model_200;  // to select phone option3
        private JCheckBoxMenuItem    VoiceMail; // to select option1
        private JCheckBoxMenuItem    TextMessage; // to select option2
        private JLabel messageLabel;
     
     
      public Package_Selector_GUI()
       {
        //to display a title
        setTitle("Anthony's GetItGotIt Phone Service");
     
        // specifying an action for the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     
        // creating borderlayout manager
        setLayout(new BorderLayout());
     
        //command to build the button panel
        buildPackagePanel();
     
        add(packagePanel);
     
        //command to build the MenuBar
        buildMenuBar();
     
        //packing all the contents of the window in the display and setting it visible
        pack();
        setVisible(true);
       }
     
        //the build MenuBar method starts building the MenuBar
      private void buildMenuBar()
        {
        // creating the menu bar
        menuBar = new JMenuBar();
     
        //creating the four menu tabs
        buildFileMenu();
        buildPhoneMenu();
        buildPackageMenu();
        buildOptionMenu();
     
        // adding the menu tabs to the menu bar
        menuBar.add(FileMenu);
        menuBar.add(PhoneMenu);
        menuBar.add(PackageMenu);
        menuBar.add(OptionMenu);
     
        // setting the windows menu bar
        setJMenuBar(menuBar);
        }
     
        //the build FileMenu method starts building the FileMenu
      private void buildFileMenu()
        {
        //creating an exit item for the file menu
        exitItem = new JMenuItem("Exit");
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.addActionListener(new ExitListener());
     
        //creating the Jmenu object for the file menu
        FileMenu = new JMenu("File");
        FileMenu.setMnemonic(KeyEvent.VK_F);
     
        //adding the exit item to the the file menu
        FileMenu.add(exitItem);
        }
     
        //the build PhoneMenu method starts building the PhoneMenu
      private void buildPhoneMenu()
        {
        //creating the radio buttons for user to choose what phone they desire
        Model_100 = new JRadioButtonMenuItem ("Model_100",true);
        Model_100.setMnemonic(KeyEvent.VK_P);
     
     
     
        Model_110 = new JRadioButtonMenuItem ("Model_110");
        Model_110.setMnemonic(KeyEvent.VK_H);
     
     
     
        Model_200 = new JRadioButtonMenuItem ("Model_200");
        Model_200.setMnemonic(KeyEvent.VK_O);
     
        //grouping radio buttons together
        ButtonGroup  bg = new ButtonGroup();
        bg.add(Model_100);
        bg.add(Model_110);
        bg.add(Model_200);
     
        //creating the Jmenu object for the Phone menu
        PhoneMenu = new JMenu("Phone");
        PhoneMenu.setMnemonic(KeyEvent.VK_N);
     
        //adding the menu items to the the phone menu
        PhoneMenu.add(Model_100);
        PhoneMenu.add(Model_110);
        PhoneMenu.add(Model_200);
       }
     
     
     
        //the build PackageMenu method starts building the PackageMenu
      private void buildPackageMenu()
        {
     
        //creating the radio buttons for user to choose what Package they desire
        Package1 = new JRadioButtonMenuItem ("300minutes",true);
        Package1.setMnemonic(KeyEvent.VK_A);
     
     
     
        Package2 = new JRadioButtonMenuItem ("800minutes");
        Package2.setMnemonic(KeyEvent.VK_G);
     
     
     
        Package3 = new JRadioButtonMenuItem ("1500minutes");
        Package3.setMnemonic(KeyEvent.VK_E);
     
        //grouping radio buttons together
        ButtonGroup  bg = new ButtonGroup();
        bg.add(Package1);
        bg.add(Package2);
        bg.add(Package3);
     
        //creating the Jmenu object for the Package Menu
        PackageMenu = new JMenu("Package");
        PackageMenu.setMnemonic(KeyEvent.VK_S);
     
     
        //adding the menu items to the the Package Menu
        PackageMenu.add(Package1);
        PackageMenu.add(Package2);
        PackageMenu.add(Package3);
       }
     
        //the build OptionMenu method starts building the OptionMenu
      private void buildOptionMenu()
        {
        //creating the radio buttons for user to choose what Package they desire
        VoiceMail = new JCheckBoxMenuItem ("VoiceMail",true);
        VoiceMail.setMnemonic(KeyEvent.VK_V);
     
     
        TextMessage = new JCheckBoxMenuItem  ("TextMessaging");
        TextMessage.setMnemonic(KeyEvent.VK_T);
     
        //creating the Jmenu object for the Option Menu
        OptionMenu = new JMenu("Options");
        OptionMenu.setMnemonic(KeyEvent.VK_R);
     
        //adding the menu items to the the Option Menu
        OptionMenu.add(VoiceMail);
        OptionMenu.add(TextMessage);
       }
     
     
     
      private void buildPackagePanel()
        {
        double Package1_rate = 45.00;
        double Package2_rate = 65.00;
        double Package3_rate = 99.00;
        String input;
     
        input = PackageTextField.getText();
        messageLabel = new JLabel("Package desired");
        PackageTextField = new JTextField(10); 
     
        if (Package1.isSelected())
           Package1_rate = Double.parseDouble(input);
     
        else if (Package2.isSelected())
                Package2_rate = Double.parseDouble(input);
     
        else if (Package3.isSelected())
            Package3_rate = Double.parseDouble(input);
     
        packagePanel = new JPanel();
        packagePanel.add(messageLabel);
        packagePanel.add(PackageTextField);
     
      }
     
     
        /*
        private inner class that handles the event when the exit button is clicked
        */
     private class ExitListener implements ActionListener
       {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
       }
     
     
        /* 
           this is the method header which is defined as the main method
        */
     public static void main(String[] args)
       {
        Package_Selector_GUI psGUI = new Package_Selector_GUI();
       }
     
     
    }


    --- Update ---

    Hello, I'm new the the forum but joined in hopes of figuring out various problems with my coding. I don't quite understand the error message I'm given (I'm very very new to Java programming) so bear with me, and thank you in advance for any help you are willing to offer.

    MY ERROR MESSAGE
     
      at Package_Selector_GUI.buildPackagePanel(Package_Selector_GUI.java:213)
            at Package_Selector_GUI.<init>(Package_Selector_GUI.java:64)
            at Package_Selector_GUI.main(Package_Selector_GUI.java:251)

    MY FULL CODE
     
    //needed for swing classes for all of the graphical components created in the GUI
     import javax.swing.*; 
     
    //needed for the actionlistener class for events such as clicking a button in the application
     import java.awt.*;
     
    /*
    Needed for for ActionListener Interface for buttons performing a certain action  such as   calculating
    */
     import java.awt.event.*; 
     
    // this for your calculation to come out in a dollar format 
     import java.text.DecimalFormat;
     
        // the class header which marks the start of a class definition
      public class Package_Selector_GUI extends JFrame
        //extends JFrame class to inherit members from the JFrame class
     
    {
     
        private JPanel packagePanel;
        //private JPanel phonePanel;
        //private JPanel optionPanel;
        //private JTextField PhoneTextField;// to hold the Phone input
        private    JTextField PackageTextField; // to hold the Package input
        //private JTextField OptionTextField;  // to hold the Option input
        private JMenuBar menuBar;             // the menu bar
        private JMenu FileMenu;           // the file menu
        private JMenu PhoneMenu;         // the phone menu
        private JMenu PackageMenu;        // the package menu
        private JMenu OptionMenu;        // the option menu
        private JMenuItem exitItem;      // the to exit
        private JRadioButtonMenuItem Package1; //to select 300minutes
            private JRadioButtonMenuItem Package2; // to select 800minutes
        private JRadioButtonMenuItem Package3; // to select 1500minutes
        private JRadioButtonMenuItem Model_100; // to select phone option1
        private JRadioButtonMenuItem Model_110 ; // to select phone option2
        private JRadioButtonMenuItem Model_200;  // to select phone option3
        private JCheckBoxMenuItem    VoiceMail; // to select option1
        private JCheckBoxMenuItem    TextMessage; // to select option2
        private JLabel messageLabel;
     
     
      public Package_Selector_GUI()
       {
        //to display a title
        setTitle("Anthony's GetItGotIt Phone Service");
     
        // specifying an action for the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     
        // creating borderlayout manager
        setLayout(new BorderLayout());
     
        //command to build the button panel
        buildPackagePanel();
     
        add(packagePanel);
     
        //command to build the MenuBar
        buildMenuBar();
     
        //packing all the contents of the window in the display and setting it visible
        pack();
        setVisible(true);
       }
     
        //the build MenuBar method starts building the MenuBar
      private void buildMenuBar()
        {
        // creating the menu bar
        menuBar = new JMenuBar();
     
        //creating the four menu tabs
        buildFileMenu();
        buildPhoneMenu();
        buildPackageMenu();
        buildOptionMenu();
     
        // adding the menu tabs to the menu bar
        menuBar.add(FileMenu);
        menuBar.add(PhoneMenu);
        menuBar.add(PackageMenu);
        menuBar.add(OptionMenu);
     
        // setting the windows menu bar
        setJMenuBar(menuBar);
        }
     
        //the build FileMenu method starts building the FileMenu
      private void buildFileMenu()
        {
        //creating an exit item for the file menu
        exitItem = new JMenuItem("Exit");
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.addActionListener(new ExitListener());
     
        //creating the Jmenu object for the file menu
        FileMenu = new JMenu("File");
        FileMenu.setMnemonic(KeyEvent.VK_F);
     
        //adding the exit item to the the file menu
        FileMenu.add(exitItem);
        }
     
        //the build PhoneMenu method starts building the PhoneMenu
      private void buildPhoneMenu()
        {
        //creating the radio buttons for user to choose what phone they desire
        Model_100 = new JRadioButtonMenuItem ("Model_100",true);
        Model_100.setMnemonic(KeyEvent.VK_P);
     
     
     
        Model_110 = new JRadioButtonMenuItem ("Model_110");
        Model_110.setMnemonic(KeyEvent.VK_H);
     
     
     
        Model_200 = new JRadioButtonMenuItem ("Model_200");
        Model_200.setMnemonic(KeyEvent.VK_O);
     
        //grouping radio buttons together
        ButtonGroup  bg = new ButtonGroup();
        bg.add(Model_100);
        bg.add(Model_110);
        bg.add(Model_200);
     
        //creating the Jmenu object for the Phone menu
        PhoneMenu = new JMenu("Phone");
        PhoneMenu.setMnemonic(KeyEvent.VK_N);
     
        //adding the menu items to the the phone menu
        PhoneMenu.add(Model_100);
        PhoneMenu.add(Model_110);
        PhoneMenu.add(Model_200);
       }
     
     
     
        //the build PackageMenu method starts building the PackageMenu
      private void buildPackageMenu()
        {
     
        //creating the radio buttons for user to choose what Package they desire
        Package1 = new JRadioButtonMenuItem ("300minutes",true);
        Package1.setMnemonic(KeyEvent.VK_A);
     
     
     
        Package2 = new JRadioButtonMenuItem ("800minutes");
        Package2.setMnemonic(KeyEvent.VK_G);
     
     
     
        Package3 = new JRadioButtonMenuItem ("1500minutes");
        Package3.setMnemonic(KeyEvent.VK_E);
     
        //grouping radio buttons together
        ButtonGroup  bg = new ButtonGroup();
        bg.add(Package1);
        bg.add(Package2);
        bg.add(Package3);
     
        //creating the Jmenu object for the Package Menu
        PackageMenu = new JMenu("Package");
        PackageMenu.setMnemonic(KeyEvent.VK_S);
     
     
        //adding the menu items to the the Package Menu
        PackageMenu.add(Package1);
        PackageMenu.add(Package2);
        PackageMenu.add(Package3);
       }
     
        //the build OptionMenu method starts building the OptionMenu
      private void buildOptionMenu()
        {
        //creating the radio buttons for user to choose what Package they desire
        VoiceMail = new JCheckBoxMenuItem ("VoiceMail",true);
        VoiceMail.setMnemonic(KeyEvent.VK_V);
     
     
        TextMessage = new JCheckBoxMenuItem  ("TextMessaging");
        TextMessage.setMnemonic(KeyEvent.VK_T);
     
        //creating the Jmenu object for the Option Menu
        OptionMenu = new JMenu("Options");
        OptionMenu.setMnemonic(KeyEvent.VK_R);
     
        //adding the menu items to the the Option Menu
        OptionMenu.add(VoiceMail);
        OptionMenu.add(TextMessage);
       }
     
     
     
      private void buildPackagePanel()
        {
        double Package1_rate = 45.00;
        double Package2_rate = 65.00;
        double Package3_rate = 99.00;
        String input;
     
        input = PackageTextField.getText();
        messageLabel = new JLabel("Package desired");
        PackageTextField = new JTextField(10); 
     
        if (Package1.isSelected())
           Package1_rate = Double.parseDouble(input);
     
        else if (Package2.isSelected())
                Package2_rate = Double.parseDouble(input);
     
        else if (Package3.isSelected())
            Package3_rate = Double.parseDouble(input);
     
        packagePanel = new JPanel();
        packagePanel.add(messageLabel);
        packagePanel.add(PackageTextField);
     
      }
     
     
        /*
        private inner class that handles the event when the exit button is clicked
        */
     private class ExitListener implements ActionListener
       {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
       }
     
     
        /* 
           this is the method header which is defined as the main method
        */
     public static void main(String[] args)
       {
        Package_Selector_GUI psGUI = new Package_Selector_GUI();
       }
     
     
    }
    [/QUOTE]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need Help with java coding error "Exception in thread "main" java.lang.NullPointerException"

    at Package_Selector_GUI.buildPackagePanel(Package_Sel ector_GUI.java:213)
    at Package_Selector_GUI.<init>(Package_Selector_GUI.j ava:64)
    There is a variable with a null value on line 213. Look at line 213 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 213 and print out the values of all the variables on that line.

    The line number is assuming that all the error message was copied.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 2013, 06:48 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM