0

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

6
  • It should tell you the exact line. dbfragen is null or the object returned by dbfragen.get(x) or fragen.get(i) should be null. Commented Nov 9, 2013 at 11:14
  • try to print fragen.get(i) , then fragen.get(i).getTyp() before your if condition , see which one throws exception Commented Nov 9, 2013 at 11:15
  • It tells me it's in line 49 (this is fragen.get(i).getTyp() ==1) But it works the first time, when i is 0, but not the second time, when it is 1 Commented Nov 9, 2013 at 11:16
  • Why don't you just show your full code? Commented Nov 9, 2013 at 11:16
  • The full code has more than 1000 lines Commented Nov 9, 2013 at 11:29

1 Answer 1

1

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;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. You are right, there are 29 nulls in it. Now i have to find out why...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.