-1

What is wrong with the statement if (!adjacentNodes.get(nodeId).isEmpty()) in the below code?

Such statement leads to the following message:

java.lang.NullPointerException

package IN.company;

import java.util.HashMap;
import java.util.HashSet;

public class DiffusionModels {
    public int ICModel(int[] seedSet, HashMap<Integer, HashSet<Integer>> adjacentNodes, double p) {
        HashSet<Integer> infected = new HashSet();
        HashSet<Integer> recentlyInfected = new HashSet();
...

        while (recentlyInfected.size() > 0) {
            HashSet<Integer> nextActivator = new HashSet<>();
            if (!recentlyInfected.isEmpty()) {
                for (Integer nodeId : recentlyInfected) {
                        if (!adjacentNodes.get(nodeId).isEmpty()) {
                            HashSet<Integer> neighborsOfNode =adjacentNodes.get(nodeId) ;
                            for (Integer nodeNeighbor : neighborsOfNode) {
                                if ((Math.random() <= p)&& !(infected.contains(nodeNeighbor))) {
                                    nextActivator.add(nodeNeighbor);
                                }
                            }
                        }
                }
            }
            infected.addAll(recentlyInfected);
            recentlyInfected.clear();
            recentlyInfected.addAll(nextActivator);
        }
        return infected.size();
    }
}
Zumo de Vidrio
  • 2,021
  • 2
  • 15
  • 33
JavaDev
  • 3
  • 3
  • If the expression you designate generates a `NullPointerException` then it is surely because `adjacentNodes.get(nodeId)` returns `null`. Note in particular that `null` is an altogether different beast from a non-null reference to an empty `Map`. – John Bollinger Mar 16 '17 at 13:23
  • Thanks, but how can I fix it? I check this issue with isEmpty function, is it wrong? – JavaDev Mar 16 '17 at 13:26
  • @Timothy Truckle, thank you, but that topic would not be useful for me. – JavaDev Mar 16 '17 at 13:28

3 Answers3

1

This beacause adjacentNodes is null,which result to NPE.Before that line try to print the values of adjacentNodes.Hope this helps.

Porkko M
  • 307
  • 2
  • 10
1

Try this if (adjacentNodes.get(nodeId) != null && !adjacentNodes.get(nodeId).isEmpty()) {

jr593
  • 287
  • 1
  • 8
0

I would be grateful if anyone help me and explain what is the problem of this statement:

if (!adjacentNodes.get(nodeId).isEmpty()) 

This statement leads to "java.lang.NullPointerException"

According to the code you posted either adjacentNodes is null or it does not contain the key currently referenced by nodeId.

You have to check that adjacentNodes is not null before that line.

If adjacentNodes is not null you should use the getOrDefault() method of Java8's Map class (https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#getOrDefault-java.lang.Object-V-)

if(!adjacentNodes.getOrDefault(nodeId, Collections.emptySet()).isEmpty())){ //...
Community
  • 1
  • 1
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51