0

I have a class like this one:

class Tickets{
    Data data;
    class Data{
        String departure;
    }
    public String getDeparture(){
        return data.departure;
    }

}

When I try something like this:

Tickets tickets=new Tickets();
if (tickets.getDeparture!=null)

I get nullpointerexception on this check. Why is that so? How can I avoid it? What to do instead? Thanks for help.

Zvone Peran
  • 53
  • 1
  • 11

1 Answers1

0

Before you can use object instance you have to create (initialize) it.

class Tickets{
    Data data = new Data(); // initialize data instance
    class Data{
        String departure;
    }
    public String getDeparture(){
        return data.departure
    }
}

Tickets tickets = new Tickets();
if (tickets.getDeparture!=null)

Just because you are using nested class in above case does not mean that you don't have to initialize data instance.

Since you are using above class for parsing JSON data, you can end up with data instance being null in case of invalid or missing JSON data even though it has been originally initialized with tickets instance.

To avoid that NPE you would have to add additional check if data is null in getDeparture method.

    public String getDeparture()
    {
        return data != null ? data.departure : null;
    }
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
  • Yeah, I know that. That was accidental error while posting code here . I intended to initialize it. But I still get nullpointerexception. – Zvone Peran Apr 22 '16 at 09:56
  • 1
    You need to initialize data instance also. See `Data data = new Data();` – Dalija Prasnikar Apr 22 '16 at 09:56
  • i think the code is still wrong initialize departure variable too; – Priyamal Apr 22 '16 at 10:08
  • Is there any way it could be done automatically? For example could I initialize data instance in Tickets constructor? – Zvone Peran Apr 22 '16 at 10:09
  • @ Priyamal I am using null check for that reason. If I knew it had been initialized to some value, there would no be need for null check. – Zvone Peran Apr 22 '16 at 10:11
  • Have you tried code I posted? It initializes data instance automatically when you create ticket instance. you can also use constructor if you wish, but it is not necessary in above example – Dalija Prasnikar Apr 22 '16 at 10:11
  • The only thing that you need to additionally initialize is departure string instance, but your code that checks it for null is there for that purpose I presume. – Dalija Prasnikar Apr 22 '16 at 10:14
  • Didn't see that line, sorry. That would probably work. I am using this nested class to parse JSON into these variables. Not sure if that would be problem if I initialize data instance that way. – Zvone Peran Apr 22 '16 at 10:14
  • OK, parsing is bit different. It depends on the parser, but you can easily end up with `data` being `null` even when you initialize it before parsing. If that is the case (when you have invalid or missing data) you will also have to add `null` check for `data` in `getDeparture` method. – Dalija Prasnikar Apr 22 '16 at 10:54
  • Thanks, I think that your last code snippet is answer. I 'll check it. – Zvone Peran Apr 22 '16 at 11:46