> Java > 본문

'this.colName'이 null이므로 'javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)'을 호출할 수 없습니다.

PHPz
풀어 주다: 2024-02-10 13:15:18
앞으로
813명이 탐색했습니다.

php小编新一在使用JavaFX时遇到了一个问题,即在调用"javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)"时出现了"this.colName为null"的错误。这个错误的原因是因为colName为空,导致无法调用该方法。为了解决这个问题,我们需要检查colName是否正确初始化,并且确保其不为空。只有在colName正确初始化之后,才能成功调用"javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)"方法。希望以上解答对您有帮助!

问题内容

所以我的问题是我无法将数据显示到表视图,我不明白为什么 我有一个表单使用 productcontroller 和显示 tableview 方法,在另一个控制器 cardproductcontrller 中,我在 addfcn 方法中调用 productcontroller 中的方法,它返回错误:

无法调用 javafx.scene.control.tablecolumn.setcellvaluefactory(javafx.util.callback),因为 this.colname 为 null

这里有更多详细错误:

java.lang.nullpointerexception: cannot invoke "javafx.scene.control.tablecolumn.setcellvaluefactory(javafx.util.callback)" because "this.colname" is null
    at application.productcontroller.showorderdetail(productcontroller.java:409)
    at application.cardprodcontroller.addfcn(cardprodcontroller.java:169)
    at java.base/jdk.internal.reflect.directmethodhandleaccessor.invoke(directmethodhandleaccessor.java:103)
    at java.base/java.lang.reflect.method.invoke(method.java:580)
    at com.sun.javafx.reflect.trampoline.invoke(methodutil.java:72)
    at java.base/jdk.internal.reflect.directmethodhandleaccessor.invoke(directmethodhandleaccessor.java:103)
    at java.base/java.lang.reflect.method.invoke(method.java:580)
    at javafx.base@21/com.sun.javafx.reflect.methodutil.invoke(methodutil.java:270)
    at javafx.fxml@21/com.sun.javafx.fxml.methodhelper.invoke(methodhelper.java:84)
    at javafx.fxml@21/javafx.fxml.fxmlloader$methodhandler.invoke(fxmlloader.java:1855)
    at javafx.fxml@21/javafx.fxml.fxmlloader$controllermethodeventhandler.handle(fxmlloader.java:1726)
    at javafx.base@21/com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(compositeeventhandler.java:86)
    at javafx.base@21/com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:232)
    at javafx.base@21/com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:189)
    at javafx.base@21/com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(compositeeventdispatcher.java:59)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:58)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.eventutil.fireeventimpl(eventutil.java:74)
    at javafx.base@21/com.sun.javafx.event.eventutil.fireevent(eventutil.java:49)
    at javafx.base@21/javafx.event.event.fireevent(event.java:198)
    at javafx.graphics@21/javafx.scene.node.fireevent(node.java:8875)
    at javafx.controls@21/javafx.scene.control.button.fire(button.java:203)
    at javafx.controls@21/com.sun.javafx.scene.control.behavior.buttonbehavior.mousereleased(buttonbehavior.java:207)
    at javafx.controls@21/com.sun.javafx.scene.control.inputmap.inputmap.handle(inputmap.java:274)
    at javafx.base@21/com.sun.javafx.event.compositeeventhandler$normaleventhandlerrecord.handlebubblingevent(compositeeventhandler.java:247)
    at javafx.base@21/com.sun.javafx.event.compositeeventhandler.dispatchbubblingevent(compositeeventhandler.java:80)
    at javafx.base@21/com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:232)
    at javafx.base@21/com.sun.javafx.event.eventhandlermanager.dispatchbubblingevent(eventhandlermanager.java:189)
    at javafx.base@21/com.sun.javafx.event.compositeeventdispatcher.dispatchbubblingevent(compositeeventdispatcher.java:59)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:58)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.basiceventdispatcher.dispatchevent(basiceventdispatcher.java:56)
    at javafx.base@21/com.sun.javafx.event.eventdispatchchainimpl.dispatchevent(eventdispatchchainimpl.java:114)
    at javafx.base@21/com.sun.javafx.event.eventutil.fireeventimpl(eventutil.java:74)
    at javafx.base@21/com.sun.javafx.event.eventutil.fireevent(eventutil.java:54)
    at javafx.base@21/javafx.event.event.fireevent(event.java:198)
    at javafx.graphics@21/javafx.scene.scene$mousehandler.process(scene.java:3984)
    at javafx.graphics@21/javafx.scene.scene.processmouseevent(scene.java:1890)
    at javafx.graphics@21/javafx.scene.scene$scenepeerlistener.mouseevent(scene.java:2708)
    at javafx.graphics@21/com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:411)
    at javafx.graphics@21/com.sun.javafx.tk.quantum.glassvieweventhandler$mouseeventnotification.run(glassvieweventhandler.java:301)
    at java.base/java.security.accesscontroller.doprivileged(accesscontroller.java:400)
    at javafx.graphics@21/com.sun.javafx.tk.quantum.glassvieweventhandler.lambda$handlemouseevent$2(glassvieweventhandler.java:450)
    at javafx.graphics@21/com.sun.javafx.tk.quantum.quantumtoolkit.runwithoutrenderlock(quantumtoolkit.java:424)
    at javafx.graphics@21/com.sun.javafx.tk.quantum.glassvieweventhandler.handlemouseevent(glassvieweventhandler.java:449)
    at javafx.graphics@21/com.sun.glass.ui.view.handlemouseevent(view.java:551)
    at javafx.graphics@21/com.sun.glass.ui.view.notifymouse(view.java:937)
    at javafx.graphics@21/com.sun.glass.ui.win.winapplication._runloop(native method)
    at javafx.graphics@21/com.sun.glass.ui.win.winapplication.lambda$runloop$3(winapplication.java:185)
    at java.base/java.lang.thread.run(thread.java:1583)
