Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

NullPointerException when passing an array

Thread view: 
Rick - 07 Oct 2006 18:19 GMT
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[i] = newOrderItems[i]; // THIS LINE IS GIVING ME AN ERROR
        }
    }

    public double computeOrderTotal()
    {
        double orderTotal = 0.0;
        for (int i = 0; i < orderItems.length; i++)
        {
            orderTotal += orderItems[i].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 - 07 Oct 2006 18:53 GMT
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 - 07 Oct 2006 18:55 GMT
Rick schrieb:
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.