Problem bei Erstellung eines Index

node

Mitglied
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.

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
 
Bevor sich hier jemand 140 Zeilen Code durchliest, solltest du angeben, WO die NullPointerException geschmissen wird (die Zeile/Stelle wird bei der Fehlermeldung mit angegeben)
 
Sorry das hatte ich nicht bedacht ich glaube der Fehler beginnt ab Zeile 105. Falls es hilft kann ich die komplette Fehlermeldung auch noch posten.

Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at mainwindow.createAndIndexUser(mainwindow.java:87)
	at mainwindow.actionPerformed(mainwindow.java:106)
	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:6504)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6269)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4860)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	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:2713)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
	at java.awt.EventQueue.access$000(EventQueue.java:101)
	at java.awt.EventQueue$3.run(EventQueue.java:666)
	at java.awt.EventQueue$3.run(EventQueue.java:664)
	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:680)
	at java.awt.EventQueue$4.run(EventQueue.java:678)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
 
Zuletzt bearbeitet:
Zeile 87: schau mal, was in dieser Zeile null sein könnte.

Und schwupps: filmDB ist nicht initialisiert - heißt "null" 😉
 
wenn es in Zeile 17 wäre, wofür ist dann Zeile 96
> GraphDatabaseService filmDB = new EmbeddedGraphDatabase(DB_PATH);
da?

Grundverständnis zu Variablen-Deklaration, Zuweisung, Objekterzeung vorhanden?

in Zeile 96 vorne den Typ weg, dann wird keine gleichnamige lokale Variable angelegt, sondern das Attribut filmDB befüllt,
welches dann in anderen Methoden genutzt werden kann

in Zeile 34 steht ja auch
> label=new JPanel(new GridLayout(1,3));
und nicht
> JPanel label=new JPanel(new GridLayout(1,3));
 
Soweit ich das verstanden habe ist <b>GraphDatabaseService filmDB = new EmbeddedGraphDatabase(DB_PATH);</b> zum öffnen bzw. zum erstellen der Datenbank. Kann mich aber auch irren.

Was die Deklaration und Initialisierung von Variablen angeht kenne ich mich halbwegs aus, Objekte sind mir noch nicht ganz vertraut. Bin ja noch Anfänger🙂
 
so hab die Änderungen vorgenommen und es klappt. Da hätte ich auch selber drauf kommen können.

Die in Zeile 17 gesetzte Variable macht macht filmDB für alle sichtbar, sehe ich das richtig?

Gibt es noch irgendwelche Anfänger Fehler?
 
> Die in Zeile 17 gesetzte Variable macht macht filmDB für alle sichtbar, sehe ich das richtig?

in Zeile 17 wird eine weithin sichtbare Variable deklariert, diese Variable ist dann einfach so sichbar, allerdings noch leer,
mit 'setzen' und 'machen' könntest du etwas falsches meinen,
in Zeile 96 wird dann erst ein Objekt in der Variablen gespeichert/ gesetzt/ zugewiesen
 
so in etwa hab ich mir das gedacht. Werde wohl noch ne menge lernen und nachholen müssen aber da bin ich ja genau richtig hier. Ich werde euch weiter empfehlen.:toll:

Danke an alle die mir hier geholfen haben.
 

Zurück
Oben