0

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

mike_cus
  • 67
  • 1
  • 10
  • 1
    You are opening yourself up to SQL injection with the way your query is constructed. – Fabulous Jun 22 '17 at 22:45
  • 1
    `t.Korisnik = new Korisnik { Ime = txtImeKorisnika.Text };` perhaps? but: your current approach is a horrible SQL injection vector – Marc Gravell Jun 22 '17 at 22:48
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sir Rufo Jun 22 '17 at 22:53
  • @MarcGravell is it just another way to do what I did already by adding part if(korisnik==null) etc in property Korisnik inside Tiket. because I am not getting exception while taking value from textbox. but later, inside query..but for the same thing.maybe you actually gave me solution but I dont understand ? :) – mike_cus Jun 22 '17 at 22:58
  • Just put a breakpoint & debug, see which line you are getting error & what you are getting in 'upit' and try to execute that in SSMS.. – Arun Vinoth-Precog Tech - MVP Jun 22 '17 at 23:01
  • After looking at your comment, I'd still want to see what the query is like @ArunVinoth said and I alluded to in my answer. – Fabulous Jun 22 '17 at 23:30
  • @mike_cus ah, yes, I didn't spot that in the getter; in that case, you'll need to run it in the debugger and see what breaks – Marc Gravell Jun 22 '17 at 23:37
  • @MarcGravell, Fabulous, Arun Vinoth - please guys if You can manage few minutes to check again.. I edited post. I dont know any better, and I need help :) I went through debugging many times. – mike_cus Jun 22 '17 at 23:51

1 Answers1

0

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 a class name only the code didn't compile for me

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.

The string is still constructed

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.

Fabulous
  • 2,393
  • 2
  • 20
  • 27
  • No It will work if there is nothing after Tiket in query. There is only class name Tiket. Thanks for effort. – mike_cus Jun 22 '17 at 22:52
  • Maybe attach a screenshot of where the exception is happening – Fabulous Jun 22 '17 at 22:53
  • Thank you very much for your huge effort really, I appreciate it. But please read above comment which I wrote to Marc Gravell. Tiket is class, and table in database and it is not empty. It will work if you delete part after name of the table. t.Korisnik.Ime inside query is a problem, because Korisnik is not initialized. again thanks man – mike_cus Jun 22 '17 at 23:24