|
-
Dec 13th, 2005, 01:48 AM
#1
Thread Starter
Hyperactive Member
Error Correction problem
This is kinda hard to explain. I have a class that looks like this:
Code:
import java.text.*;
public class BookOrder{
private Book book;
private int quantity;
private double total;
private static int objectCount = 0;
public BookOrder(String bookCode, int bookQuantity){
book = new Book(bookCode);
quantity = bookQuantity;
setTotal();
objectCount++;
}
public static int getObjectCount(){
return objectCount;
}
public void setTotal(){
total = quantity * book.getPrice();
}
public Book getBook(){
return book;
}
public int getQuantity(){
return quantity;
}
public double getTotal(){
return total;
}
public String toString(){
NumberFormat currency = NumberFormat.getCurrencyInstance();
String orderString = "Code: " + book.getCode() + "\n"
+ "Title: " + book.getTitle() + "\n"
+ "Price: " + currency.format(book.getPrice()) + "\n"
+ "Quantity: " + quantity + "\n"
+ "Total: " + currency.format(total) + "\n";
return orderString;
}
}
The question I have is about the static variable and the static method. I have a driver class that instanciates this class and when the user exits a loop manually, it calls the static method to display how many instances of the object were created. Here is the code in question in the driver class:
Code:
public static void main(String[] args){
String choice = "";
try{
while (!(choice.equalsIgnoreCase("x"))){
String title = JOptionPane.showInputDialog(
"Enter a book code:");
String inputQuantity = JOptionPane.showInputDialog(
"Enter a quantity:");
int quantity = parseQuantity(inputQuantity);
BookOrder bookOrder = new BookOrder(title, quantity);
String message = bookOrder.toString() + "\n"
+ "Press Enter to continue or enter 'x' to exit:";
choice = JOptionPane.showInputDialog(null,
message, "Book Order", JOptionPane.PLAIN_MESSAGE);
}//end while
}
catch(NullPointerException e){
System.exit(0);
}
System.exit(0);
System.out.println("Objects created: " + bookOrder.getObjectCount());
}
It returns an error in the last line of the driver class saying that it can't resolve the bookOrder variable. But I already have an instance of the BookOrder class created with the same variable so I should be able to use it right?
-
Dec 13th, 2005, 03:03 AM
#2
Re: Error Correction problem
the first thing I have noticed is that they are not in the same code block. {}
perhaps an error occured before bookOrder was created/instantiated, and gets out of that block and so the last line will have no object to point to.
edit:
you instantiate a new bookOrder object for each case in your loop?
definitely the bug is there.
-
Dec 13th, 2005, 03:06 AM
#3
Re: Error Correction problem
you could take out the definitition(not instantiation) of bookOrder out of the block just right after the definition of choice.
-
Dec 13th, 2005, 12:34 PM
#4
Thread Starter
Hyperactive Member
Re: Error Correction problem
the first thing I have noticed is that they are not in the same code block. {}
The driver class and the BookOrder class aren't in the same file.
you instantiate a new bookOrder object for each case in your loop?
definitely the bug is there.
That part of code is fine because when I remove the line of code that outputs the number of instances created the program works fine.
I tried moving the statement out of the while loop and it didn't work then but I understand why and that's because the variables that I'm passing to the class aren't created yet. However that rasies another point because it generates the same error when the statement is in the while block when the variables are created. (I'm talking about the statement that outputs the number of instances created) Does this mean that the instance of the BookOrder is terminated after the while block ends?
Edit:
The instance must end up being destroyed at the end of the while loop because I placed a bit of code into section of the while loop to output the number of instances created and it worked there but anytime I place the statement outside the loop it is not working.
Last edited by GamerMax5; Dec 13th, 2005 at 12:50 PM.
-
Dec 13th, 2005, 07:21 PM
#5
Re: Error Correction problem
 Originally Posted by GamerMax5
The driver class and the BookOrder class aren't in the same file.
That part of code is fine because when I remove the line of code that outputs the number of instances created the program works fine.
I tried moving the statement out of the while loop and it didn't work then but I understand why and that's because the variables that I'm passing to the class aren't created yet. However that rasies another point because it generates the same error when the statement is in the while block when the variables are created. (I'm talking about the statement that outputs the number of instances created) Does this mean that the instance of the BookOrder is terminated after the while block ends?
Edit:
The instance must end up being destroyed at the end of the while loop because I placed a bit of code into section of the while loop to output the number of instances created and it worked there but anytime I place the statement outside the loop it is not working.
It might be the case that since in everyloop a new instance is created, thus the other created objects( the iteration before that) will have no more reference to it java automatically garbage collects it.
-
Dec 13th, 2005, 07:24 PM
#6
Re: Error Correction problem
Have you tried this?
Notice in the end of the line i used the Class name to access the object instead of the variable name.
Code:
public static void main(String[] args){
String choice = "";
BookOrder bookOrder;
try{
while (!(choice.equalsIgnoreCase("x"))){
String title = JOptionPane.showInputDialog(
"Enter a book code:");
String inputQuantity = JOptionPane.showInputDialog(
"Enter a quantity:");
int quantity = parseQuantity(inputQuantity);
bookOrder = new BookOrder(title, quantity);
String message = bookOrder.toString() + "\n"
+ "Press Enter to continue or enter 'x' to exit:";
choice = JOptionPane.showInputDialog(null,
message, "Book Order", JOptionPane.PLAIN_MESSAGE);
}//end while
}
catch(NullPointerException e){
System.exit(0);
}
System.exit(0);
System.out.println("Objects created: " + BookOrder.getObjectCount());
}
Last edited by oceanebelle; Dec 13th, 2005 at 07:28 PM.
-
Dec 13th, 2005, 08:09 PM
#7
Thread Starter
Hyperactive Member
Re: Error Correction problem
Well that didn't exactly solve the problem but it continued without crashing! lol
I got the program to display the number of objects created from within the loop but any time I want to try and show it outside the loop it never works. Here is the modified driver class:
Code:
public static void main(String[] args){
String choice = "";
try{
while (!(choice.equalsIgnoreCase("x"))){
String title = JOptionPane.showInputDialog(
"Enter a book code:");
String inputQuantity = JOptionPane.showInputDialog(
"Enter a quantity:");
int quantity = parseQuantity(inputQuantity);
BookOrder bookOrder = new BookOrder(title, quantity);
String message = bookOrder.toString() + "\n"
+ "Instances created: " + bookOrder.getObjectCount() + "\n"
+ "Press Enter to continue or enter 'x' to exit:";
choice = JOptionPane.showInputDialog(null,
message, "Book Order", JOptionPane.PLAIN_MESSAGE);
}//end while
}
catch(NullPointerException e){
System.exit(0);
}
System.exit(0);
System.out.println("Objects created: " + BookOrder.getObjectCount());
}
That last line doesn't work but it don't cause the program to crash either! lol It's a start. I just don't understand why it doesn't work outside the loop... And btw thx for all your help.
You'll have to pardon the odd placement of the code as I did this in Visual Studio.NET.
-
Dec 17th, 2005, 07:44 AM
#8
Lively Member
Re: Error Correction problem
Try this:
Code:
public static void main(String[] args){
String choice = "";
BookOrder bookOrder = null;
try{
while (!(choice.equalsIgnoreCase("x"))){
String title = JOptionPane.showInputDialog("Enter a book code:");
String inputQuantity = JOptionPane.showInputDialog("Enter a quantity:");
int quantity = Integer.parseInt(inputQuantity);
bookOrder = new BookOrder(title, quantity);
String message = bookOrder.toString() + "\n"
+ "Instances created: " + bookOrder.getObjectCount() + "\n"
+ "Press Enter to continue or enter 'x' to exit:";
choice = JOptionPane.showInputDialog(null,
message, "Book Order", JOptionPane.PLAIN_MESSAGE);
}//end while
} catch(NullPointerException e){
System.exit(0);
}
System.out.println("Objects created: " + bookOrder.getObjectCount());
System.exit(0);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|