对象崩溃说它是NULL

时间:2017-08-21 08:12:39

标签: c# data-structures

我创建了一个类

public partial class Subrank
{
    private System.DateTime startDateField;

    public System.DateTime StartDate
    {
        get
        {
            return this.startDateField;
        }
        set
        {
            this.startDateField = value;
        }
    }
}
然后我试着创建一个这个数组...

Subrank[] pastSubRank = new Subrank[1];
pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");

但是pastSubRank[0]崩溃说它是NULL ....为什么会这样?

3 个答案:

答案 0 :(得分:6)

是的,因为在访问它的属性之前,您尚未实例化@Override protected String doInBackground(String... params) { if ((proname.trim().equals("")) || prodesc.trim().equals("")) //if (proname.length() == 0|| prodesc.trim().equals("")) //(proname == null) z = "Silahkan Scan Barcode"; else { try { Connection con = connectionClass.CONN(); if (con == null) { z = "Error in connection with SQL server"; } else { String query = "insert into Producttbl (ProName,ProDesc,OnDate,Qty,Users,Locations) values ('" + proname + "','" + prodesc + "','" + getDateTime() + "','" + qty + "','" + userid + "','" + location2 + "')"; PreparedStatement preparedStatement = con.prepareStatement(query); preparedStatement.executeUpdate(); z = "Added Successfully"; isSuccess = true; } } catch (final SQLException e) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { Toast.makeText(AddProducts.this, e.getMessage().toString(), Toast.LENGTH_LONG).show(); } }); } } return z; } 类。

Subrank

答案 1 :(得分:4)

您需要在访问属性

之前创建一个放入数组的对象
        Subrank[] pastSubRank = new Subrank[1];
        pastSubRank[0] = new Subrank();
        pastSubRank[0].StartDate = DateTime.Parse("2012-05-22");

答案 2 :(得分:2)

private Guichet guichet;