로그인 후 복사

这里是代码: 卡产品控制器:

package application;

import java.net.url;
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.date;
import java.util.optional;
import java.util.resourcebundle;

import javafx.fxml.fxml;
import javafx.fxml.initializable;
import javafx.scene.control.alert;
import javafx.scene.control.alert.alerttype;
import javafx.scene.control.button;
import javafx.scene.control.buttontype;
import javafx.scene.control.label;
import javafx.scene.control.spinner;
import javafx.scene.control.spinnervaluefactory;
import javafx.scene.image.image;
import javafx.scene.image.imageview;
import javafx.scene.layout.borderpane;

public class cardprodcontroller implements initializable {

    @fxml
    private button addbtn;

    @fxml
    private borderpane cardform;

    @fxml
    private imageview imageview;

    @fxml
    private label name;

    @fxml
    private label price;

    @fxml
    private spinner<integer> quantity;
    
    private productmodel productm;
    
    private image image;
    
    private string proid;
    private string pro_image;
    private string pro_date;
    
    private spinnervaluefactory<integer> spin;
    
    @fxml 
    private connection connection;
    private preparedstatement prepare;
    private resultset result;
    private alert alert;

    private double pr;
    private int cusid;
    
    public void setdata(productmodel productm) {
        this.productm = productm;
        
        pro_image = productm.getproductimage();
        pro_date = string.valueof(productm.getdate());
        proid = productm.getproductid();
        name.settext(productm.getproductname());
        price.settext(string.valueof(productm.getproductprice()));
        string path = "file:" + productm.getproductimage();
        image = new image(path, 190, 130, false, true);
        imageview.setimage(image);
        pr = productm.getproductprice();

    }
    
