开发者

Array returning Null values

Dunno why it is...heres the coding for it.

http://pastebin.org/301343

/**********************************\
*                                  *
*       Multiplication Quiz        *
*    Developed and Written by:     *
*          Nick Gibson             *
*             CIT                  *
*       ***Finished Code***        *
*                                  *
\**********************************/

import javax.swing.JOptionPane;
import java.awt.*;
import java.lang.Math.*;
import java.util.*;
import javax.swing.*;
import java.text.*;
import java.text.DecimalFormat.*;
import java.io.*;
import java.lang.Integer.*;



public class TestMaker
{
    public static void main(String[] args)
    {
        int y = 1, x, quesNum = 1, z, zz, qq, g, u = 1;

        while (y == 1)
        {
            String xx = JOptionPane.showInputDialog(null,"How many questions is this test? \n(May be 1 - 99 questions)");
                x = Integer.parseInt(xx);

            if(x > 99 || x < 1)
            {
                JOptionPane.showMessageDialog(null,"Invalid number. Please try again.", "Invalid Number", JOptionPane.ERROR_MESSAGE);
            }
            else
            {

                String[] questions = new String[25];
                String[] answers = new String[25];
                String[] a = new String[25];
                String[] b = new String[25];
                String[] c = new String[25];
                String[] d = new String[25];

                while(u == 1)
                {

                    questions[1] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[1] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[1] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[1] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[1] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[1] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[2] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[2] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[2] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[2] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[2] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[2] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[3] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[3] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[3] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[3] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[3] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[3] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[4] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[4] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[4] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[4] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[4] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[4] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[5] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[5] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[5] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[5] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[5] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[5] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[6] = JOptionPane.showInputDialog(null,"Please Enter Que开发者_Python百科stion Number "+quesNum);
                    a[6] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[6] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[6] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[6] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[6] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[7] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[7] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[7] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[7] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[7] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[7] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[8] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[8] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[8] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[8] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[8] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[8] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[9] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[9] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[9] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[9] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[9] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[9] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[10] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[10] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[10] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[10] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[10] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[10] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                }//while

                    zz = JOptionPane.showConfirmDialog(null,"All questions are completed. \nWould you like to review any questions?", "Question Review", JOptionPane.YES_NO_OPTION);

                    if(zz == JOptionPane.YES_OPTION)
                    {
                        while(u == 1)
                        {
                            String q = JOptionPane.showInputDialog(null,"Which question would you like to review? \n\nCurrently on Question "+quesNum+"\n\nPress 0 to exit.");
                            qq = Integer.parseInt(q);

                            if(qq == 0)
                            {
                                break;
                            }

                            JOptionPane.showMessageDialog(null,"You are now going to edit question "+qq);
                            questions[qq] = JOptionPane.showInputDialog(null,"Question: "+questions[qq]+"\nType in the correct question.");
                            a[qq] = JOptionPane.showInputDialog(null,"A: "+a[qq]+"\nType in answer A");
                            b[qq] = JOptionPane.showInputDialog(null,"B: "+b[qq]+"\nType in answer B");
                            c[qq] = JOptionPane.showInputDialog(null,"C: "+c[qq]+"\nType in answer C");
                            d[qq] = JOptionPane.showInputDialog(null,"D: "+d[qq]+"\nType in answer D");
                            answers[qq] = JOptionPane.showInputDialog(null,"Answer: "+answers[qq]+"\nPlease type the correct answer. \nType in lowercase a - d");

                            JOptionPane.showMessageDialog(null,questions[qq]+"\n\nA: "+a[qq]+"\nB: "+b[qq]+"\nC: "+c[qq]+"\nD: "+d[qq]+"\n\nCorrect Answer: "+answers[qq]);

                        }//while

                    }//if

                if(quesNum > x)
                {
                    JOptionPane.showMessageDialog(null,"That concludes the creation portion of the test. \nIt is now time to take the test.");
                }

                JOptionPane.showMessageDialog(null,"When you ready for the test, press OK.");

                double number1 = (Math.random() * 10);
                double number2 = (Math.random() * 10);
                double number3 = (Math.random() * 10);

                long aa, bb, cc, ss;
                int abc;

                aa = Math.round(number1);
                bb = Math.round(number2);
                cc = Math.round(number3);

                ss = aa;

                String str = Long.toString(ss);
                abc = Integer.parseInt(str);

                JOptionPane.showMessageDialog(null,questions[abc]+"\n\n"+a[abc]+"\n"+b[abc]+"\n"+c[abc]+"\n"+d[abc]);

            }//else

        }//while y = 1

    }//public main

}//public class

I know theres a lot of repetitiveness in the coding...but its because the arrays were retuning null so I got tired of messing with them.

Everything works until it reaches line 224, which returns null values.


1.

Nick, do you realize that array indexes in Java start at 0?

Thus:

String[] questions = new String[3];

Means that:

questions[0] = "a";
questions[1] = "b";
questions[2] = "c";

Are valid, but:

questions[3] = "d";

Is not.

2.

This would generate a number between 0 and 10 I think:

Math.round(Math.random()*10);

But I think you want only 1 - 10, which I think would be this:

Math.round((Math.random() * 9) + 1);


null is a valid value for an array. Both the pointer to the array and the contents in the array may be null. I took the time to read your too-long code. It looks like your code is not filling the arrays completely. For one thing, you do not put anything in slot 0 (questions[0] is always null).

Also, your code should be a for loop. And you should allocate the arrays to the number of questions allowed, not a fixed number. This way you will be certain that your arrays have no null elements (assuming JOptionPane doesn't return null if the user doesn't enter any value).

String[] questions = new String[x];
String[] answers = new String[x];
String[] a = new String[x];
String[] b = new String[x];
String[] c = new String[x];
String[] d = new String[x];

for (int i = 0; i < x; i++)
{

  questions[i] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+i);
  a[i] = JOptionPane.showInputDialog(null,"What is the answer for a?");
  b[i] = JOptionPane.showInputDialog(null,"What is the answer for b?");
  c[i] = JOptionPane.showInputDialog(null,"What is the answer for c?");
  d[i] = JOptionPane.showInputDialog(null,"What is the answer for d?");
  answers[i] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
}


Your arrays, a,b,c,d, questions, answers are initialized to size 25. This will mean that they all hold null values initially - 25 of them. Your code fills in indexes 1..10, and then reviews a random question.

EDIT: The root cause is how you are genering random numbers, which can be 0, and index 0 is not filled. The array elements 1..10 are filled, but 0 and 11..24 are not. If the random number comes out as 0, you'll get a NullPointerException.

The fix is to set your question numbers starting from 0, and instead of Math.round(), cast the random number to an int which truncates the frantional part, so the range is 0..9.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