【问题标题】:How should i tackle this sort of error我应该如何解决这种错误
【发布时间】:2010-11-25 06:31:05
【问题描述】:

//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);

    }

}

//当我执行这个程序时,我得到一个错误,上面写着

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

【问题讨论】:

    标签: java exception exception-handling error-handling


    【解决方案1】:

    问题出在这一行:

    frameObj.getContentPane().add(panelObj);
    

    看看frameObj是如何定义的:

    static JFrame frameObj;
    

    它实际上从未被初始化。当您尝试获取其内容窗格时,它仍然为空。这就是 NullPointerException 的含义 - 您正在尝试在一个为 null 的对象上运行一个方法。

    尝试将 frameObj 调用更改为:

    static JFrame frameObj = new JFrame();
    

    这应该可以解决问题。

    【讨论】:

      【解决方案2】:

      frameObj 还没有被初始化/赋值,所以它是NULL。调用它的getContentPane() 会给你一个NullPointerException

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-21
        • 2016-09-09
        • 1970-01-01
        • 2015-02-28
        • 2013-08-16
        相关资源
        最近更新 更多