java - 线程 "main"java.lang.NullPointerException 中的异常 - HBase 索引数据

标签 java

我正在解析 pdf 并将标题、作者等存储在变量中,我需要在 hbase 中索引这些值。所以我从我在项目中创建的变量中获取 hbase 表的数据。当我在 hbase 表中使用变量进行索引时,程序显示 NullPointerException 错误。

Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.<init>(String.java:154)
    at testSolr.Testt.Parsing(Testt.java:50)
    at testSolr.Testt.main(Testt.java:94)

我尝试了两种不同的类型,但都没有用。

String title = new String(metadata.get("title"));  

String title = metadata.get("title");

这是我的部分代码(我写了重要的部分。):

          Random rand = new Random();
          int min=1, max=5000;
          int randomNumber = rand.nextInt((max - min) + 1) + min;

           //parsing part
           String title = new String(metadata.get("title"));
           String nPage = new String(metadata.get("xmpTPg:NPage"));
           String author = new String(metadata.get("Author"));
           String content = new String(handler.toString());

          //hbase part(the part where I am getting the error.)
          Put p = new Put(Bytes.toBytes(randomNumber));
          p.add(Bytes.toBytes("book"),
                  Bytes.toBytes("title"),Bytes.toBytes(title));
          p.add(Bytes.toBytes("book"),
                  Bytes.toBytes("author"),Bytes.toBytes(author));
          p.add(Bytes.toBytes("book"),
                  Bytes.toBytes("pageNumber"),Bytes.toBytes(nPage));
          p.add(Bytes.toBytes("book"),
                  Bytes.toBytes("content"),Bytes.toBytes(content));
          hTable.put(p);

我应该在解析开始时让变量为 null 吗?我认为这没有任何意义。我应该怎么做才能修复错误?

更新: 完整代码

public static String location = "/home/alican/Downloads/solr-4.10.2/example/solr/senior/PDFs/solr-word.pdf";

public static void Parsing(String location) throws IOException, SAXException, TikaException, SolrServerException {
  // random number generator for ids
  Random rand = new Random();
  int min=1, max=5000;
  int randomNumber = rand.nextInt((max - min) + 1) + min;
  // random number generator for ids ends
  // pdf Parser
  BodyContentHandler handler = new BodyContentHandler(-1);
  FileInputStream inputstream = new FileInputStream(location);
  Metadata metadata = new Metadata();
  ParseContext pcontext = new ParseContext();
  PDFParser pdfparser = new PDFParser();
  pdfparser.parse(inputstream, handler, metadata, pcontext);      
   String title = new String(metadata.get("title"));
   String nPage = metadata.get("xmpTPg:NPage");
   String author = new String(metadata.get("Author"));
   String content = new String(handler.toString());

  System.out.println("Title: " + metadata.get("title"));
  System.out.println("Number of Page(s): " + metadata.get("xmpTPg:NPages"));
  System.out.println("Author(s): " + metadata.get("Author"));

  System.out.println("Content of the PDF :" + handler.toString());
  // pdf Parser ends
  // solr Indexing
  SolrClient server = new HttpSolrClient(url); 
  SolrInputDocument doc = new SolrInputDocument();


  doc.addField("id", randomNumber);
  doc.addField("author", author);
  doc.addField("title", title);
  doc.addField("pageNumber", nPage);
  doc.addField("content", content);

  server.add(doc);
  System.out.println("solr commiiitt......");
  server.commit();
  // solr Indexing ends
  // hbase Indexing
  Configuration config = HBaseConfiguration.create();
  HTable hTable = new HTable(config, "books");
  Put p = new Put(Bytes.toBytes(randomNumber));
  p.add(Bytes.toBytes("book"),
          Bytes.toBytes("title"),Bytes.toBytes(title));
  p.add(Bytes.toBytes("book"),
          Bytes.toBytes("author"),Bytes.toBytes(author));
  p.add(Bytes.toBytes("book"),
          Bytes.toBytes("pageNumber"),Bytes.toBytes(nPage));
  p.add(Bytes.toBytes("book"),
          Bytes.toBytes("content"),Bytes.toBytes(content));
  hTable.put(p);
  System.out.println("hbase commiiitttt..");
  hTable.close();
  // hbase Indexing ends

标题、作者、页数和内容的输出:

Title: solr-word
Number of Page(s): 1
Author(s): Grant Ingersoll
Content of the PDF :
This is a test of PDF and Word extraction in Solr, it is only a test.  Do not panic. 

HBase 部分假定 nPage 的变量为空。其实不然。 nPage 的值为 1。

p.add(Bytes.toBytes("book"),
              Bytes.toBytes("pageNumber"),Bytes.toBytes(nPage));

解决方案:

metadata.get("xmpTPg:NPage") 由于某种原因分配给变量时返回 null。我意识到这是因为解析器。我更改了解析器,不再有任何空变量。 - Apache PDFBox(我的新解析器)比 Apache Tika(我的旧解析器)更好。

最佳答案

您的 metadata.get("title") 返回 null,因此抛出 NullPointerException。参见 Javadoc了解更多详情。

关于java - 线程 "main"java.lang.NullPointerException 中的异常 - HBase 索引数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35856830/

相关文章:

java - 启动画面中的 GIF 不流畅

java - 将 java 对象保存到 PostgreSQL 问题

java - 使用 GSON 生成链接而不是整个对象树的序列化

java - 在 ConstraintValidator 中注入(inject) EntityManager

Java方法帮助

java - Weblogic:调用没有架构名称的 DB2 存储过程(属性 currentSchema)

java - ActionPerformed 返回变量

java - 错误 : syntax error at or near "$1" postgresql query with params

java - 我在一个按钮上有 LinearLayout。有什么办法可以处理下方的按钮点击事件吗?

java - 嵌套 if(e.getActionCommand().equals ("")