0

What I want to do is initialize an array of strings and then fill each space with a different string, like so:

        int year = 1995; //the current year i'm working with

        String[] Year; //Initialize the year string
        String j;


        for(int i=(year-50); i < year; i++)
        {
            j = Integer.toString(i); //Converts the integer to a string
            Year[i] = j; //EXCEPTION OCCURS HERE WHEN I SET 'String[] Year'
                         //equal to 'null'

        }

The problem is that I can't initialize the string because I get the following error: 'The local variable Year may not have been initialized'

If I set String[] Year = null, then I can initialize the string. HOWEVER, if I do that, an exception is thrown when attempting to run & compile the code: java.lang.NullPointerException

I know that my code could be shorter, but I am trying to isolate the problem...

user2371809
  • 351
  • 2
  • 4
  • 9

5 Answers5

1

Use this code:

String[] Year = new String[50]; //Initialize the year string

Now Java knows how long you want the array to be, so it can initialize it.

If you don't tell Java how long you want your array to be, it can't make the array because it doesn't know how long the array should be.

Also, you could use a final variable SIZE if you want to adjust it easily.

Furthermore, you don't need the string j since you only use it to store the result of something before adding it to the array.

This will not affect your code, but the correct Java naming convention is that variables should start with a lowercase letter, so change Year to years.

Therefore, your code could be improved like this:

    int year = 1995; //the current year i'm working with
    final int SIZE = 50; //how big

    String[] years = new String[SIZE]; //Initialize the year string

    for(int i=(year-SIZE); i < year; i++) //Note the use of the SIZE variable
    {
        years[i] = Integer.toString(i);
    }
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • This is untested and will fail. The code is trying to write to `years[1945]` even though `years.length` is only `50`. – Alderath May 20 '13 at 13:09
1

You haven't initialized Year:

String[] years = new String[SIZE];  

or

String[] years;  
years = new String[SIZE];

or

String[] years = {"Hello","World","Again"};

Also, use correct java naming conventions.

Alessio Gaeta
  • 5,670
  • 1
  • 16
  • 26
Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
1

U didn;t Initilize Year Varible, also, there is not Items inside the Year Array

String[] Year = new String[50]; // Initlaize adn add Some content
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23
0

You need to do new on the array with the desired size

String[] Year = new String[desired size];

Without doing this, there is no array created in the memory and hence you hit the exception.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

The String[] Year; only declares the array reference with name year you need to declare the array with the required length before using it so you must make it as

String[] years = new String[size]; 

where 'size' is the length of the array you want to define. Also String[] year=null will only create a reference pointing to null that is why you get NullPointerException.

Lakshmi
  • 2,204
  • 3
  • 29
  • 49