Rohit Sharma
Rohit Sharma

Reputation: 121

How should i tackle this sort of error

//Customer.java

import javax.swing.*;
public class Customer
{
//variables for from window

static JFrame frameObj;
static JPanel panelObj;

// variables for labels

JLabel labelCustomerName;
JLabel labelCustomerCellNo;
JLabel labelCustomerPackage;
JLabel labelCustomerAge;

// Variables for data entry controls

JTextField textCustomerName;
JTextField textCustomerCellNo;
JComboBox comboCustomerPackage;
JTextField textCustomerAge;

public static void main(String args[])
    {
        Customer CustObj = new Customer();
    }

public Customer()
    {

            ///Add the appropriate controls to the frame in the construcor
            ///Create Panel
            panelObj= new JPanel();
            frameObj.getContentPane().add(panelObj);

            ///Setting close button
            frameObj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            ///Create and add the appropriate controls

            ///Initializing the labels

            labelCustomerName = new JLabel("Customer Name: ");
            labelCustomerCellNo = new JLabel("Cell Number: ");
            labelCustomerPackage = new JLabel("Package: ");
            labelCustomerAge = new JLabel("Age: "); 

            ///NIintialzing the data entry Controls
            textCustomerName = new JTextField(30);
            textCustomerCellNo = new JTextField(15);
            textCustomerAge = new JTextField(2);    
            String packages[] = { "Executive" , "Standard"};
            comboCustomerPackage = new JComboBox(packages);

            ///Adding Controls to the Customer Name
            panelObj.add(labelCustomerName);
            panelObj.add(textCustomerName);

            ///Adding Controls to the Customer Cell Number
            panelObj.add(labelCustomerCellNo);
            panelObj.add(textCustomerCellNo);

            ///Adding Controls to the Customer Age
            panelObj.add(labelCustomerAge);
            panelObj.add(textCustomerAge);

            ///Adding Controls to the Customer Package
            panelObj.add(labelCustomerPackage);
            panelObj.add(comboCustomerPackage);

    }

}

//when i am executing this program i get an error which says

exception in thread  "main" java.lang.NullPointerException 
at Customer.<init>(Customer.java:35) 
at Customer.<init>(Customer.java:26)

Upvotes: 1

Views: 54

Answers (2)

Erica
Erica

Reputation: 2251

The problem is in this line:

frameObj.getContentPane().add(panelObj);

Take a look at how frameObj is defined:

static JFrame frameObj;

It's never actually getting initialized. It's still null when you try and get its content pane. That's what a NullPointerException means - you're trying to run a method on an object which is null.

Try changing the frameObj call to this:

static JFrame frameObj = new JFrame();

That should fix the issue.

Upvotes: 3

lijie
lijie

Reputation: 4871

frameObj hasn't been initialized/assigned to, so it is NULL. Calling its getContentPane() is going to give you a NullPointerException.

Upvotes: 2

Related Questions