# NullPointerException

• 11-18-2008, 10:51 PM
Aika
NullPointerException
I am a java beginner and while trying to run a program to write integer n and n random numbers to a specified file the following exception occurs:

at Q4_4.writeFile(Q4_4.java:70)
at Q4_4.main(Q4_4.java:16)

As far as I understand this exception says that the slot in an integer array is null. However, it is filled with the random numbers. Can you please advise why this happens? :confused:

Thank you very much in advance!

Here is the code:

import java.util.Random ;

public class Q4_4
{
public static int n ;
public static int array[] ;
public static String filename ;

public static void main(final String [] args)
{
getInput() ;
random(n) ;
writeFile() ;
}

// Gets an integer n and a file name.
public static void getInput()
{
KeyboardInput in = new KeyboardInput () ;

System.out.print("Enter a positive integer n: ") ;

System.out.print("Enter file name: ") ;
}

// Creates an array with integer values of n and n random integers between 1 and 10 inclusive.
public static int random (int n)
{
Random generator = new Random () ;

int array[] = new int [n+1] ;
array [0] = n ;
for (int i = 1 ; i <= n ; i++ )
{
array [i] = generator.nextInt(10) + 1 ;
}
return array[n];
}

// Opens and writes the file.
public static void writeFile()
{
FileOutput file = new FileOutput (filename) ;

for (int i = 0 ; i < n+1 ; i++)
{
file.writeInteger (array[i]) ;
file.writeNewline() ;
}
file.close () ;
}

}
• 11-18-2008, 11:00 PM
xcallmejudasx
where is line 16 and 70?
• 11-18-2008, 11:22 PM
Aika
line 16 in main ---> writeFile() ;
line 70 in writeFile method ---> file.writeInteger (array[i]) ;
• 11-18-2008, 11:30 PM
xcallmejudasx
send filename as a parameter of writeFile().

I also think array needs to be declared as int array[] = new int[some size];
• 11-18-2008, 11:40 PM
Aika
I've done the first suggestion, and the second one was there already - but still the same exception...
for some reason command: file.writeInteger (array[i]) ; considers that an array is empty
• 11-18-2008, 11:43 PM
xcallmejudasx
comment out that line and in front of it do a loop to display each element of the array to make sure that there is actually something inside of it.
• 11-19-2008, 12:00 AM
Aika
yes, the array is actually empty when I check it in main method. However, it was filled with random integers in random method. Is there any reason why the same array cannot be used in main? I assumed that making this array of type public static will allow me access it globally. Am I right?
Thank u
• 11-19-2008, 12:17 AM
xcallmejudasx
you defined it globally so yes it should be able to be accessed within anywhere in the program.

Oh change
int array[] = new int [n+1] ;
to use array = new int[n+1];
This sets your current array to size n+1 instead of creating a new array with the same name.
your creating a new int[] with the same name, i'm surprised it didn't get you an error for that.
• 11-19-2008, 12:34 AM
Aika
oh! thank u, xcallmejudasx! it works!