-2

I have the following situation, where my program terminates with a NullPointerException during the function. What can I do to make this function work?

UPDATE

else if (str == null)
    emptyLinks.add(newPageLinks.get(i).getText() + "at link: " + str);
Source Code
  • 288
  • 1
  • 2
  • 16

2 Answers2

1

The problem is not in:

else if (str == null) 

The actual problem is in:

do something

In your do something you are trying to access a property or method of a null object which isn't possible and will throw an NPE.

Normally if you want to access methods from an object that could be null you do a null check like so:

if (str != null)
do something
brso05
  • 13,142
  • 2
  • 21
  • 40
1

If you use str == null the NullPointerException shouldn't be thrown. You can use:

 if (str == null || str.equals("null"))
    do something

If first condition is true then second conditions won't be evaluated and the exception won't be thrown.

Probably your problem is in do something when the str is null.

maiklahoz
  • 135
  • 8