title: Querying Solr server using SolrJ
body: <p>I'm trying to query my solr database based off the code provided in <a href="https://stackoverflow.com/questions/11143188/solrj-select-all">this question</a> with SolrJ but it keeps throwing a null pointer exception. </p>
  
  <p>My code is:</p>
  
  <pre><code>@PUT
  @Produces(MediaType.TEXT_PLAIN)
  public String returnText(String url) throws MalformedURLException, SolrServerException{
      SolrServer server = new HttpSolrServer("http://localhost:8080/apache-solr-1.4.0");
  
      ModifiableSolrParams params = new ModifiableSolrParams();
      params.set("?q", "*:*");
      params.set("facet", true);
      params.set("rows", 5);
  
      QueryResponse response = server.query(params);
      System.out.println(response);
      return "success";
  }
  </code></pre>
  
  <p>and if I run this url: <code>http://localhost:8080/apache-solr-1.4.0/select/?q=*:*&amp;facet=true&amp;rows=5</code> in my browser then it returns the correct values however if I enter: <code>http://localhost:8080/apache-solr-1.4.0/select/q=*:*&amp;facet=true&amp;rows=5</code> then I get the exact same error as when I run it in eclipse, the error is: </p>
  
  <pre><code>19/12/2012 1:09:01 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
  SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
  org.apache.solr.client.solrj.SolrServerException: Server at http://localhost:8080/apache-solr-1.4.0 returned non ok status:500, message:null  java.lang.NullPointerException    at java.io.StringReader.&lt;init&gt;(StringReader.java:33)    at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:197)    at org.apache.solr.search.LuceneQParser.parse(LuceneQParserPlugin.java:78)  at org.apache.solr.search.QParser.getQuery(QParser.java:131)    at org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:89)     at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:174)    at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:131)    at org.apache.solr.core.SolrCore.execute(SolrCore.java:1316)    at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:338)  at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:241)     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)   at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)   at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)   at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)   at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)   at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)  at java.lang.Thread.run(Thread.java:619)
      at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:328)
      at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:211)
      at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:89)
      at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
      at geoportal.webservice.download.returnText(download.java:48)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      .......
  </code></pre>
  
  <p>If I change my code to <code>params.set("q","*:*");</code> then I get this error:</p>
  
  <pre><code>19/12/2012 1:13:30 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
  SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
  org.apache.solr.client.solrj.SolrServerException: Error executing query
      at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:95)
      at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
      at geoportal.webservice.download.returnText(download.java:48)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        .......
  </code></pre>
  
  <p>Any ideas as to what I've done wrong?</p>
  
  <p>Thanks heaps</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is my new code for the solrQuery based off <a href="http://wiki.constellio.com/index.php/Solrj_example" rel="nofollow noreferrer">this</a>: </p>
  
  <pre><code>@PUT
  @Produces(MediaType.TEXT_PLAIN)
  public String returnText(String url) throws MalformedURLException, SolrServerException{
      SolrServer server = new HttpSolrServer("http://localhost:8080/apache-solr-1.4.0");
      SolrQuery query = new SolrQuery();
  
      query.setQuery("*:*");
      query.setFacet(true);
      query.set("wt", "json");
      query.setRows(5);
      query.setStart(0);
  
      QueryResponse response = server.query(query);
  
      System.out.println(response);
      return "success";
  }
  </code></pre>
  
  <p>But it still isn't working :(
  Console output:</p>
  
  <pre><code>19/12/2012 2:24:33 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
  SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
  org.apache.solr.client.solrj.SolrServerException: Error executing query
      at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:95)
      at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:311)
      at geoportal.webservice.download.returnText(download.java:39)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            ......
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I put a <code>System.out.println(query.toString());</code> in my code and it outputted:</p>
  
  <pre><code>q=*%3A*&amp;facet=true&amp;wt=json&amp;rows=5&amp;start=0
  </code></pre>
  
  <p>If I enter this into my browser like </p>
  
  <pre><code>http://localhost:8080/apache-solr-1.4.0/select/q=*%3A*&amp;facet=true&amp;wt=json&amp;rows=5&amp;start=0
  </code></pre>
  
  <p>then it throws a null pointer exception again. However if I manually enter a <code>?</code> in front of <code>q=*%3A*...</code> then it works. So I assume that my solr server needs this <code>?</code> (is this because it's so old?), is there any way to hard code one in?</p>
  
  <p>Thanks</p>