1

newbie here. So i'm testing out arrays, it's the next topic in the tutorial that i have been reading. I made a simple program to specify how many numbers to get or to input. The problem is on the getArray() method, i can't seem to display it. It's giving me an exception in thread error. Any help and advice is much appreciated. :) Sorry for the newbie question. :)) So here's the code.

import java.util.Scanner;

public class ArrayNumbers {

Scanner input = new Scanner(System.in);
int totalNum, counter, count = 0, display;
double arrayNum[];

public void setArrayNum() {
    System.out.print("How many numbers?: ");
    totalNum = input.nextInt();
    double arrayNum[] = new double[totalNum];

    for (counter = 0 ; counter < arrayNum.length ; counter++) {
        System.out.print("Num "+(count = counter + 1)+": ");
        arrayNum[counter] = input.nextInt(); 
    }
}

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();

    System.out.print(arrayNum[display]);
}
}

import java.util.Scanner;

public class MainProgram {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Numbers NumbersObject = new Numbers();
    ArrayNumbers ArrayNumbersObject = new ArrayNumbers();
    String name;
    int choice;

    System.out.print("Enter name: ");
    name = input.nextLine();
    System.out.println("Menu");
    System.out.println("1. Math Operations");
    System.out.println("2. Grade Computation");
    System.out.println("3. Counting Numbers");
    System.out.println("4. Array Numbers");
    System.out.print("Enter choice: ");
    choice = input.nextInt();

    switch(choice) {

    case 1:
        System.out.println("Choose Operation");
        System.out.println("1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Multiplication");
        System.out.println("4. Division");
        System.out.print("Enter choice: ");
        choice  = input.nextInt();

        NumbersObject.setNum();

        if (choice == 1)
            System.out.println("The answers is "+(NumbersObject.getNum1() + NumbersObject.getNum2()));
        else if (choice == 2)
            System.out.println("The answers is "+(NumbersObject.getNum1() - NumbersObject.getNum2()));
        else if (choice == 3)
            System.out.println("The answers is "+(NumbersObject.getNum1() * NumbersObject.getNum2()));
        else if (choice == 4)
            System.out.println("The answers is "+(NumbersObject.getNum1() / NumbersObject.getNum2()));
        else
            System.out.print("Invalid Input!");
        break;

    case 2:
        NumbersObject.setNum();
        System.out.println("Your average grade is "+((NumbersObject.getNum2() + NumbersObject.getNum2()) / 2));
        break;

    case 3:
        System.out.println("Welcome to Counting Numbers!");
        System.out.println("Enter 2 numbers to start and end!");
        NumbersObject.setNum();

        for (int counter = (int) NumbersObject.getNum1() ; counter <= NumbersObject.getNum2() ; counter++) {
            System.out.println(counter);
        }

        System.out.println("End!");
        break;

    case 4:
        ArrayNumbersObject.setArrayNum();
        ArrayNumbersObject.getArray();
        break;

    default:
        System.out.println("Mr/Ms "+name+" you entered an Invalid Choice!");
        break;
    }//end of switch    
}// end of main
}// end of class

Sorry about that, for not including the main.

And here's the error:

Exception in thread "main" java.lang.NullPointerException
at ArrayNumbers.getArray(ArrayNumbers.java:23)
at MainProgram.main(MainProgram.java:67)

Guys, the only i problem i have is in the getArray() It asks for what number in the array i want to see but when i input it, it gives an exception in the thread error. The only problem is how am i going to display the array[number i specified]? Sorry for the newbie questions.

3
  • Please post the stack trace of your error in your question. Commented Nov 4, 2013 at 11:15
  • 1
    Where is your main? Commented Nov 4, 2013 at 11:15
  • could you provide your main method as well? Commented Nov 4, 2013 at 11:16

3 Answers 3

2

It throws an exception because you create an array of size display and you try to print the element at the display position but arrays are 0 base indexed in Java.

This figure should be useful :

enter image description here

So imagine that display = 10 :

double arrayNum[] = new double[display]; //create an array that can holds 10 elements
System.out.print(arrayNum[display]); //try to access at the 10th element of the array but it doesn't exists !

You don't have to create a new array because you want to display the number in the arrayNum you created in your class.

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();
    System.out.print(arrayNum[display]);
}

Also you need to check if display is in the correct bounds range [0,...,displayNum.length-1] before trying to access its elements.

Edit :

In setArrayNum() replace double arrayNum[] = new double[totalNum]; by arrayNum = new double[totalNum];

Sign up to request clarification or add additional context in comments.

3 Comments

@RvDeAsis Check my edit, you already created arrayNum[] at the class level.
Got it! Thanks for the help. A little correction: When i removed the double from this arrayNum[] = new double[totalNum]; This still gives me an error. But when i removed the [] from above, arrayNum = new double[totalNum]; Problem solved! :)
@RvDeAsis WOOPS yeah sorry =)
0

Arrays are 0-based. So if you create an array of size display you can access indexes 0..display-1

Comments

0

You are creating local array double arrayNum[] = new double[display]; within getArray() method.

You need to remove double arrayNum[] = new double[display]; from getArray() method.

After that your method like below.

public void getArray() {
    System.out.print("What number to display?: ");
    display = input.nextInt();
    if(display >0 && display <=arrayNum.length){
            System.out.print(arrayNum[display-1]);
    }else{
         System.out.print(display + " is out of range");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.