[英]Object not set to an instance of an object in sql query, null reference exception
In this specific case I am getting null reference exception, and I cant solve it. 在这种情况下,我得到了空引用异常,但我无法解决。
public class Tiket
{
private Korisnik korisnik;
public Korisnik Korisnik
{
get { if (korisnik == null)
{
korisnik = new Korisnik();
}
return korisnik; }
set { korisnik = value; }
}
}
Inside form I am taking value inserted in textbox and sending it to method which deal with database(its much more complex but I am trying to make it simple): 在表单内部,我采用插入文本框中的值并将其发送给处理数据库的方法(它要复杂得多,但我试图使其变得简单):
Tiket t = new Tiket();
t.Korisnik.Ime = txtImeKorisnika.Text;
thatMethod(t);
So above, I am not getting null reference exception while assigning value to t.Korisnik.Ime, but Inside method I am getting one: 因此,在上面给t.Korisnik.Ime赋值时,没有得到null引用异常,但是在Inside方法中却得到了一个:
string upit = "SELECT * FROM " + Tiket + " " + " WHERE " + t.Korisnik.Ime;
extra question connected to this topic: I am having same problem while trying to add values to these fields from database which are also class properties inside Tiket. 与此主题相关的一个额外问题:尝试向数据库中的这些字段(也是Tiket内的类属性)添加值时遇到相同的问题。
tikeet.Korisnik.JMBG = red[5].ToString();
tikeet.Radnik.SifraRadnika = Convert.ToInt32(red[6]);
I know why is this happening but I dont know how to initialize Korisnik, Radnik here. 我知道为什么会这样,但是我不知道如何在这里初始化Korisnik和Radnik。
EDIT: I hope this helps now, so You can help me ! 编辑:我希望这对您有所帮助,所以您可以帮助我! I have client server app and I am sending Tiket to Server..than its sent as OpstiDomenskiObjekat(instead of Tiket) to method I have problem now.
我有客户端服务器应用程序,我正在将Tiket发送到Server ..而不是作为OpstiDomenskiObjekat(而不是Tiket)发送给我现在有问题的方法。
OpstiDomenskiObjekat is interface, so I could have one or few methods for many similar things I need. OpstiDomenskiObjekat是接口,因此我可以使用一种或几种方法来处理许多我需要的类似东西。
public List<OpstiDomenskiObjekat> vratiZaUslovJedanPlus(OpstiDomenskiObjekat odo)
{
string upit = "SELECT * FROM " + odo.tabela + " " + " WHERE " + odo.uslovJedan;
OleDbDataReader citac = null;
OleDbCommand komanda = new OleDbCommand(upit, konekcija, transakcija);
try
{
citac = komanda.ExecuteReader(); // **here is exception thrown**
DataTable tabela = new DataTable();
tabela.Load(citac);
List<OpstiDomenskiObjekat> lista = new List<OpstiDomenskiObjekat>();
foreach (DataRow red in tabela.Rows)
{
OpstiDomenskiObjekat pom = odo.napuni(red);
lista.Add(pom);
}
return lista;
}
catch (Exception ex)
{
throw ex;
}
//this is implementation in class Tiket //这是Tiket类中的实现
public string uslovJedan
{
get
{
return Korisnik.Ime;
}
}
//this should happen after method, I will have List as I needed //这应该在方法之后发生,我将根据需要使用List
List<Tiket> lista = Broker.dajSesiju().vratiZaUslovJedanPlus(odo).OfType<Tiket>().ToList<Tiket>();
return lista;
Connection, transaction etc.. its all somewhere else, but It all works, I promise :) 连接,交易等。所有其他地方都可以,但是我保证:)
The + operator in this case is a string concatenation operator. 在这种情况下, +运算符是字符串连接运算符。 Ticket is the name of your class and its not clear whether you have a property with that same name.
票证是您班级的名称,尚不清楚您是否拥有同名的物业。 If you do have a property by that name,
如果您确实拥有该名称的属性,
string upit = "SELECT * FROM " + Tiket + " " + " WHERE " + t.Korisnik.Ime;
it is possible that the above line is failing because the Tiket
property is null. 上一行可能由于
Tiket
属性为null而失败。 And also, it's ToString()
might not be valid for the FROM clause of a query. 而且,它的
ToString()
可能对查询的FROM子句无效。
Update 更新
As you can see, it didn't compile for me at all. 如您所见,它根本不适合我编译。 When I wrote a property with the same name.
当我写了一个同名的属性时。 It compiled, and constructed the string, inserting a null character where Ticket was.
它编译并构造了字符串,在Ticket所在的位置插入了一个空字符。
The second screenshot shows what happens if it was a property. 第二张屏幕截图显示了如果它是一个属性会发生什么。
Your string is still constructed in that case and you will get an invalid query at the line string upit = "SELECT * FROM " + Tiket + " " + " WHERE " + t.Korisnik.Ime;
在这种情况下,您的字符串仍会被构造,并且在
string upit = "SELECT * FROM " + Tiket + " " + " WHERE " + t.Korisnik.Ime;
处将得到无效的查询string upit = "SELECT * FROM " + Tiket + " " + " WHERE " + t.Korisnik.Ime;
which is probably causing your issues. 这可能是造成您的问题的原因。 Please update with a screenshot of where the exception is being thrown.
请使用抛出异常的屏幕截图进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.