我真的不知道还能做什么,我到处搜索,尝试了任何方法但无法解决我的问题。我创建了一个 neo4j 图形数据库并通过 localhost:7474(视觉)创建了节点名称属性的索引,现在我想通过搜索它们的名称属性索引来获取节点号,但我得到空指针异常错误,我知道这是因为存在一个空值,我知道这是从哪里来的,但我无法解决。
这是代码:
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);
了解 hits 是否为空,但我在输出中看到 hits 有 org.neo4j.index.impl.lucene.LuceneIndex$1@481f3834
值(这是正确的输出吗?我希望得到节点)。
这是java api,如果你能帮助我,你真的会很友善,拜托,我头晕。