0

I have the Java app which takes some rdf dataset as input and works on this data. I used TreeMap in which the key is instance name and the value is TreeSet including the list of properties that instance have and the int which let us .

static TreeMap<String, TreeSet<PropertyIsTyped>> instanceListPropertiesTreeMap = new TreeMap<String, TreeSet<PropertyIsTyped>>();

String instancemapKey = s[0];
TreeSet<PropertyIsTyped> setOfPropertiesPerInstance = new TreeSet<PropertyIsTyped>();
if (instanceListPropertiesTreeMap.get(instancemapKey) == null) {
    setOfPropertiesPerInstance.add(new PropertyIsTyped(s[1], 1));
    instanceListPropertiesTreeMap.put(instancemapKey, setOfPropertiesPerInstance);
} else if (instanceListPropertiesTreeMap.get(instancemapKey) != null
        && !setOfPropertiesPerInstance.contains(s[1])) {
    setOfPropertiesPerInstance = instanceListPropertiesTreeMap.get(instancemapKey);
    PropertyIsTyped currentListproperties = new PropertyIsTyped(s[1], 1);
    setOfPropertiesPerInstance.add(currentListproperties);
}

On the other hand my PropertyIsTyped class is like:

public class PropertyIsTyped {

    public int isTyped;

    public List<String> PropertySet;

    public PropertyIsTyped(String properties, int typevalue) {
        // this.propertyId = id;
        if (properties != "")
            PropertySet.add(properties);
        if (typevalue == 1) {
            this.isTyped = 1;
        } else {
            this.isTyped = 0;
        }
    }

}

Problem: When I give my data set when it wants to insert my new propeties in my List PropertySet it gives my the NullpointerExceptionError

Any Ideas?

Abdullah Khan
  • 9,469
  • 3
  • 52
  • 64
  • A) your naming is terrible ... variables go camelCase in java - and why call something a SET when it is a list ... just use `List properties = new ArrayList<>()` and the NPE will go away. – GhostCat Aug 01 '17 at 11:15

1 Answers1

-2

public List propertySet = null;

if (properties!= "") propertySet.add(properties);` dont correct! List must be initialized!

You need to initialize the list:

public List PropertySet = new ArrayList<>();

....

denAbra
  • 1
  • 1
  • Although you are going in the correct direction - your source code contains various bugs. And the idea to create new properties each time you add something ... bad idea. Long story short: please do **not** try to teach other people whilst you are still a **beginner student**. – GhostCat Aug 01 '17 at 11:14
  • This is not a call to action, it's just an example of how to eliminate the NPE error. A bad idea or a good one depends on the realization of what the programmer wants to do. Here, again, this task was not, oh great and terribly powerful Jedi Master. – denAbra Aug 01 '17 at 11:21
  • This is a Q/A community. It could happen that your answer gets read by thousands and thousands of people over time. Therefore we **care** about the **quality** of answers. Simple - fix the problems or delete the answers. Otherwise we will delete if for you. It is that simple. And again: you managed to get **three** problems into that code. So fix your code, instead of complaining to people who *explain* you what you are doing wrong. And bonus hint: read about **raw** types so that **you** understand a little bit better why your code is so bad. – GhostCat Aug 01 '17 at 11:23