    private int qty;
    private double totalp;
    private string orddid;
    public void addfcn() throws sqlexception {
            productcontroller pc = new productcontroller();
            pc.customerid(); 
            pc.order_detailid();
            qty = quantity.getvalue();
            string check = "";
            string checkavailable = "select pro_status from product where pro_id = '" + proid + "'";
            
            connection = database.connect();
            try {
                int checks = 0;
                string checkstock = "select pro_quantity from product where pro_id = '" + proid + "'";

                prepare = connection.preparestatement(checkstock);
                result = prepare.executequery();
                
                if(result.next()) {
                    checks = result.getint("pro_quantity");
                }      
                
                if(checks == 0){
                    string updatestock = "update product set pro_status = 'unavailable' where pro_id = '"+proid+"'";   
                    //update to database to mark it as unavailable
                    prepare = connection.preparestatement(updatestock);
                    prepare.executeupdate();
                }
                prepare = connection.preparestatement(checkavailable);
                result = prepare.executequery();

                if (result.next()) {
                    check = result.getstring("pro_status");
                }
                if (!check.equals("available") || qty == 0) {
                    alert = new alert(alerttype.error);
                    alert.settitle("error message");
                    alert.setheadertext(null);
                    alert.setcontenttext("product currently unavalible or we run out");
                    alert.showandwait();
                } else {
                    if (checks < qty) {
                        alert = new alert(alerttype.error);
                        alert.settitle("error message");
                        alert.setheadertext(null);
                        alert.setcontenttext("invalid. this product is out of stock");
                        alert.showandwait();
                    } else {
                        pro_image = pro_image.replace("\\", "\\\\");
                        
                        string insertdata = "insert into order_detail "
                                + "(cus_id, ord_id, pro_id ,pro_name, pro_price, pro_quantity, orderdate) "
                                + "values(?,?,?,?,?,?,?)";
                        prepare = connection.preparestatement(insertdata);
                        prepare.setstring(1, string.valueof(data.cusid));
                        
                        orddid = data.orddid;
                        prepare.setstring(2, orddid);
                        prepare.setstring(3, proid);
                        prepare.setstring(4, name.gettext());
                        
                        totalp = (qty * pr);
                        prepare.setstring(5, string.valueof(totalp));
                        prepare.setstring(6, string.valueof(qty));
                        
                        date date = new date();
                        java.sql.date sqldate = new java.sql.date(date.gettime());
                        prepare.setstring(7, string.valueof(sqldate));
                        
                        prepare.executeupdate();

                        int upstock = checks - qty;

                        string updatestock = "update product set pro_quantity = " + upstock + " where pro_id = '"+proid+"'";
                        //update product to mark it's quantity as decreased as being order by customer 
                        prepare = connection.preparestatement(updatestock);
                        prepare.executeupdate();
                        
                        alert = new alert(alerttype.information);
                        alert.settitle("information message");
                        alert.setheadertext(null);
                        alert.setcontenttext("successfully added!");
                        optional<buttontype> option = alert.showandwait();
                        
                        if(option.get().equals(buttontype.ok)) {
                            
                            
                            pc.showorderdetail();
                        }
                        //pc.menugettotal();//tinh sau
                    }
                }
            } catch (exception e) {
                e.printstacktrace();
            }   
        }
    
    public void setquantity() {
        spin = new spinnervaluefactory.integerspinnervaluefactory(1, 100, 0);
        quantity.setvaluefactory(spin);
    }
    
    @override
    public void initialize(url arg0, resourcebundle arg1) {
        setquantity();
    }
    
}
로그인 후 복사

产品控制器:

package application;

import java.io.ioexception;
import java.net.url;
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.optional;
import java.util.resourcebundle;

import com.mysql.cj.x.protobuf.mysqlxcrud.column;

import javafx.collections.fxcollections;
import javafx.collections.observablelist;
import javafx.event.actionevent;
import javafx.fxml.fxml;
import javafx.fxml.fxmlloader;
import javafx.fxml.initializable;
import javafx.geometry.insets;
import javafx.scene.node;
import javafx.scene.parent;
import javafx.scene.scene;
import javafx.scene.control.alert;
import javafx.scene.control.alert.alerttype;
import javafx.scene.control.button;
import javafx.scene.control.buttontype;
import javafx.scene.control.label;
import javafx.scene.control.tablecolumn;
import javafx.scene.control.tableview;
import javafx.scene.control.cell.propertyvaluefactory;
import javafx.scene.layout.anchorpane;
import javafx.scene.layout.gridpane;
import javafx.scene.layout.pane;
import javafx.stage.stage;

