1

i'm building this program using javafx and sqllite i have button in fxml with fx:id button1 and onAction: WriteToSql

package valgykla;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;

/**
 * FXML Controller class
 *
 * @author Lukas
 */
public class MeniuController implements Initializable {
    @FXML
    private Button button1;

    @FXML
  public static void WriteToSql(ActionEvent sql){    
        Connection con = null;
        PreparedStatement prSt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            con = DriverManager.
                getConnection("jdbc:sqlite:database.db");
            String query = "insert into Meniu(name,code) values(?,?)";
            prSt = con.prepareStatement(query);
           prSt.setString(1, "jack");
           prSt.setString(12, "02545");
            int count = prSt.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block

        }
        // TODO Auto-generated catch block
         finally{
            try{
                if(prSt != null) prSt.close();
                if(con != null) con.close();
            } catch(Exception ex){}


        }
  }

    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    


}

sadly I get error

Caused by: javafx.fxml.LoadException: Error resolving onAction='#WriteToSql', either the event handler is not in the Namespace or there is an error in the script.
file:/C:/Users/Lukas/Desktop/lukasX/Valgykla/dist/run1382723305/Valgykla.jar!/valgykla/Meniu.fxml:29
Bacteria
  • 8,406
  • 10
  • 50
  • 67
pregot
  • 31
  • 5

2 Answers2

1

The WriteToSql method is static. JavaFX does not consider static methods of the controller for event handlers (anymore)... (See also javafx 8 compatibility issues - FXML static fields)

Simply removing the static keyword should solve the issue. Furthermore since you annotated the method with @FXML you can also make it private:

@FXML
private void WriteToSql(ActionEvent sql){
    ...
Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114
-1

emmmm,when I first try it on eclipse i use onAction="#youfuctionname",then cause a error.I check it all,nothing wrong.then i delete the symbol #,it works well.But after a while,when I try to run another program,it gets error,and I don't know why,I search it online and see u put # in your onAction name,finally I put # back to my onAction name ,weirdly it works. my ide always happen so many strangely problems........not only eclipes

jayce_hu
  • 1
  • 1
  • 1
    Can you clean up the language and formatting a little? It's unclear whether this is a working answer, or a 'me too' type of response. Please keep answers short, clear and to the point. – roelofs Nov 16 '17 at 01:29