Hallo zusammen,
ich bin neu hier und hoffe das mir jemand helfen kann.
Ich quäl mich gerade durch das Tutorial von Neo4j für ein kleines Filmdatenbank Projekt. Allerdings komme ich im Moment leider nicht weiter wegen einer NullPointerException.
Ich hoffe das mir jemand einen Gedankenanstoß geben könnte.
Ps. falls es eine für Anfänger besser geeignete NoSQL Datenbank geben sollte dann immer raus damit.
mfg
ich bin neu hier und hoffe das mir jemand helfen kann.
Ich quäl mich gerade durch das Tutorial von Neo4j für ein kleines Filmdatenbank Projekt. Allerdings komme ich im Moment leider nicht weiter wegen einer NullPointerException.
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
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.Index;
import org.neo4j.kernel.EmbeddedGraphDatabase;
public class mainwindow extends JFrame implements ActionListener {
private static final String DB_PATH = "db/filmDB"; //pfad zur datenbank
private static final String USERNAME_KEY = "username";
private static GraphDatabaseService filmDB;
private static Index<Node> nodeIndex;
private JButton createNode1, terminate;
private JTextField txt_eingabe1, txt_eingabe2, txt_eingabe3;
private JPanel knoepfe, textfelder, label;
private JLabel node1, node2, beziehung;
public mainwindow(int b, int h, int x, int y, String t, int closeOp){
//Constructor area
this.setSize(b,h);
this.setLocation(x,y);
this.setTitle(t);
this.setDefaultCloseOperation(closeOp);
this.setLayout(new BorderLayout(2,2));
//creating area
label=new JPanel(new GridLayout(1,3));
knoepfe=new JPanel(new GridLayout(1,2));
textfelder=new JPanel(new GridLayout(1,3));
node1=new JLabel("Name Knoten 1", node1.CENTER);
node2=new JLabel("Name Knoten 2", node2.CENTER);
beziehung=new JLabel("beziehung zu", beziehung.CENTER);
txt_eingabe1=new JTextField();
txt_eingabe2=new JTextField("KNOWS");
txt_eingabe3=new JTextField();
createNode1=new JButton("Erstelle ersten Knoten");
terminate=new JButton("VORSICHT, LÖSCHT ALLES!!!");
//add area
this.add(BorderLayout.NORTH, label);
this.add(BorderLayout.CENTER, textfelder);
this.add(BorderLayout.SOUTH, knoepfe);
label.add(node1);
label.add(beziehung);
label.add(node2);
textfelder.add(txt_eingabe1);
textfelder.add(txt_eingabe2);
textfelder.add(txt_eingabe3);
knoepfe.add(createNode1);
createNode1.addActionListener(this);
knoepfe.add(terminate);
terminate.addActionListener(this);
this.setVisible(true);
}
private static void registerShutdownHook( final GraphDatabaseService filmDB ) {
//stellt sicher das die datenbank sicher geschlossen wurde
Runtime.getRuntime().addShutdownHook( new Thread() {
@Override
public void run() {
filmDB.shutdown();
}
} );
}
public static enum RelTypes implements RelationshipType{
//hier sind alle beziehungstypen eingetragen
//KNOWS //fuer test auskommentiert
USERS_REFERENCE,
USER
}
//start testfunktionen
public static String idToUserName( final int id ) {
return "user" + id + "@neo4j.org";
}
public static Node createAndIndexUser( final String username ) {
Node node = filmDB.createNode();
node.setProperty( USERNAME_KEY, username );
nodeIndex.add( node, USERNAME_KEY, username );
return node;
}
//ende testfunktionen
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals(createNode1.getText())){
GraphDatabaseService filmDB = new EmbeddedGraphDatabase(DB_PATH);
nodeIndex = filmDB.index().forNodes( "nodes" );
registerShutdownHook(filmDB);
Transaction tx = filmDB.beginTx();
try {
Node usersReferenceNode = filmDB.createNode();
filmDB.getReferenceNode().createRelationshipTo(usersReferenceNode, RelTypes.USERS_REFERENCE );
//bis hier funktioniert es
for ( int id = 0; id < 100; id++ ) {
Node userNode = createAndIndexUser( idToUserName( id ) );
usersReferenceNode.createRelationshipTo( userNode, RelTypes.USER );
}
tx.success();
} finally {
tx.finish();
}
filmDB.shutdown();
}
if(e.getActionCommand().equals(terminate.getText())){
//löscht alle knoten und beziehungen
GraphDatabaseService filmDB = new EmbeddedGraphDatabase(DB_PATH);
registerShutdownHook(filmDB);
Transaction tx = filmDB.beginTx();
try {
for(Node node : filmDB.getAllNodes()){
for(Relationship rel : node.getRelationships()) {
rel.delete();
}
node.delete();
}
tx.success();
} finally {
tx.finish();
}
filmDB.shutdown();
}
}
public static void main(String [] args) {
new mainwindow(500,125,10,10,"neo4j test",mainwindow.EXIT_ON_CLOSE);
}
}
Ich hoffe das mir jemand einen Gedankenanstoß geben könnte.
Ps. falls es eine für Anfänger besser geeignete NoSQL Datenbank geben sollte dann immer raus damit.
mfg