Sarah Millard
Sarah Millard

Reputation: 1

ActionPerformed and passing an object

`

public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == saleButton)
        {
            processSale(Wine wi);
            //System.out.println("fvghj");
        }
        else if (e.getSource() == returnButton)
        {
                processReturn(Wine wi);
            //System.out.println("fdgchj"); 
        }

    }


    //create a wine object to be returned - responsible for getting details from textfields when either sale or return is pressed
    //this object is then passed to the relevant method in LWMGUI to process either sale or return


public Wine getWine()
    {
        String wName = nameWineText.getText();

        String costBottles = costBottleText.getText();
        int cBottle = Integer.parseInt(costBottles);
        //System.out.println(cBottle);

        //get numBottle
        String numBottles = numBottlesText.getText();
        int nBottle = Integer.parseInt(numBottles);
        //System.out.println(nBottle);

        Wine wi = new Wine(wName, cBottle, nBottle);

        return wi;

    }


    private int processSale(Wine wi)
    {
        int totalSaleAmount = wi.getNumBottle() * (int) wi.getCostBottle();
        //System.out.println(totalAmount);
        transactionText.setText("" + totalSaleAmount);
        return totalSaleAmount;
    }

    private int processReturn(Wine wi)
    {
        int totalReturnAmount = wi.getNumBottle() * (int) wi.getCostBottle();
        //System.out.println(totalReturnAmount);
        transactionText.setText("" + totalReturnAmount);
        return totalReturnAmount;
    }

`I'm having problems with an actionPerformed method. I have got text from user input textfields to give me the cost, quantity and name of a wine, for a sale or return to be processed. I have created a Wine object (wi) with this information, and want to pass it to further methods, whether sale or return.

I can't get the processSale() or processReturn() methods to work, and I'm really confused!! I thought passing the Wine wi object to the methods would work? Here is the relevant code;

Upvotes: 0

Views: 430

Answers (1)

Alim Ul Gias
Alim Ul Gias

Reputation: 6791

Just do this

processSale(getWine())

Or

processReturn(getWine())

Actually all the time you are sending a new instance of wine having nothing. So your those two methods are always having a null instance of wine

Upvotes: 1

Related Questions