0

I would like to create an instance of a class, that has an array of class members within it where the array is defined in length upon initialization. The code I have written does not contain any errors precompile, but after running returns nullPointerException. I want to be able to access products of class storeA by typing storeA.products[productnumber].(product variable), is this possible?

package tinc2;

public class FirstProgram {

    public static void main(String[] args) {
        store storeA = new store();
        storeA.name = "Walmart";
        storeA.products = new store.product[3];
        storeA.products[0].name = "Horses";
        System.out.println(storeA.products[0].name);
    }

    public static class store{
        String name;
        product products[];
        static class product{
            String name;
            int quantity;
            double price;
        }
    }

}

2 Answers 2

1

Go for

public static void main(String[] args) {
    store storeA = new store();
    storeA.name = "Walmart";
    storeA.products = new store.product[3];
    storeA.products[0] = new store.product();
    storeA.products[0].name = "Horses";
    System.out.println(storeA.products[0].name);
}

instead.

Besides you should place those classes in separate files. You should follow naming conventions in Java, e.g. Store instead of store. You should use getters and setters.

I would avoid statics, if it is possible.

Sign up to request clarification or add additional context in comments.

3 Comments

that's the first syntax I tried, nullPointerException.
No, it's modified ;)
it appears that each individual product as a piece of the array needs to be declared individually to be used, and that is what I was missing. I added these two line to your code and now it works exactly how it needs to, thank you. for(int i=0;i<storeA.products.length;i++) storeA.products[i] = new store.product();
0

You should not be instantiating a static class. Your Product class should not be defined as static. I recommend:

package tinc2;

public class FirstProgram {

    public static void main(String[] args) {
        Store.name = "Walmart";
        Store.products = new Product[1];
        Store.products[0] = new Product();
        Store.products[0].name = "Horses";
        System.out.println(Store.products[0].name);
    }

    public static class Store{
        String name;
        Product products[];
    }

    public class Product{
        String name;
        int quantity;
        double price;
    }
}

5 Comments

That would imply that I only have one store.
This is just a sample. You can size the array any way that you want. Alternatively, you could use a data structure with a flexible size like a linked list.
I am trying to make it so the class Store is more of a data type, that contains an array of products that can be initialized whenever, however, and numerously.
Indeed. Do you think that the Product class definition has to be an internal class of Store for that to work? It does not. I would recommend that you define Product separately.
You suggest without reasoning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.