我应该如何解决我的NullPointerException?

时间:2013-10-29 16:16:36

标签: java lucene nullpointerexception indexing neo4j

我真的不知道还能做什么,我已经搜索了所有地方,尝试过但无法解决我的问题。我创建了一个neo4j图形数据库,并通过localhost:7474(直观地)创建了节点名称属性的索引,现在我想通过搜索他们的名称属性索引得到节点号,但我得到nullpointerexception错误,我知道这是因为现有的空值,我知道这是从哪里来的,但我无法解决它。

这是代码:

package graph_traversing;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.IndexHits;
import org.neo4j.graphdb.index.IndexManager;

public class search_string extends javax.swing.JFrame
{


    public search_string() {initComponents(); }
    @SuppressWarnings("unchecked")

private static enum RelTypes implements RelationshipType
{
    INCLUDES
} 

  private static final String DB_PATH = "C:\\Users\\fereshteh\\Downloads\\Compressed\\neo4j-community-1.9.4\\DB";
  GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
 IndexManager nodeindex = db.index();
 Index<Node> names = nodeindex.forNodes( "name" );

    String [] str;
    Node [] nodes;Relationship rel;
    IndexHits<Node> hits;

    String find;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        search_txb = new javax.swing.JTextField();
        search_btn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText(" Enter The Word Please");

        search_btn.setText("Search");
        search_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search_btnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(search_btn)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(search_txb, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(search_txb, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addComponent(search_btn)
                .addGap(50, 50, 50))
        );

        pack();
    }// </editor-fold>                        

    private void search_btnActionPerformed(java.awt.event.ActionEvent evt) {                                           

        str=search_txb.getText().split(" ");

       for(int i=0;i<str.length;++i)
        {
            find=str[i];
            hits = names.get("name", find);
            System.out.println(hits);
            nodes [i] = hits.getSingle();
          // nodes [i]= nodeIndex.get( "name", find ).getSingle();
           System.out.println(nodes[i]);
        }
       String s ="hi";
       for(int i=0;i<nodes.length;++i)
       {
         rel= (Relationship) nodes[i].getRelationships();
         for(int j=i+1;j<nodes.length;++j)
         {
           s=rel.toString();

         }
       }
     System.out.println(s);

    }                                          


    public static void main(String args[])
    {

        java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new search_string().setVisible(true);} });




    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton search_btn;
    private javax.swing.JTextField search_txb;
    // End of variables declaration                   
}

错误首先属于这一行:nodes [i] = hits.getSingle();

我已将其更改为hits = names.get("name", find);以及其他许多代码,但始终会收到错误。

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at graph_traversing.search_string.search_btnActionPerformed(search_string.java:90)
    at graph_traversing.search_string.access$000(search_string.java:14)
    at graph_traversing.search_string$1.actionPerformed(search_string.java:50)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我已经让System.out.println(hits);了解命中是否为空,但是我在输出命中看到有org.neo4j.index.impl.lucene.LuceneIndex$1@481f3834值(这是正确的输出吗?我希望得到节点)。

这是java api,如果你帮助我,这真的很有意思,拜托,我有头晕。

2 个答案:

答案 0 :(得分:3)

您似乎没有将nodes初始化为任何内容。在i处尝试访问null的索引nodes[i]时,您将获得一个空指针。

认为您想要做的是在nodes方法中初始化search_btnActionPerformed,类似于......

private void search_btnActionPerformed(java.awt.event.ActionEvent evt) {                                           
    str=search_txb.getText().split(" ");
    // initialize nodes to a new array with the same size as str
    nodes = new Node[str.length]; 
    for(int i=0;i<str.length;++i)
    ......

}

答案 1 :(得分:1)

hits = names.get("name", find);

可以返回null。您需要检查,否则保证它不会为空。也许您传入的字符串不正确(您确定“name”是一个节点吗?)。也就是说,如果它可以为null,你应该检查null;仅在值为null时才打印。否则,您可以抛出异常或打印错误或再试一次:

if(hits != null){
            System.out.println(hits);
            nodes [i] = hits.getSingle();
            if(nodes[i] != null){
                     System.out.println(nodes[i]);
            }
    }
else{
    System.out.println("'name' not found);
}