public class productcontroller implements initializable{
    @fxml
    private gridpane gridprod;

    @fxml
    private button clbtn;

    @fxml
    private button donebtn;
 
    @fxml
    private button clrbtn;

    @fxml
    private tablecolumn<order_detailmodel, string> colname;

    @fxml
    private tablecolumn<order_detailmodel, string> colprice;

    @fxml
    private tablecolumn<order_detailmodel, string> colquantity;
    
    @fxml
    private tableview<order_detailmodel> orderview;
    
    @fxml
    private button paybtn;

    @fxml
    private button printbillbtn;

    @fxml
    private label totalbill;
    
    @fxml 
    private connection connection;
    private preparedstatement prepare;
    private resultset result;
    private alert alert;
    
    @fxml
    private stage sidestage;
    private scene scene;
    private parent root;

    private int cid;
    public void customerid() throws sqlexception {
         string sql = "select max(cus_id) from customer";
         connection = database.connect();
         
         try {
             prepare = connection.preparestatement(sql);
             result = prepare.executequery();
             if (result.next()) {
                 cid = result.getint("max(cus_id)");
             }
             string checkcid = "select max(cus_id) from receipt";
             prepare = connection.preparestatement(checkcid);
             result = prepare.executequery();
             int checkid = 0;
             if (result.next()) {
                 checkid = result.getint("max(cus_id)");
             }
             if (cid == 0) {
                 cid += 1;
             } else if (cid == checkid) {
                 cid += 1;
             }
             data.cusid = cid;
         } catch (exception e) {
             e.printstacktrace();
         }
    }
    
    private string odid;
    public void order_detailid() throws sqlexception {
     string sql = "select max(ord_id) from order_detail";
        connection = database.connect();
        
        try {
            prepare = connection.preparestatement(sql);
            result = prepare.executequery();
            if (result.next()) {
                odid = result.getstring("max(ord_id)");
            }
            string checkodid = "select max(ord_id) from receipt ";
            prepare = connection.preparestatement(checkodid);
            result = prepare.executequery();
            string checkid = null;
            if (result.next()) {
                checkid = result.getstring("max(ord_id)");
            }
            if (odid == null) {
                string aim = "ord-" + 0;
                odid = aim + 1;
            } else if (odid == checkid) {
                string aim = "ord-" + 0;
                odid = aim + 1;
            }
            data.orddid = odid;
            system.out.println(data.orddid);
        } catch (exception e) {
            e.printstacktrace();
        }
   }
    

       private observablelist<productmodel> carddatalist = fxcollections.observablearraylist();
        
        public observablelist<productmodel> getdata(){
            
            observablelist<productmodel> listdata = fxcollections.observablearraylist();
            
            string getdata = "select * from product";
            try {
                connection = database.connect();
            } catch (sqlexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            }
            try {
                prepare = connection.preparestatement(getdata);
                result = prepare.executequery();
                
                productmodel productm;
                
                while(result.next()) {
                    productm = new productmodel(result.getint("id")
                                                ,result.getstring("pro_id")
                                                , result.getstring("pro_name")
                                                , result.getdouble("pro_price")
                                                , result.getint("pro_quantity")
                                                , result.getstring("pro_image"));
                    listdata.add(productm);
                }
            }catch (sqlexception e) {
                // todo: handle exception
                e.printstacktrace();
            }
            return listdata;
        }
        
