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:
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.
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.
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.
OpstiDomenskiObjekat is interface, so I could have one or few methods for many similar things I need.
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
public string uslovJedan
{
get
{
return Korisnik.Ime;
}
}
//this should happen after method, I will have List as I needed
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 :)