NullPointerException when passing an array

Rick (37)

Usenet Archive
Joined
Oct 7, 2006
Messages
3
Reaction score
0
I get a NullPointerException error when trying to run my program. I think I am doing something wrong when copying the array, but am not seeing what. Here is what I have and I marked where the error is occuring.

/* The Client class ...
*/

public class Client
{

public static void main(String args)
{

double order1Total, order2Total = 0.0;

// Create objects

// Products
Product p1 = new Product(1, "Computer", 350.00);
Product p2 = new Product(2, "Printer", 40.00);
Product p3 = new Product(3, "Scanner", 45.50);
Product p4 = new Product(4, "Digital Camera", 150.99);

// Line Items
// structure (item number, quantity, product)
LineItem li1 = new LineItem(1, 1, p1);
LineItem li2 = new LineItem(2, 2, p3);
LineItem li3 = new LineItem(3, 1, p1);
LineItem li4 = new LineItem(4, 3, p3);
LineItem li5 = new LineItem(5, 2, p4);

// Orders
LineItem order1Items = {li1, li2};
LineItem order2Items = {li3, li4, li5};
java.util.Date date1 = new java.util.Date;
java.util.Date date2 = new java.util.Date;
Order o1 = new Order(1, date1, order1Items); // THIS LINE IS GIVING ME AN ERROR Order o2 = new Order(2, date2, order2Items);

order1Total = o1.computeOrderTotal;
System.out.println(order1Total);
}

}

/* The Order class ...
*/

public class Order
{
private int orderNumber;
private java.util.Date orderDate = new java.util.Date; private LineItem orderItems;

// Default contructor
Order
{
}

// Constructor with parameters
Order(int newOrderNumber, java.util.Date newOrderDate, LineItem newOrderItems) { orderNumber = newOrderNumber; orderDate = newOrderDate; for (int i = 0; i < newOrderItems.length; i++) { orderItems = newOrderItems; // THIS LINE IS GIVING ME AN ERROR } }

public double computeOrderTotal
{
double orderTotal = 0.0;
for (int i = 0; i < orderItems.length; i++)
{
orderTotal += orderItems.computeLineItemTotal;
}

return orderTotal;
}

}

/* The LineItem class ...
*/

public class LineItem
{
private int itemNumber;
private int quantity;
private Product lineProduct;

// Default contructor
LineItem
{
}

// Constructor with parameters
LineItem(int newItemNumber, int newQuantity, Product newLineProduct) { itemNumber = newItemNumber; quantity = newQuantity; lineProduct = newLineProduct; }

public double computeLineItemTotal
{
double lineTotal = 0.0;
lineTotal = quantity * lineProduct.getProductPrice;
return lineTotal;
}

}
 

Jean-Francois Briere

Usenet Archive
Joined
Sep 23, 2004
Messages
104
Reaction score
0
In the Order constructor you forgot to instantiate the orderItems field before using it:

Order(...)
{
...
// instantiate orderItems
orderItems = new LineItem[newOrderItems.length]; for (...) ...
}

Regards
 

Michael Rauscher

Usenet Archive
Joined
Jan 28, 2004
Messages
280
Reaction score
0
Rick said:
I get a NullPointerException error when trying to run my program. I think I am doing something wrong when copying the array, but am not seeing what. Here is what I have and I marked where the error is occuring.

The problem is that orderItems == null (in class Order). Allocate some memory for it, e. g.

orderItems = new LineItems[newOrderItems.length];

Bye
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top