        public void menuprod() {
            carddatalist.clear();
            carddatalist.addall(getdata());
            
            int row = 0; 
            int column = 0;
            gridprod.getchildren().clear();
            gridprod.getrowconstraints().clear();
            gridprod.getcolumnconstraints().clear();
            
            for(int i  = 0; i < carddatalist.size(); i++) {
                try {
                    fxmlloader loader = new fxmlloader();
                    loader.setlocation(getclass().getresource("cardproduct.fxml"));
                    pane pane = loader.load();
                    cardprodcontroller cpc = loader.getcontroller();
                    cpc.setdata(carddatalist.get(i));
                    
                    if(column == 3) {
                        column = 0;
                        row += 1;
                    }
                    
                    gridprod.add(pane, column++, row);
                    
                    gridpane.setmargin(pane, new insets(10));
                }catch (ioexception e) {
                    // todo: handle exception
                    e.printstacktrace();
                }
            }
        }
        
        public void clfcn(actionevent event) throws ioexception {
            
            root = fxmlloader.load(getclass().getresource("staffsection.fxml"));
            sidestage = (stage)((node)event.getsource()).getscene().getwindow();
            scene = new scene(root);
            sidestage.setscene(scene);
            sidestage.setresizable(false);
            sidestage.show();
        }
        
        public void donefcn(actionevent event) throws ioexception {
            
            root = fxmlloader.load(getclass().getresource("staffsection.fxml"));
            sidestage = (stage)((node)event.getsource()).getscene().getwindow();
            scene = new scene(root);
            sidestage.setscene(scene);
            sidestage.setresizable(false);
            sidestage.show();
        }
        
        public observablelist<order_detailmodel> orderdlist() throws sqlexception {
               observablelist<order_detailmodel> olist = fxcollections.observablearraylist();
               
               string getdata = "select pro_name, pro_price, pro_quantity from order_detail";
               connection = database.connect();
               try {
                    prepare = connection.preparestatement(getdata);
                    result = prepare.executequery();
                    
                    order_detailmodel orddm;
                    while(result.next()) {
                        orddm = new order_detailmodel(result.getstring("pro_name")
                                                        ,result.getdouble("pro_price")
                                                        ,result.getint("pro_quantity"));
                        olist.add(orddm);
                    }
               }catch (exception e) {
                e.printstacktrace();
                system.out.println(e.getmessage());
            }
            return olist; 
        }
        
        observablelist<order_detailmodel> listordd;
        public void showorderdetail() {
            try {
                
                listordd = orderdlist();
                
                colname.setcellvaluefactory(new propertyvaluefactory<>("pro_name"));
                colprice.setcellvaluefactory(new propertyvaluefactory<>("pro_price"));
                colquantity.setcellvaluefactory(new propertyvaluefactory<>("pro_quantity"));
                
                orderview.setitems(listordd);
                
            }catch (exception e) {
                e.printstacktrace();
                e.getmessage();
            }
        }

    @override
    public void initialize(url arg0, resourcebundle arg1) {
        getdata();
        menuprod();
    }
}
로그인 후 복사

