0

I have no idea why I get this error message:

Cannot resolve method 'setCellValueFactory' in 'TableColumn'

But when I change to this code in initialize with a test variable, the error message get away:

@Override                                                                                 
public void initialize(URL url, ResourceBundle resourceBundle) {                          
    javafx.scene.control.TableColumn kol2 = null;                                         
    kol2.setCellValueFactory(new PropertyValueFactory<Tabellmodell,String>("name"));        
}                                                                                         

Does anyone know what the problem is with my original code?

package com.example.oppgave;

import javax.swing.table.TableColumn;

import com.example.oppgave.Modell.Ansatt;
import com.example.oppgave.Modell.Tabellmodell;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;

public class AnsattController implements Initializable {
    @FXML
    private Label tilbakemelding;
    @FXML
    private TextField innputNavn;
    @FXML
    private TextField innputId;
    @FXML
    private TableView<Tabellmodell> ansattTabell;
    @FXML
    private TableColumn kolname;
    @FXML
    private TableColumn kolid;

    ObservableList<Tabellmodell> oblist= FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
         kolname.setCellValueFactory(new PropertyValueFactory<Tabellmodell,String>("name"));
    }

    private void listAnsatteTabell(){
        int c;
        try{
            Db nyDb= new Db();
            Connection tilkobling=nyDb.dbnyTilkobling();
            Statement stmt=tilkobling.createStatement();
            System.out.println("Connected to the database");
            String sql = "SELECT id, navn from ansatt;";

            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                oblist.add(new Tabellmodell(rs.getString("id"),rs.getString("navn")));
            }

ansattTabell.setItems(oblist); 
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
}

FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.oppgave.AnsattController">
   <children>
      <TextField fx:id="innputNavn" layoutX="61.0" layoutY="110.0" />
      <TextField fx:id="innputId" layoutX="61.0" layoutY="156.0" />
      <Label layoutX="26.0" layoutY="114.0" text="Navn" />
      <Label layoutX="26.0" layoutY="160.0" text="Id" />
      <Button fx:id="regAnsatt" layoutX="61.0" layoutY="200.0" mnemonicParsing="false" onAction="#regNyAnsatt" text="Registrer" />
      <TableView fx:id="ansattTabell" editable="true" layoutX="306.0" layoutY="89.0" prefHeight="200.0" prefWidth="273.0">
        <columns>
          <TableColumn prefWidth="75.0" text="ID" fx:id="kolid"/>
          <TableColumn prefWidth="191.0" text="Navn" fx:id="kolname"/>
        </columns>
      </TableView>
      <Label fx:id="tilbakemelding" layoutX="26.0" layoutY="72.0" />
      <Label layoutX="212.0" layoutY="39.0" text="Ansatt">
         <font>
            <Font size="27.0" />
         </font>
      </Label>
   </children>
</Pane>

I also seem to get a error message when the @FXML variable and the x:id in the fxml is the same

revl
  • 3
  • 6
  • Enable all compiler warnings, and pay attention to them. Those warnings will tell you that you are using a [raw type](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). You need to change `TableColumn` to `TableColumn`. – VGR Mar 22 '22 at 00:29

1 Answers1

-2

You are trying to set attribute of null object. I think that may be the case. Test it out and reach out to me if it doesn't work.

Johnys
  • 111
  • 6
  • What do you mean I should do? – revl Mar 21 '22 at 10:52
  • Try initializing it as new TableColumn(). Dont understand why did you downvote the answer. – Johnys Mar 21 '22 at 10:54
  • I did not downvote you. I get error when @FXML variable and the fx:id in the fxml is the same. I tried to `add javafx.scene.control.TableColumn kolname = new javafx.scene.control.TableColumn(); kolname.setCellValueFactory(new PropertyValueFactory("name")); ` but it did not change anything – revl Mar 21 '22 at 11:06
  • I don't have enough reputation to upvote or downvote. I added the whole code file – revl Mar 21 '22 at 11:18
  • Then i dont know why would anyone downvote without providing better solution. Btw I think i cracked it. You are using TableColumn from javax.swing. I think you should organize your imports. – Johnys Mar 21 '22 at 12:02
  • WOW thank you!! I have tried for 4 days to rewrite the code and google and google all day. I changed the import and the errors disappear, but after I runned the program and executed the function I get a new error: `module javafx.base cannot access class com.example.oppgave.Modell.Tabellmodell (in module com.example.oppgave) because module com.example.oppgave does not open com.example.oppgave.Modell to javafx.base` Do you have any idea what that means? – revl Mar 21 '22 at 12:15
  • in module-info.java you should have this -- opens Modell to javafx.base (I dont know the exact syntax of the class, check your module-path ). Basically it cannot access the class becuase its not visible to the module – Johnys Mar 21 '22 at 12:50