Results 1 to 9 of 9
  1. #1
    Aika is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Question 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 () ;
    }

    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    where is line 16 and 70?

  3. #3
    Aika is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    line 16 in main ---> writeFile() ;
    line 70 in writeFile method ---> file.writeInteger (array[i]) ;

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    send filename as a parameter of writeFile().

    I also think array needs to be declared as int array[] = new int[some size];

  5. #5
    Aika is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    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

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  7. #7
    Aika is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    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

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  9. #9
    Aika is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. NullPointerException - createImage()
    By Bojevnik in forum AWT / Swing
    Replies: 8
    Last Post: 06-29-2008, 10:20 PM
  2. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 09:42 AM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-04-2008, 12:19 AM
  4. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 05:20 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •