0

I have task to do and I have problem with one point and I'm getting error. I havve no idea how to repair it:

Exception in thread "main" java.lang.NullPointerException at test.main(test.java:29)

Here's my code:

import java.util.Scanner;


public class test
{

    static int size;
    static String[] productDescription;     

    public static void readProductsData()
    {
        Scanner data = new Scanner(System.in);
        System.out.print("Array size: ");                   
        size = data.nextInt();

        String[] productDescription = new String[size];

        for(int i = 0; i < size; i++)
        {
            System.out.print("Product name: ");
            productDescription[i] = data.next();
        }

        for(int i = 0; i < size; i++)
            System.out.println(productDescription[i]);
    }

    public static void main(String[] args)
    {
        readProductsData();
        System.out.println(productDescription[0]);
    }
}

1 Answer 1

1

You are shadowing productDescription. Remove String[] before productDescription in readProductsData.

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

1 Comment

Oh well, didn't noticed. Thanks a lot.

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.