0

I have an exception with the following code

  public calculWeightdoc(OWLOntology onto, String xml) {

        for(OWLClass cls: onto.getClassesInSignature()){

        freqConcept(xml, cls);
        System.out.println("la taille de liste : "+list.getLength());

            if(list.getLength()!=0){   
                listConceptRetenus.put(cls, list.getLength());
            }

            else 
                {
                    listConceptRetenus.put(cls, 0);
                }


        }

   }

This is the function FreqConcept

    public void freqConcept(String xmldoc,OWLClass node){
        try {
            String filepath = xmldoc;
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);


             list = doc.getElementsByTagName(node.getIRI().getFragment());


        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException sae) {
            sae.printStackTrace();
        }
    }

And this is the exception:

Exception in thread "main" java.lang.NullPointerException
at com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.nextMatchingElementAfter(DeepNodeListImpl.java:199)
at com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.item(DeepNodeListImpl.java:146)
at com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.getLength(DeepNodeListImpl.java:117)
at com.onto.weight.document.calculWeightdoc.<init>(calculWeightdoc.java:59)
at com.onto.weight.document.Main_Class_une_seule_onto.main(Main_Class_une_seule_onto.java:70)

Actually, the same code works perfectly for some ontologies like people with 60 classes, but for the others with an important number of classes like Dbpedia with 1173 classes it doesn't work, I'm not sure if that is the problem or something else...
The exception is about that line list.getLength() in that function calculWeightdoc(OWLOntology onto, String xml) .

Thank you for sharing any idea that may hepl me ti fix the problem.

Nina
  • 105
  • 1
  • 13
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS Jul 30 '18 at 11:47
  • Please add a [mcve]. For example, the shortest XML string that still allows to reproduce the problem. – M. Prokhorov Jul 30 '18 at 11:47
  • @OHGODSPIDERS, I'm not sure if it's a usual case of NPE question - after all, the NPE happens inside the DOM library, seemingly without user's accord. – M. Prokhorov Jul 30 '18 at 11:48
  • Surely, it's not a usual case of NPE since the code works perfectly for some ontologies. to answer to your question @M.Prokhorov I use the same XML file for the test and multiple ontologies, So, it's not the problem ... – Nina Jul 30 '18 at 12:05
  • since you did not mention **which ontology** fails, impossible to help you. are what do you expect now, given that inside a 3rd party library an error occurs? – UninformedUser Jul 31 '18 at 05:17
  • It's also not clear **which part of DBpedia** you're loading and **which OWL API version** you use. Information matters for debugging, you should already know this... – UninformedUser Jul 31 '18 at 05:19
  • To answer the comments about whether this is an usual null pointer or not - all null pointer exceptions are usual. A null value is dereferenced. In this case, there are stack traces that are being printed but not shown here. – Ignazio Aug 02 '18 at 21:11

1 Answers1

0

The list initialization happens only if the input does not fail parsing. Is there any stacktrace appearing when the list is initialised? If so, the list is left as null. To avoid the problem, check if the list is null before accessing it.

Ignazio
  • 10,504
  • 1
  • 14
  • 25
  • you're right, the problem appears when trying the initialization of the list. – Nina Jul 31 '18 at 11:50
  • I explained the problem as another subject here https://stackoverflow.com/questions/51612722/comparing-an-classes-of-ontologies-to-an-xml-document-nodes – Nina Jul 31 '18 at 11:54