-3

I created a debtor system which creates debtor proflies which are can be displayed in a table or edited, my data storage for the debtors is done through the use of a text file. The problem I have encountered is that when I run the program as an executable file and try to add a new debtor, which works perfectly fine when running through netbeans, the debtor is not added and I get the "Error saving debtor information" from my try cathc statement. I would assume this has to do the executable file not having access or not being able to find the text file but I am not sure.

Until now, what I have tried is to simply move the text file into the same dist file the executable file is in and changed any references to the path to the new location, this did not work but I am pretty new to using text files so I could just have done something incorrectly.

enter image description here


package acaivendorgui;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.swing.*;


public class NewDebtor extends javax.swing.JFrame {

    public NewDebtor() {
        initComponents();
    }    
    private double calcDebt() {
        double cost = ((int) acaiBought.getValue() * 5) + ((int) btBought.getValue() * 2) 
                      + ((int) stBought.getValue() * 3.50) + ((int) pmBought.getValue() * 2) + ((int) cwBought.getValue() * 7.50)
                      + ((int) wBought.getValue() * 5) + ((int) sBought.getValue() * 7.50);
        double payment = Double.parseDouble(newDebtorsAmountPaid.getText());
        return(cost - payment);
    }     
    
private void menuButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
       this.setVisible(false);
       new Menu().setVisible(true);
    }                                          

    private void newDebtorsAmountPaidActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        // TODO add your handling code here:
    }                                                    

    private void debtDisplayActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if(newDebtorsName.getText().isEmpty() || newDebtorsSurname.getText().isEmpty() || newDebtorsAmountPaid.getText().isEmpty() || newDate.getDate() == null){
            JOptionPane.showMessageDialog(null, "Please enter all fields!");
        }
        else{
            String name = newDebtorsName.getText().trim();
            String surname = newDebtorsSurname.getText().trim();
            double debt = calcDebt();              
            String date = ((JTextField)newDate.getDateEditor().getUiComponent()).getText();
            newDateTxtField.setText(date);
            date = newDateTxtField.getText().trim();
            Debtor debtor = new Debtor(name , surname , debt, date);  
            System.out.println(debtor.toString());
            debtDisplay.setText(Double.toString(debt));        
            try {
                FileWriter fw = new FileWriter("debtor.txt", true);
                fw.write(debtor.toString() + System.lineSeparator());
                fw.close();
                JOptionPane.showMessageDialog(null, "Debtor information saved successfully! \n" 
                                                    + "Name: " + debtor.getName() + " " + debtor.getSurname() 
                                                    + "\n" + "Debt: " + debtor.getDebt()
                                                    + "\n" + "Date: " + debtor.getDate());
            } 
            catch (IOException e) {
                JOptionPane.showMessageDialog(null, "Error saving debtor information.");
                e.printStackTrace();
        }
        }
    }                                          

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        newDebtorsName.setText("");
        newDebtorsSurname.setText("");
        newDebtorsAmountPaid.setText("");
        newDate.setDate(null);
        debtDisplay.setText("");
        newDateTxtField.setText("");
        JSpinner[] allbuys = {acaiBought, btBought, stBought, cwBought, wBought, sBought, pmBought};
        for (JSpinner spinner : allbuys) {
            spinner.setValue(0);
        }
    }     
    private void newDateTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
    }                                               

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewDebtor().setVisible(true);
            }
        });
    }

0 Answers0