0

I am trying to initiate the save of a .txt file on the form side after a save button is chosen. The save is going to happen on the business package. However, it doesn't seem to be working. I can't figure out why. Here is what I have on the form side:

private void jMnuSaveActionPerformed(java.awt.event.ActionEvent evt) {                                         
    statusMessageLabel.setText("");
    JFileChooser f = new JFileChooser(".");
    String path = "";
    f.setDialogTitle("Save Asset Depreciation File");
    FileNameExtensionFilter filter = new FileNameExtensionFilter ("Text File (*.txt)", "txt");
    f.setFileFilter(filter);
    JDialog dg = new JDialog();
    int rval = f.showSaveDialog(dg);
    if (rval == f.CANCEL_OPTION){
        statusMessageLabel.setText("Open canceled.");
    }
    else if (rval == f.APPROVE_OPTION){
        path = f.getSelectedFile().getName();
        a.setSave(path);
        statusMessageLabel.setText(path);
    }
}                                        

And here is what I have on the business package/Asset class side:

public boolean setSave(String p){
    boolean result = true;
    try{
        PrintWriter out = new PrintWriter(
        new FileWriter(p + ".txt", true));
        out.println("Asset Name: " + this.AssetNm);
        out.println("Salvage value: " + this.salvage);
        out.println("Cost: " + this.cost);
        out.println("Life (years): " + this.life);
        out.close();
    }catch (IOException e){
        result = false;
        this.emsg = "Write status error: " + e.getMessage();
    }
    return result;
}
aseb1986
  • 5
  • 1
  • 1
    `it doesn't seem to be working`, can you be more specific, what is not working, no file, error message, etc. – PeterMmm Sep 07 '16 at 15:21
  • It gives me a NullPointerException – aseb1986 Sep 07 '16 at 15:29
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at depcalc.DepCalcView.jMnuSaveActionPerformed(DepCalcView.java:462) at depcalc.DepCalcView.access$1000(DepCalcView.java:34) at depcalc.DepCalcView$6.actionPerformed(DepCalcView.java:307) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) – aseb1986 Sep 07 '16 at 15:30
  • 1
    And what is line `462` in `jMnuSaveActionPerformed` ? probably it is `a.setSave(path);` and `a` is null. – PeterMmm Sep 07 '16 at 15:39
  • You're right. a.setSave(path); is line 462. Why do you think a is null? I have the Asset class saved as a global object as 'a'. – aseb1986 Sep 07 '16 at 16:15
  • I can't tell you why `a` is null. Probably it isn't initialized before you use it. You can do `Asset aa = new Asset(); aa.setPath(path);` – PeterMmm Sep 07 '16 at 16:26

0 Answers0