Java Getter方法在第一次获取之后但不是第二次获取

时间:2013-10-06 09:58:18

标签: java setter getter getter-setter

所以我正在使用封装(我是一个tafe学生)重写一些已经正在运行的代码,并且该方法正在进入第一个方法而不是第二个方法

First Methode:

public void getDetails() throws IOException
{
    do
    {
        String fullName = JOptionPane.showInputDialog(null, "Enter your full name", "Input Request", JOptionPane.QUESTION_MESSAGE);
        f = true;

        if (fullName == null)
        {
            System.out.println("Program has been aborted!");
            System.exit(0);
        } else
        {
            f = true;
        }


        Pattern numbers = Pattern.compile("[1-9,!@#$%^&*()_+=]");
        Matcher match = numbers.matcher(fullName);

        if (match.find())
        {
            JOptionPane.showMessageDialog(null, "Incorrect!\nLetters Only", "Error", JOptionPane.ERROR_MESSAGE);
            f = false;
        } else if (fullName.isEmpty())
        {
            JOptionPane.showMessageDialog(null, "pelase enter a name!", "Error", JOptionPane.ERROR_MESSAGE);
            f = false;
        } else
        {
            f = true;
            FullName fn = new FullName();
            fn.setFullName(fullName);
            System.out.println(fn.getFullName());
        }
    } while (f == false);

}

第二种方法:

 public void print()
{
    DecimalFormat df, df1;
    df = new DecimalFormat("0.00");


    FullName fn = new FullName();
    System.out.println(fn.getFullName());
    JOptionPane.showMessageDialog(null, fn.getFullName()+  "\nYour tax outcome is: " + "\nIncome Tax $" + incomeTax + "\nMedicare Levy $" + df.format(medicareLevy)
            + "\nTax Paid $" + taxWitheld + sReturn + "\nActual Tax Rate " + df.format(actualTaxRate) + "%");



}

setter和Getter类:

public class FullName
{

    private String fullName;

    public FullName()
    {
        fullName = null;
    }

    public FullName(String n1)
    {
        fullName = n1;
    }

    public String getFullName()
    {
        return fullName;
    }

    public void setFullName(String name)
    {
        this.fullName = name;
    }
}

你可能会看到任何令人惊奇的东西

1 个答案:

答案 0 :(得分:0)

我认为你的问题出现在第二种方法中,你是“新”你的全名类,但你从来没有setFullName(“blah blah”);所以fn.getFullName()返回null。