fxml 文件使用 productcontroller :

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<BorderPane prefHeight="700.0" prefWidth="1200.0" stylesheets="@CSS/TableServe.css" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ProductController">
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <AnchorPane fx:id="acPane1" layoutX="15.0" layoutY="15.0" prefHeight="670.0" prefWidth="750.0">
               <children>
                  <ScrollPane layoutX="15.0" layoutY="15.0" prefHeight="640.0" prefWidth="720.0">
                     <content>
                        <GridPane fx:id="gridProd" hgap="10.0" prefHeight="879.0" prefWidth="680.0" vgap="10.0">
                          <columnConstraints>
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                              <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                          </columnConstraints>
                          <rowConstraints>
                            <RowConstraints fillHeight="false" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
                            <RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
                            <RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
                              <RowConstraints minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
                          </rowConstraints>
                        </GridPane>
                     </content>
                     <padding>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </padding>
                  </ScrollPane>
               </children>
            </AnchorPane>
            <AnchorPane fx:id="acPane2" layoutX="785.0" layoutY="15.0" prefHeight="670.0" prefWidth="400.0">
               <children>
                  <Pane prefHeight="670.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <TableView fx:id="orderView" layoutX="15.0" layoutY="14.0" prefHeight="456.0" prefWidth="370.0">
                          <columns>
                            <TableColumn fx:id="colName" prefWidth="166.4000244140625" text="Name" />
                            <TableColumn fx:id="colPrice" prefWidth="108.800048828125" text="Price" />
                              <TableColumn fx:id="colQuantity" prefWidth="93.5999755859375" text="Quantity'" />
                          </columns>
                        </TableView>
                        <Button fx:id="payBtn" layoutX="10.0" layoutY="566.0" mnemonicParsing="false" prefWidth="120.0" text="Pay" />
                        <Button fx:id="printBillBtn" layoutX="270.0" layoutY="566.0" mnemonicParsing="false" prefWidth="120.0" text="Print BIll" />
                        <Button fx:id="clrBtn" layoutX="140.0" layoutY="566.0" mnemonicParsing="false" prefWidth="120.0" text="Clear" />
                        <Button fx:id="clBtn" layoutX="225.0" layoutY="629.0" mnemonicParsing="false" onAction="#clFcn" prefHeight="30.0" prefWidth="160.0" text="Cancel" />
                        <Button fx:id="doneBtn" layoutX="17.0" layoutY="629.0" mnemonicParsing="false" onAction="#doneFcn" prefHeight="30.0" prefWidth="160.0" text="Done" />
                        <Label fx:id="totalBill" layoutX="17.0" layoutY="489.0" prefHeight="30.0" prefWidth="173.0" text="Total: 00$$">
                           <font>
                              <Font size="20.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
               </children>
            </AnchorPane>
         </children>
      </AnchorPane>
   </center>
</BorderPane>
로그인 후 복사

我几乎做了所有事情,使用chatgpt得到了ofc的帮助,但仍然不知道为什么,我认为我的sql是错误的?

解决方法

在 fxml 中指定控制器时,不要在代码中创建控制器

切勿在由 fxml 文件中的 fx:controller 属性指定的控制器上调用 new

此代码错误

ProductController pC = new ProductController();
로그인 후 복사

有关更多信息,另请参阅:

当您可以在代码中创建新控制器

在控制器上调用 new 的唯一有效情况是如果这两个条件都为 true:

  • 您将调用 setcontroller 在加载 fxml 之前在 fxmlloader 上。

并且

  • 正在加载的 fxml 文件中指定控制器。

但是,您并没有做任何这些事情。

来自 setcontroller 的文档:

有关在代码中设置控制器的示例,请参阅此答案中标题为“在 fxmlloader 上设置控制器”的部分:

您可能希望在代码中设置控制器的一种情况是与 fx:root 构造结合使用,如 oracle 教程中所述:

在代码中创建新控制器的常见问题

只有 fxmlloader 会将控制器中的 @fxml 注释字段初始化为 fxml 文件中配置的值。如果您只是在代码中创建一个新的控制器实例,而没有在 fxmlloader 上设置该控制器实例并调用 load() 来初始化控制器实例,则控制器实例中的所有 @fxml 注释字段都将为 null

此外,如果您还在其他地方加载了 fxml,并且 fxml 引用了像您一样的控制器,那么当您加载 fxml 时,加载器将创建您指定的控制器的实例在 fxml 中。因此,如果您随后在控制器上调用 new,您将获得控制器的另一个实例,该实例与 fxmlloader 创建的实例以及由 load() 使用的方法。

替代方法

通常,当人们创建新的控制器时,他们应该做的是:

或者

  • 重构他们的代码,以便他们在尝试使用它的地方不需要控制器引用(例如通过 应用 mvc 设计)。

或者

  • 在 fxmlloader 上设置控制器实例,然后要求加载程序 load() 一个 fxml(没有 fx:controller 属性)。这将初始化在代码中创建的控制器实例。

위 내용은 'this.colName'이 null이므로 'javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)'을 호출할 수 없습니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:stackoverflow.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!