Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems connecting with Dbpedia Lookup web service
    primarykey
    data
    text
    <p>I'm trying to use the Lookup web service from DBpedia, but I'm finding difficulties because there's very little information on the web. I found a code example, but when I execute it, I get errors:</p> <pre><code>java.lang.NullPointerException at tutorial.DBpediaLookupClient.containsSearchTerms(DBpediaLookupClient.java:111) at tutorial.DBpediaLookupClient.endElement(DBpediaLookupClient.java:87) at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source) at javax.xml.parsers.SAXParser.parse(SAXParser.java:195) at tutorial.DBpediaLookupClient.&lt;init&gt;(DBpediaLookupClient.java:56) at tutorial.DBpediaLookupClient.main(DBpediaLookupClient.java:126) </code></pre> <p><strong>HERE IS THE CODE:</strong></p> <pre class="lang-java prettyprint-override"><code>package tutorial; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import javax.xml.parsers.SAXParser; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import com.sun.org.apache.bcel.internal.classfile.Attribute; public class DBpediaLookupClient extends DefaultHandler { /** * @param args */ private List&lt;Map&lt;String, String&gt;&gt; variableBindings = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); private Map&lt;String, String&gt; tempBinding = null; private String lastElementName = null; private String query = ""; public DBpediaLookupClient( String query ) throws Exception { this.query = query; HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=place&amp;QueryString="+query); try{ client.executeMethod(method); System.out.println(method); InputStream ins = method.getResponseBodyAsStream(); SAXParserFactory factory = SAXParserFactory.newInstance(); javax.xml.parsers.SAXParser sax = factory.newSAXParser(); sax.parse(ins, this); }catch (HttpException he) { System.err.println("Http error connecting to lookup.dbpedia.org"); }catch (IOException ioe) { System.err.println("Unable to connect to lookup.dbpedia.org"); }catch (ParserConfigurationException e) { System.out.println("O parser não foi configurado corretamente."); e.printStackTrace(); }catch (SAXException e) { System.out.println("Problema ao fazer o parse do arquivo."); e.printStackTrace(); } method.releaseConnection(); } public void startElement(String uri, String localName, String qName, Attribute attributes) throws SAXException { if (qName.equalsIgnoreCase("result")) { tempBinding = new HashMap&lt;String, String&gt;(); } lastElementName = qName; } public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("result")) { System.out.println("Qname:" + qName); if (!variableBindings.contains(tempBinding) &amp;&amp; containsSearchTerms(tempBinding)) variableBindings.add(tempBinding); } } public void characters(char[] ch, int start, int length) throws SAXException { String s = new String(ch, start, length).trim(); if (s.length() &gt; 0) { if ("Description".equals(lastElementName)) tempBinding.put("Description", s); if ("URI".equals(lastElementName)) tempBinding.put("URI", s); if ("Label".equals(lastElementName)) tempBinding.put("Label", s); } } public List&lt;Map&lt;String, String&gt;&gt; variableBindings() { return variableBindings; } private boolean containsSearchTerms(Map&lt;String, String&gt; bindings) { StringBuilder sb = new StringBuilder(); for (String value : bindings.values()){ sb.append(value); // do not need white space } String text = sb.toString().toLowerCase(); StringTokenizer st = new StringTokenizer(this.query); while (st.hasMoreTokens()) { if (text.indexOf(st.nextToken().toLowerCase()) == -1) { return false; } } return true; } public static void main(String[] args) { try { DBpediaLookupClient client = new DBpediaLookupClient("berlin"); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>If anybody has any idea of what's wrong please help me. Or if any of you has any other code example it will help a lot too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload