2017-05-13 2 views
0

Ich möchte ein Bibliotheksverwaltungssystem erstellen, in dem ich das Buch in meine Datenbank einfügen möchte, die perfekt funktioniert. Aber wenn ich Daten aus meiner Datenbank abrufen möchte, meine Klasse Loader das ist die Struktur meines Projekts kann nicht mein fxml Datei laden enter image description hereMeine Loader-Klasse kann die .fxml-Datei nicht laden

Dies ist mein Code Book_listController.java

package library.listbook; 

import java.net.URL; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ResourceBundle; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import library.database.DatabaseHandler; 

public class Book_listController implements Initializable { 

    ObservableList<Book> list = FXCollections.observableArrayList(); 
    @FXML 
    private TableView<Book> tableview; 
    @FXML 
    private TableColumn<Book, String> titlecol; 

    @FXML 
    private TableColumn<Book, String> idcol; 

    @FXML 
    private TableColumn<Book, String> availabilitycol; 

    @FXML 
    private TableColumn<Book, String> authorcol; 

    @FXML 
    private TableColumn<Book, String> publishercol; 
    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     iniCol(); 
     try { 
      loadData(); 
     } catch (SQLException ex) { 
      Logger.getLogger(Book_listController.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
    private void iniCol(){ 
    titlecol.setCellValueFactory(new PropertyValueFactory<>("title")); 
    idcol.setCellValueFactory(new PropertyValueFactory<>("id")); 
    authorcol.setCellValueFactory(new PropertyValueFactory<>("author")); 
    publishercol.setCellValueFactory(new PropertyValueFactory<>("publisher")); 
    availabilitycol.setCellValueFactory(new PropertyValueFactory<>("available")); 
    } 
    private void loadData() throws SQLException{ 
    DatabaseHandler handler=new DatabaseHandler(); 
    String qu="select * from book"; 
    ResultSet resultset=handler.exeQuery(qu); 
    try{ 
    while(resultset.next()){ 
    String id=resultset.getString("id"); 
    String title=resultset.getString("title"); 
    String author=resultset.getString("author"); 
    String publisher=resultset.getString("publisher"); 
    String avail=resultset.getString("isAvail"); 

    list.add(new Book(id,title,author,publisher,avail)); 
    } 
    }catch(SQLException ex){ 
    Logger.getLogger(Book_listController.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    tableview.getItems().setAll(list); 
    } 
    public static class Book{ 
    private final SimpleStringProperty title; 
    private final SimpleStringProperty id; 
    private final SimpleStringProperty author; 
    private final SimpleStringProperty publisher; 
    private final SimpleStringProperty available; 
    Book(String title,String id,String author,String pub,String avail){ 
    this.title=new SimpleStringProperty(title); 
    this.id=new SimpleStringProperty(id); 
    this.author=new SimpleStringProperty(author); 
    this.publisher=new SimpleStringProperty(pub); 
    this.available=new SimpleStringProperty(avail); 
    } 
     public String getTitle() { 
      return title.get(); 
     } 

     public String getId() { 
      return id.get(); 
     } 

     public String getAuthor() { 
      return author.get(); 
     } 

     public String getPublisher() { 
      return publisher.get(); 
     } 

     public String getAvailable() { 
      return available.get(); 
     } 




    } 
} 

Book_listloader.java `

package library.listbook; 

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

/** 
* 
* @author Dell 
*/ 
public class Book_listloader extends Application { 
    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("book_list.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 

und das ist mein fxml Datei book_list.fxml `

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.net.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="library.listbook.Book_listController"> 
    <stylesheets> 
     <URL value="@book_list.css" /> 
    </stylesheets> 
    <children> 
     <TableView fx:id="tableview" layoutX="37.0" layoutY="14.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <columns> 
      <TableColumn fx:id="titlecol" prefWidth="125.0" text="Book Title" /> 
      <TableColumn fx:id="idcol" minWidth="2.0" prefWidth="111.0" text="Book id" /> 
      <TableColumn fx:id="authorcol" prefWidth="110.0" text="Author" /> 
      <TableColumn fx:id="publishercol" prefWidth="141.0" text="Publisher" /> 
      <TableColumn fx:id="availabilitycol" prefWidth="112.0" text="Availability" /> 
     </columns> 
     </TableView> 
    </children> 
</AnchorPane> 
    ` 
+0

[Try this] (http://stackoverflow.com/a/43934700/4310386) –

Antwort

0

Ihre Datei book_list.fxml innerhalb des Ordners ist library.listbook, aber wenn man getClass().getResource es nennen Ressourcen innerhalb Ressourcen-Ordner. Versuchen wir, den Pfad zu Ihrer Datei zu ändern. Ex: library.listbook\book_list.fxml

+0

Danke für Antwort. Ich versuche das, aber das kann auch nicht laden meine fxml-Datei –

+0

Haben Sie Folgendes versucht: 'Parent root = FXMLLoader.load (getClass(). GetResource ("/main.fxml "));' [Referenz] (https://www.google.com.vn/search?q=Location+is+required&oq=Location+ist+required&aqs=chrome..69i57&sourceid=chrome&ie=UTF-8#q=Location+is+required+%2B+FXMLLoader) gibt es viele andere trafen das gleiche Problem –

0

Seine werfen eine Ausnahme

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException: Location is required. 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at library.listbook.loader.start(loader.java:25) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    ... 1 more 
Exception running application library.listbook.loader 
C:\Users\Dell\Documents\NetBeansProjects\Library\nbproject\build-impl.xml:1052: The following error occurred while executing this line: 
C:\Users\Dell\Documents\NetBeansProjects\Library\nbproject\build-impl.xml:806: Java returned: 1 
BUILD FAILED (total time: 0 seconds) 
Verwandte Themen