使用GET时在netbeans中引发异常

时间:2014-02-24 03:30:04

标签: java swing exception netbeans

我正在尝试通过GET从我的Model类中检索数据到textfield,尽管nullpointexception正在抛出错误

View类中的代码是=

  public View_EditCustomer(Model_Customer cust) {
        customer = cust;
        txtname.setText(customer.GetFName());
        txtSecondName.setText(customer.GetLName());

        initComponents();
    }

在另一个View类中,它是=

private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {                                          
      ListSelectionModel rowSM = jTable1.getSelectionModel();
            int row = rowSM.getMinSelectionIndex();
            int Appointment_ID = (Integer)resultModel.getValueAt(row, 0);
            Model_Customer cust = null;
            try{
                cust = Controller_ManageCustomer.GetCustomer(Appointment_ID);
                new View_EditCustomer(cust).setVisible(true); 
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
            }
    }          

Model_Customer代码部分=

  public static Model_Customer QueryID(int Appointment_ID) throws Exception
    {
        try{
            Statement stmt = Model_Customer.conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM appointment WHERE appointmentid="+Appointment_ID+" LIMIT 1;");
            if(rs.next())
                return new  Model_Customer(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12));
        }catch(Exception e){
            throw new Exception(e.getMessage());
        }
        return null;
}


private Model_Customer(int Appointment_ID, String FName, String LName, String Registration, String Make, String Model, String Engine, String Year, String Mileage, String Type, String Date, String Time)
    {
        this._Appointment_ID=Appointment_ID;
        this._Type=Type;
        this._Time=Time;
        this._Date=Date;
        this._FName=FName;
        this._LName=LName;
        this._Make=Make;
        this._Model=Model;
        this._Engine=Engine;
        this._Year=Year;
        this._Mileage=Mileage;
        this._Registration=Registration;
        this._inSync=true;    
    }
    public int GetID()
    {
        return this._Appointment_ID;
    }
    public String GetFName()
    {
        return _FName;
    }
    public String GetLName()
    {
        return _LName;
    }
    public String GetRegistration()
    {
        return _Registration;
    }

    public String GetMake()
    {
        return _Make;

    }
    public String GetModel()
    {
        return _Model;
    }


    public String GetEngine()
    {
        return _Engine;
    }
    public String GetYear()
    {
        return _Year;
    }
    public String GetMileage()
    {
        return _Mileage;
    }
    public String GetType()
    {
        return _Type;
    }
    public String GetDate()
    {
        return _Date;
    }
    public String GetTime()
    {
        return _Time;
    }

在调试中,Model_Customer cust实际上是由数据填充的,它实际上是到了txtname.setText(customer.GetFName());转到Model_Customer GetFName并应检索名称但抛出异常(int)0。真的很感谢你的帮助!!

1 个答案:

答案 0 :(得分:1)

不应该是initComponents();在之前使用TextViews调用

public View_EditCustomer(Model_Customer cust) {

        initComponents();
        customer = cust;
        txtname.setText(customer.GetFName());
        txtSecondName.setText(customer.GetLName());


    }