I have two files, an application file and a class file. While they both run without errors, the application file returns a null when I run it.
Here are the two parts that dont seem to work?
Can someone tell me waht I am doing wrong?

App file code:

Invoice invoice = new Invoice(subtotal, customerType);
System.out.println();
System.out.println(invoice.getInvoice());

Class file code:

// constructors
public Invoice(double subtotal,String customerType)
{ this.subtotal=subtotal; this.customerType=customerType; }


//      set and get statements for Invoice
public void  setInvoice(double subtotal, String customerType)
{this.subtotal=subtotal; this.customerType=customerType; }


public String  getInvoice()
{return Invoice; }

What do you want getInvoice() to return? Are you sure you have posted all the code of the Invoice class?
It seems to me that the Invoice variable inside getInvoice() doesn't have a value, but I will need the rest of the code to be sure

What do you want getInvoice() to return? Are you sure you have posted all the code of the Invoice class?
It seems to me that the Invoice variable inside getInvoice() doesn't have a value, but I will need the rest of the code to be sure

// set and get statements for Invoice
public void setInvoice(double subtotal, String customerType)
{this.subtotal=subtotal; this.customerType=customerType; }

public String getInvoice()
{return Invoice; }

I assumed that when I set the setInvoice code, that the getInvoice statement returns the values in that format, but if I try to use "public String getInvoice((double subtotal, String customerType)" I get an type error. My problem is the calling file needs a double value and a string value returned. If you need all the code from both files let me know as they are rather long?
Thanks,
Ray

Do you declare Invoice somewhere in the Invoice class?

public String getInvoice()
{return Invoice; }

I assumed that when I set the setInvoice code, that the getInvoice statement returns the values in that format

No, the getInvoice will return the value of Invoice. If you don't give it value it will return null.

if I try to use "public String getInvoice((double subtotal, String customerType)" I get an type error.

If you declare getInvoice like this:

public String getInvoice(double subtotal, String customerType) {
  return "someString";
}

Then you call it like this:

Invoice inv = new Invoice();
String s = inv.getInvoice(5.00, "type 1");

You still haven't told me what you want the getInvoice to return.

Post the Invoice code only.

Here is the whole Invoice Class code
Hope you can find my error(s)

import java.text.NumberFormat;


public class Invoice
{


//instance variables
private double discountPercent;
private double discountAmount;
private double total;
private String customerType;
private double subtotal;
private String Invoice;



// constructors


public Invoice(double subtotal,String customerType)


{
this.subtotal=subtotal;
this.customerType=customerType;
}



//      set and get statements for Invoice
public void  setInvoice(double subtotal, String customerType)
{


this.subtotal=subtotal;
this.customerType=customerType;



}


public String getInvoice()
{
return Invoice;


}



//    set and get statements for subtotal


public void setsubtotal (double subtotal)
{
this.subtotal=subtotal;
}


public double getsubtotal()
{
return subtotal;
}


// set and get statements for customerType


public void setcustomerType (String customerType)
{
this.customerType =customerType;
}
public String getcustomerType()
{
return customerType;
}



// set and get statements for discountPercent


public void setdiscountPercent (double discountPercent)
{
this.discountPercent =discountPercent;
}
public double getdiscountPercent()
{
return discountPercent;
}
// set and get statements for discountAmount


public void setdiscountAmount (double discountAmount)
{
this.discountAmount =discountAmount;
}
public double getdiscountAmount()
{
return discountAmount;
}


// set and get statements for total


public void settotal (double total)
{
this.total =total;
}
public double gettotal()
{
return total;
}


public String getFormattedsubtotal()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(subtotal);
}


}

Where in your program do you give value to the String variable Invoice? NOWHERE.
You dont' do this: Invoice = "someString"; so it returns null.
You have to give it value.
What do you want it to return? It is 3rd time I ask this.
Instead of this:

public String getInvoice()
{
return Invoice;
}

Remove the Invoice variable and calculate in the method the result and return it:


And MOST IMPORTANT: Don't declare variables with the same name as your class. It is bad programming. Variables should have the first letter lowerCase.

Hi guys :)
There are 2 errors in the get invoice code

public String getInvoice(){return Invoice;}

At current code you're naming private instance property with the Class name. So jvm won't know what are you trying to return.
Among that this breaks accessors naming convention, since this getter implies a property named invoice not Invoice.
My best advice is to change the name of private String Invoice to private String invoice. and change the getter code to return this.invoice instead of Invoice.

By the way... Are you trying to return either this class instance or this class instance String value to the application when you invokes getInvoice() method?
If so, the way you are trying to do it is widely wrong.
In no one of both cases you needs of a getter method, since the application haves the Invoice instance already ( inv is in fact the Invoice instance) and in the 2nd case you only needs to do inv.toString() at application code.
Best
Gustavo

I amtrying to return the values that are entered in the InvoiceApp file.
The user enters the customer value and subtotal vaule there.

Here's the InvoiceApp code

import java.text.NumberFormat;
import java.util.Scanner;


public class InvoiceApp


{
public static void main(String[] args)


{
// display a welcome message
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println();  // print a blank line


Scanner sc = new Scanner(System.in);
String choice = "y";
while(choice.equalsIgnoreCase("y"))
{
// get user entries
String customerType = Validator.getString(sc,
"Enter customer type (r/c):   ");
double subtotal = Validator.getDouble(sc,
"Enter subtotal:              ", 0, 10000);


Invoice invoice = new Invoice(subtotal, customerType);



System.out.println();
System.out.println(invoice.getInvoice());
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}

I amtrying to return the values that are entered in the InvoiceApp file.
The user enters the customer value and subtotal vaule there.

Here's the InvoiceApp code
import java.text.NumberFormat;
import java.util.Scanner;

public class InvoiceApp

{
public static void main(String[] args)

{
// display a welcome message
System.out.println("Welcome to the Invoice Total Calculator");
System.out.println(); // print a blank line

Scanner sc = new Scanner(System.in);
String choice = "y";
while(choice.equalsIgnoreCase("y"))
{
// get user entries
String customerType = Validator.getString(sc,
"Enter customer type (r/c): ");
double subtotal = Validator.getDouble(sc,
"Enter subtotal: ", 0, 10000);

Invoice invoice = new Invoice(subtotal, customerType);


System.out.println();
System.out.println(invoice.getInvoice());
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}

The only thing you needs to do is this:

//            System.out.println(invoice.getInvoice());
            System.out.println(invoice.getCustomertype()+" : " +invoice.getSubtotal());

remove private String Invoice; and setters and getters methods related to this property and your code will work well.
As i've said before, that piece of code is useless as it is... but if your intention is encapsulate the output string inside a method create a new one named like printFormatedInvoice() at your Invoice class file with something like the following code:

//remove public String Invoice;
.....
//replace getInvoice() method by this one.

public String printFormatedInvoice(){ 
      //perhaps you'd like to format this.subtotal using DecimalFormat class or other NumberFormat derivative of your preference
       String out = this.customerType+" " +this.subtotal;
       return out;
}

Hope this helps.
Best
Gustavo

Thanks for everyones help, I have resolved the issue with your suggestions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.