简体   繁体   中英

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:

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 :)

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. And also, it's ToString() might not be valid for the FROM clause of a query.

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.

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; which is probably causing your issues. Please update with a screenshot of where the exception is being thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM