Error problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Error problem

  1. #1
    Join Date
    Feb 2008
    Posts
    14

    Error problem

    Hi Guys,

    I am having this error:

    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    and it says that it is in the line:

    Code:
     int[] A  = new int[questions.length];
    This is the whole code. This is used to generate a set of questions randomly.

    Code:
     public void askQuestions(String[] questions, String[] answers) {
          
            int count = 0;
            int point = 0;
            // Create the arrays
            int[] A  = new int[questions.length];
            int[] B = new int[A.length];
            // Create the counters
            int countA = A.length;
            int countB = 0;
            //  adding the indexes to A
           for (int i = 0; i < A.length; i++){
             A[i] = i;
           }
    
          while (countA > 0){
          Random generator = new Random();
          int pos = generator.nextInt(countA);
          // Adding the index of A at pos to B
          B[countB] = A[pos];
          countB++;
         // Removing the index of A at pos from A
          for (int i = pos; i < A.length - 1; i++)
            {
              A[i] = A[i + 1];
            }
              countA--;
         }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    sounds as if questions.length is not initialized with a value. Try testing for a value in your code - a debug statement before the creation of your A array to output the length of the questions array.

    you do a lot of checking of the length of the array and use that number in many places - why not create a counter variable, initialize it once with the length of your string array, and then do all your A.length stuff using the counter value, rather than accessing the array again to get its length?

  3. #3
    Join Date
    Feb 2008
    Posts
    14
    Yeah ok. Thanks a lot.

  4. #4
    Join Date
    May 2007
    Posts
    843
    Do u define it using #define statement or static ?

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center