Simon Tenbeitel
Simon Tenbeitel

Reputation: 877

Java Lang Null Pointer Exception, but I don't understand why?

I get a Java Lang Null Pointer Exception at this code:

for (int i = 0; i<fragen.size(); i++)
    {
        for (int x = 0; x<dbfragen.size(); x++)
        {
            if(i == dbfragen.get(x).getFrageNR())
            {
                if(fragen.get(i).getTyp() == 1)
                {

It happens in the last if statement. I used the Log file: fragen.size() is 30 and it crashes at i = 1

this makes no sense to me :O

Please help ;)

Upvotes: 0

Views: 114

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533492

A collection can have 30 nulls in it. I suspect the first element is not null so it it fine but the second element index:1 is null.

BTW I suggest caching the lookup.

for (int i = 0; i<fragen.size(); i++) {
    MyType fragenI = fragen.get(i);
    if (fragenI == null) {
       // do something
       continue;
    }

Upvotes: 1

Related Questions