Witam,
Poniżej mój kod, który rozliczać ma paliwo w firmie. Okazuje się, że mam błąd 188 linia kodu przy odwołaniu się do pola pustego Stringa, proszę o pomoc. Całe źródła pod linkiem: https://mega.nz/file/Ss0EEQ4I#52jLLDy0Lz_K9YNDOEiGJ7IpflMO3pkqkHlQGCk2K48
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import java.io.IOException;
import java.lang.invoke.VarHandle;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.*;
import java.util.concurrent.atomic.AtomicLong;
import static java.lang.Double.*;
public class Main extends Application {
int lastid;
Statement st;
ResultSet rs;
@FXML
private TextField number_card_ID;
@FXML
private DatePicker date_card;
@FXML
private TextField number_card;
@FXML
private TextField counter_at_the_beginning;
@FXML
private TextField counter_at_the_end;
@FXML
private TextField number_of_km_driven;
@FXML
private TextField fuel_level_on_receipt_of_the_card;
@FXML
private TextField fuel_level_when_the_card_is_returned;
@FXML
private TextField number_of_operating_hours_of_the_unit;
@FXML
private TextField burning_oil_while_the_unit_is_running;
@FXML
private TextField amount_of_fuel_purchased;
@FXML
private TextField standard_fuel_consumption;
@FXML
private TextField fuel_actually_used_on_the_card;
@FXML
private TextField save_overheats;
@FXML
private TextField ilosc_zakupionego_paliwa;
@FXML
private TextField zuzyte_paliwo_faktycznie_z_karty;
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("ROZLICZENIE PALIWA");
primaryStage.setScene(new Scene(root, 1200, 768));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void closeItemClicked(ActionEvent event) {
}
public void createdMenuClicked(ActionEvent event) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Stage stage = new Stage();
stage.setTitle("ZESTAWIENIE");
stage.setScene(new Scene(root, 1024, 768));
getLastid();
stage.show();
// Hide this current window (if this is what you want)
//((Node)(event.getSource())).getScene().getWindow().hide();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void onenFilesMethod(ActionEvent event) {
}
public void saveTextToFile(ActionEvent event) {
}
public void getDateMethod(ActionEvent event) {
}
public static Connection connect() throws ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
String url = "jdbc:sqlite:C:\\Program Files\\DB Browser for SQLite\\fuel.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
//final AtomicLong identyfier = new AtomicLong(1);
public void insert() throws ClassNotFoundException {
String sql = ("INSERT INTO tally ('number_card_ID', 'date_card', 'counter_at_the_beginning', 'counter_at_the_end', " +
"'number_of_km_driven', 'fuel_level_on_receipt_of_the_card', 'fuel_level_when_the_card_is_returned', " +
"'number_of_operating_hours_of_the_unit', 'burning_oil_while_the_unit_is_running', " +
"'amount_of_fuel_purchased', 'standard_fuel_consumption', 'fuel_actually_used_on_the_card', 'save_overheats') " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, number_card_ID.getText());
pstmt.setString(2, ((TextField) date_card.getEditor()).getText());
pstmt.setString(3, counter_at_the_beginning.getText());
pstmt.setString(4, counter_at_the_end.getText());
pstmt.setString(5, number_of_km_driven.getText());
pstmt.setString(6, fuel_level_on_receipt_of_the_card.getText());
pstmt.setString(7, fuel_level_when_the_card_is_returned.getText());
pstmt.setString(8, number_of_operating_hours_of_the_unit.getText());
pstmt.setString(9, burning_oil_while_the_unit_is_running.getText());
pstmt.setString(10, amount_of_fuel_purchased.getText());
pstmt.setString(11, standard_fuel_consumption.getText());
pstmt.setString(12, fuel_actually_used_on_the_card.getText());
pstmt.setString(13, save_overheats.getText());
//date_card = new DatePicker();
//date_card.setPromptText("Data");
pstmt.executeUpdate();
conn.close();
} catch (SQLException | ClassNotFoundException e) {
System.out.println(e.getMessage());
}
}
public void getLastid() {
String sql = "select max(number_card_ID) from tally";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
st = conn.createStatement();
rs = st.executeQuery(sql);
if (rs.next()) {
lastid = rs.getInt(1);
lastid++;
number_card_ID.setText(Integer.toString(lastid));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void clickButton() throws ClassNotFoundException, SQLException {
int number1 = Integer.valueOf(counter_at_the_beginning.getText());
int number2 = Integer.valueOf(number_of_km_driven.getText());
int sum1 = number1 + number2;
counter_at_the_end.setText(String.format(String.valueOf(sum1)));
double number3 = valueOf(fuel_level_on_receipt_of_the_card.getText());
//System.out.println(number3);
double number4 = valueOf(amount_of_fuel_purchased.getText());
double number5 = valueOf(fuel_actually_used_on_the_card.getText());
double sum2 = number3 + number4 - number5;
BigDecimal bigDecimal = new BigDecimal(sum2).setScale(2, RoundingMode.HALF_UP);
double newBigDecimal = bigDecimal.doubleValue();
fuel_level_when_the_card_is_returned.setText(String.valueOf(newBigDecimal));
double number6 = valueOf(burning_oil_while_the_unit_is_running.getText());
double sum3 = ((number2 * 28) / 100) + number6;
BigDecimal bigDecimal1 = new BigDecimal(sum3).setScale(2, RoundingMode.HALF_UP);
double newBigDecimal1 = bigDecimal1.doubleValue();
standard_fuel_consumption.setText(String.valueOf(newBigDecimal1));
double number7 = valueOf(number_of_operating_hours_of_the_unit.getText());
double number8 = valueOf(burning_oil_while_the_unit_is_running.getText());
double sum4 = number7 * number8;
BigDecimal bigDecimal2 = new BigDecimal(sum4).setScale(2, RoundingMode.HALF_UP);
double newBigDecimal2 = bigDecimal2.doubleValue();
burning_oil_while_the_unit_is_running.setText(String.valueOf(newBigDecimal2));
// to może być błędne (przedostatnia komórka Excela)
double number9 = valueOf(standard_fuel_consumption.getText());
System.out.println(number9);
double number10 = valueOf(fuel_actually_used_on_the_card.getText());
System.out.println(number10);
number10 = number9;
System.out.println(number10);
fuel_actually_used_on_the_card.setText(String.valueOf(number10));
//oszczędności/przepały
// fuel_actually_used_on_the_card - standard_fuel_consumption
double number11 = valueOf(fuel_actually_used_on_the_card.getText());
double number12 = valueOf(standard_fuel_consumption.getText());
double difference = number11 - number12;
BigDecimal bigDecimal3 = new BigDecimal(difference).setScale(2, RoundingMode.HALF_UP);
double newBigDecimal3 = bigDecimal3.doubleValue();
save_overheats.setText(String.valueOf(newBigDecimal3));
}
}