Results 1 to 9 of 9
Thread: NullPointerException
- 11-18-2008, 10:51 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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:
Exception in thread "main" java.lang.NullPointerException
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: ") ;
n = in.readInteger() ;
System.out.print("Enter file name: ") ;
filename = in.readString () ;
}
// 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 #2
where is line 16 and 70?
- 11-18-2008, 11:22 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
line 16 in main ---> writeFile() ;
line 70 in writeFile method ---> file.writeInteger (array[i]) ;
- 11-18-2008, 11:30 PM #4
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 #5
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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 #6
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 #7
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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 #8
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 #9
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
NullPointerException - createImage()
By Bojevnik in forum AWT / SwingReplies: 8Last Post: 06-29-2008, 10:20 PM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 09:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-04-2008, 12:19 AM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 05:20 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 08:51 PM
Bookmarks