0

So i am re writing some already working code using encapsulation(Im a tafe student) and the method is getting in the first methode but not in the second

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);

}

Second Method:

 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) + "%");



}

The setter and Getter Class:

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;
    }
}

Any thing you might see would be amazing

Callum
  • 1
  • Is the intent to show the name (in `print()`) that was given in `getDetails()`? – kiheru Oct 06 '13 at 10:02
  • it is just my testing method i wanted to get the name from get details and display it at the end, i have 4 other methods that i havent changed over to getter setter methods yet – Callum Oct 06 '13 at 10:16
  • 1
    It's difficult to help when I don't know what exactly you're trying to achieve. In `print()` `fn.getFullName()` returns `null` because `setFullName()` has not been called *for that FullName instance*. You'll probably want to make `getDetails()` return `FullName`, otherwise the instance there is local to that method. – kiheru Oct 06 '13 at 10:22
  • This is the full Main Method – Callum Oct 06 '13 at 10:25
  • http://pastebin.com/YVHE0Pn2 – Callum Oct 06 '13 at 10:33
  • @Callum : Your `print()` method actually creates a new instance of `FullName`, it is not the one you set in the `getDetails()` method. You should store that reference somewhere so that you can use it in the `print()` method. – Shashank Kadne Oct 06 '13 at 10:39
  • If I understand your intent correctly, you can make the full name a field of the class, like `annualIncome` etc are. Then create an instance of it *once* (right where it's declared is a good place), and remove the `new FullName()` calls from both `getDetails()` and `print()`. Then both methods will access the same data. – kiheru Oct 06 '13 at 10:43
  • The String default value is null, so in the second one when you do not set the name with constructor, so the name is still null, and I like the others didn't understand the problem! –  Oct 06 '13 at 11:23
  • @Shashank Kadne how should i store it, should i store it in the setter getter class or in the main class? – Callum Oct 06 '13 at 12:04

1 Answers1

0

i think your problem is in the second method you "new" your fullname class, but you never setFullName("blah blah"); so fn.getFullName() returns null.

Ali Riahipour
  • 524
  • 6
  • 20