I am new to Java , can anyone explain why i am getting a Null pointer Exception ?
Additionally can anyone explain other reliable input methods besides Scanner.
The Error
Exception in thread "main" java.lang.NullPointerException
at Theater.main(Theater.java:18)
import java.util.Scanner;
public class Theater
{
public static void main(String[] args) throws Exception
{
int Screen;
Screens[] X = new Screens[5];
Scanner input = new Scanner(System.in);
do
{
System.out.println();
System.out.println(" '0' to exit");
System.out.println(" '1-5' for Booking Seats");
System.out.println(" '10' for Displaying Seating Status");
System.out.println("Enter Screen Number : ");
Screen = input.nextInt();
if(Screen >= 1 && Screen <= 4)
X[Screen-1].bookSeat();
else if(Screen == 0)
{
System.out.println("Thank You for Booking Seats in PVR Cinemas.");
System.in.read();
System.exit(0);
}
}while(true);
}
}
class Screens
{
private
int[] Gold = new int[3];
int[] Platinum = new int[3];
int[] Diamond = new int[3];
int g,d,p;
Scanner input = new Scanner(System.in);
final int MAX=3;
public Screens()
{
for( int i=0;i<3;i++)
{
Gold[i] = 0;
Platinum[i] = 0;
Diamond[i] = 0;
g = d = p = 0;
}
}
public void bookSeat()
{
int n=0,choice,i;
System.out.println("\t\tMenu");
System.out.println("1.Gold \tAvailable Seats : "+(3-g));
System.out.println("2.Platinum \tAvailable Seats : "+(3-p));
System.out.println("3.Diamond \tAvailable Seats : "+(3-d));
System.out.println("4.Return to Main Menu");
System.out.println("Your Choice : ");
choice = input.nextInt();
if(choice>=1 && choice<=3)
{
System.out.print("How many Seats ? : ");
n = input.nextInt();
if( n<=0 )
{
System.out.println("Please Check your Input.");
return;
}
else if( n>=MAX )
{
System.out.println("The Maximum Number of Seats is : "+MAX);
}
}
switch(choice)
{
case 1:
if(g+n >3)
{
System.out.println("Housefull!");
break;
}
else
{
int total = 0;
System.out.print("Seat Numbers are : ");
for(i=0;i<n;i++)
{
Gold[g++] = 1;
System.out.print("\t"+g);
}
total = 100 * n;
System.out.println("Total Money to be paid : "+total);
}
break;
case 2:
if(p+n >3)
{
System.out.println("Housefull!");
break;
}
else
{
int total = 0;
System.out.print("Seat Numbers are : ");
for(i=0;i<n;i++)
{
Platinum[p++] = 1;
System.out.print("\t"+p);
}
total = 125 * n;
System.out.println("Total Money to be paid : "+total);
}
break;
case 3:
if(d+n >3)
{
System.out.println("Housefull!");
break;
}
else
{
int total = 0;
System.out.print("Seat Numbers are : ");
for(i=0;i<n;i++)
{
Diamond[d++] = 1;
System.out.print("\t"+d);
}
total = 150 * n;
System.out.println("Total Money to be paid : "+total);
}
break;
case 4:
break;
default:
System.out.println("Sorry, That's an invalid Choice!");
}
return;
}
public void viewSeats()
{
int i;
System.out.println("Gold Category : ");
for(i=0;i<3;i++)
System.out.print("\t "+Gold[i]);
System.out.println("Platinum Category : ");
for(i=0;i<3;i++)
System.out.print("\t "+Platinum[i]);
System.out.println("Diamond Category : ");
for(i=0;i<3;i++)
System.out.print("\t "+Diamond[i]);
}
}
You need to populate your Scrrens array. In other words you have only initialized your Scrren array but never initialized its elements.If you don't initialize its elements they get default values which in this case is null. Guess what happens when you invoke somemethod on null. Boom NPE you get.
Screens[] X = new Screens[5];
x[0] = new Screen();
Although you initialize the array X you do not initailize its members so when doing X[Screen-1] you are using a null object (even if the index is in the bounderies).
Because your array X doesn't have any elements in it, so by default every element is initialized to null. So basically you are trying to do this null.bookseat() which results in a NullPointerException.
It is also important to note that if Screen is ever 5 (which is allowed by your condition <= 5) then you will get an ArrayIndexOutOfBoundsException because your array only have indices 0,1,2,3,4 (5 total)
Your array X is being created, but the elements are not initialized, they are still null. I think you are expecting to have your array initialized like this:
Screens[] X = new Screens[5];
for (int x = 0; x < 5; x++) {
X[x] = new Screens();
}
You get a NullPointerException because you have declared an array to hold 5 screen objects but you have never initialized the 5 slots with an actual Screen object
Thus when you try to use the
X[Screen-1].bookSeat();
you are referencing a null element in the array and of course you cannot call a method of a null object
You could add a check before using the object and initialize the screen
if(Screen >= 1 && Screen <= 4) {
if (X[Screen-1] == null)
X[Screen-1] = new Screens();
X[Screen-1].bookSeat();
}
also there is something weird in your usage. Arrays start at zero index but you use the zero as a value to exit from the program, so the element at index zero is never used.
Because after Screens[] X = new Screens[5]; all 5 elements of array X are null!!
One more way to populate your Screens array
Screens[] X = { new Screens(), new Screens(), new Screens(),new Screens(), new Screens() };
you may find using List from Collections more useful.
Related
I'm handling exceptions for an exercise and runs fine until I enter an invalid number (to try) after running the program for the first time, this is after the first run when asking to re-run with different values if I happen to enter invalid values it won't throw the exception and I don't know why? It's something I don't know or is it my code? Thanks
//program ReverseNumbers.java
//This program reverses the digits of each number in an array.
import java.util.Scanner;
public class ReverseNumbers{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] numbers = new int[5]; //create array numbers size 5
boolean continueInput = true; //controls loop for input
String another = "y";
while(another.equalsIgnoreCase("Y")){ //loop to re-run program
do{
System.out.print("\nEnter 5 positive integers: "); //prompt the user to enter 5 integers
//try block
try{
for(int i = 0; i < numbers.length ; i++) //initialize the array
numbers[i] = input.nextInt();
checkInput(numbers); //handler method
continueInput = false;
}
//catch block
catch(IllegalArgumentException ex){
System.out.print("\nInvalid input: ");
//input.nextLine();
}
}while(continueInput);
//outputs
System.out.print("\nEntered numbers:\t\t");
for(int e: numbers)
System.out.print(e + " ");
System.out.print("\nReversed numbers:\t\t");
reverse(numbers);
//output re-run program
System.out.println();
System.out.print("\nRe-run program with different values, Y/N? ");
another = input.next();
}
}
//Exception method
public static void checkInput(int[] array) throws IllegalArgumentException {
for(int i = 0; i < array.length; i++){
if(array[i]<0)
throw new IllegalArgumentException();
}
}
//method reverse.
public static void reverse(int[] array) {
//reverse order of element within the array
int i, k, t;
int n = array.length;
for (i = 0; i < n / 2; i++) {
t = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = t;
}
reverse(array, array.length-1);
}
//helper method
public static void reverse(int[] array, int n){ //reverse the order of the number for each element in the array
// n, number of elements in the array
if(n>=0){
int Element = array[n]; //element n in array
int NewElement = -1;
int Digit = -1;
String s = "";
if(Element<10)
s = Element + "";
while(Element >= 10){ //loop up to element is reduced to one digit number
Digit = Element%10;
s = s + "" + Digit; //save the digits
NewElement = Element/10;
if(NewElement < 10) //when NewElement has 1 digit left
s = s + "" + NewElement;
Element = NewElement;
}
System.out.print(s + " "); //print digit
reverse(array, n-1); //recursive call
}
}
}
This can be fixed simply by inserting continueInput = true in your outer while loop. Without that, continueInput will always be false after the first time you enter a valid input, and your do-while loop will always exit after one iteration.
However, I wouldn't suggest throwing exceptions yourself, and you should probably handle Scanner's InputMismatchException. Also, your reverse method is unnecessarily complicated. Here's the code I got:
import java.util.Scanner;
public class ReverseNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numbers = new int[5]; //create array numbers size 5
String another = "y";
while (another.equalsIgnoreCase("Y")) { //loop to re-run program
boolean continueInput = true; //controls loop for input
outer: do {
System.out.print("\nEnter " + numbers.length + " positive integers: ");
try {
for (int i = 0; i < numbers.length; i++) {
int num = input.nextInt();
if (num < 0) {
System.out.print("Invalid input, found a negative integer " + num)
continue outer;
} else {
numbers[i] = num;
}
}
continueInput = false;
}
//handle bad inputs that aren't digits
catch (InputMismatchException ex) {
System.out.print("\nInvalid input, please enter integers");
}
} while (continueInput); //outputs
System.out.print("\nEntered numbers:\t\t");
for (int e: numbers) System.out.print(e + " ");
System.out.print("\nReversed numbers:\t\t");
for (int i = numbers.length - 1; i >= 0; i--) {
System.out.print(numbers[i] + (i == 0 ? "\n" : " "));
}
//output re-run program
System.out.println();
System.out.print("\nRe-run program with different values, Y/N? ");
another = input.next();
}
}
}
I want to apply this function in java. Inside while loop, you need to input number of repetition you want to input a number. if you input a number that equals to the number that you enter previously, it will repeat a loop and enter a number again. This code is not finish yet. I hope u understand what i want to achive. thank you
System.out.print("Enter number of times: ");
int times = number.nextInt();
int i = 1;
while ( i <= times){
System.out.print("Enter a number : ");
int input = number.nextInt();
i++;
if( input == input){
System.out.println("It is already taken");
}
}
}
}
Let's use a temp variable to store the value of previous input. If new input is same as previous input, the iterator i should not increase, so we use i--
System.out.print("Enter number of times: ");
int times = number.nextInt();
int i = 1;
int temp=0;
int inputArray[] = new int[times];
while ( i <= times){
System.out.print("Enter a number : ");
int input = number.nextInt();
i++;
if( input == temp){
System.out.println("It is already taken");
i--;
}else {
inputArray[i-2]=input;
}
temp=input;
}
}
The thing with that solution is that is only checks for the number just entered before the current one. I understood that you want to check that the number the user entered is unique and it has to be checked against every number that he/she has entered before.
See the code for that:
import java.util.Scanner;
import java.util.ArrayList;
public class testMe{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of times: ");
int times = scanner.nextInt();
int i = 0;
ArrayList<Integer> listWithEntries = new ArrayList<Integer>();
while (i < times){
System.out.print("Enter a number : ");
int input = scanner.nextInt();
if(listWithEntries.size() == 0){
listWithEntries.add(input);
i++;
} else {
for(int j = 0; j < listWithEntries.size(); j++){
if(input == listWithEntries.get(j)){
System.out.println("It is already taken!");
break;
}
if(j == listWithEntries.size()-1 && input !=
listWithEntries.get(j)){
listWithEntries.add(input);
i++;
break;
}
}
}
}
}
}
First off I'm sorry for the title I couldn't think of a better way to word it. The actual error is in option 3(whenever I try to add together all the sales in option 1). When I try to use salesList.length to track the size of the array I get cannot find symbol- variable length I'm very new to using array lists and that method worked for me in an earlier array but that array wasn't dynamic. Is there a specific way to track the length of a dynamic array list?
import java.util.*;
public class CustomerTest
{
public static void main(String[] args)
{
double totalSales = 0;
ArrayList<String> nameList;
nameList = new ArrayList<String>();
ArrayList<Double> salesList;
salesList = new ArrayList<Double>();
Scanner myScanner = new Scanner(System.in);
boolean done = true;
do
{
System.out.println("1) Add a new customer \n 2) Print all customers \n 3) Compute and print the total sales \n 4) Quit");
int choice = Integer.parseInt(myScanner.nextLine());
if (choice == 1)
{
System.out.print("Add a new customer ");
String answer = myScanner.nextLine();
nameList.add(answer);
System.out.print("Enter their sales ");
String answer2 = myScanner.nextLine();
double answer3 = Double.parseDouble(answer2);
salesList.add(answer3);
}
else if(choice == 2)
{
System.out.println("Customers: " + nameList);
System.out.println("Sales: " + salesList);
}
else if(choice == 3)
{
for(int i = 0; i < salesList.length; i++)
{
totalSales = totalSales + salesList[i];
}
System.out.println(totalSales);
}
else if(choice == 4)
{
System.out.println("Goodbye *Bows gracefully*");
done = false;
}
else
System.out.println("Invalid Choice");
}
while (done);
System.exit(0);
}
}
Change it to salesList.size();. Unlike arrays, the length of an ArrayList is not a directly accessible field.
Array have the length field
ArrayList doesnot have the length field type Use size()
use salesList.size(). unlike arrays you cannot use salesList.lenght
else if (choice == 3) {
for (int i = 0; i < salesList.size(); i++) {
totalSales += salesList.get(i);
}
System.out.println(totalSales);
}
Replace choice 3 with this and it should work.
your code is having bugs: Change the else if(choice==3) {} conditional part to following. you cannot use salesList.length, it can be done using salesList.size() and pleas change salesList[i] to salesList.get(i).
else if(choice == 3)
{
for(int i = 0; i < salesList.size(); i++)
{
totalSales += salesList.get(i);
}
System.out.println(totalSales);
}
I am trying to make a menu where the user has an option to add numbers to the array and technically they can add how many values they want but one at a time. So when they pick to add a number to the array, they would enter one number, and then it would prompt the menu again. I'm having trouble with adding the number to the array & displaying the array. I'll add two numbers & display the array to look at it & I get 30000000000000000000000000000000000000000000000000000000000 because of my array max, I think. all I want is for it to add only the digits I enter to the array when I want to enter it & continue to do that from there.
how exactly would I fix that though? I want the user to have complete control to how big the array will be, etc.
import java.util.*;
public class arrayMenu
{
public static void main(String[] args)
{
Scanner kb;
int option;
kb = new Scanner(System.in);
option = menu(kb);
int[] myArray = new int[99];
while (option != 6)
{
switch (option)
{
case 1:
myArray = newNum(kb, myArray);
break;
case 2:
display(myArray);
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
option = menu(kb);
}
if (option == 6)
{
System.out.println();
System.out.println("Thank you. Have a nice day.");
}
}
public static int menu(Scanner kb)
{
int myOption = 0;
while (myOption != 1 && myOption != 2 && myOption != 3 && myOption != 4 && myOption != 5 && myOption != 6)
{
System.out.println();
System.out.println("Please select from the following menu choices.");
System.out.println();
System.out.println("1. Add a number to the array \n2. Display the mean \n3. Display the median \n4. Print the array to the screen \n5. Print the array in reverse order \n6. Quit");
System.out.println();
System.out.print("Choice --> ");
myOption = kb.nextInt();
kb.nextLine();
if (!(myOption == 1 || myOption == 2 || myOption == 3 || myOption == 4 || myOption == 5 || myOption == 6))
System.out.println("I am sorry that is an invalid menu choice.\nPlease try again");
}
return myOption;
}
public static int intNum(Scanner kb)
{
int num = -1;
while (!(num >= 0))
{
System.out.print("Please enter a non-negative integer -->");
num = kb.nextInt();
if (num < 0)
System.out.print("I am sorry that is not a non-negative integer. \n");
}
return num;
}
public static int[] newNum(Scanner kb, int[] array)
{
for (int i = 0; i < 1; i++)
{
System.out.print("Please enter a number:");
array[i] = kb.nextInt();
}
return array;
}
public static void display(int[] array)
{
for (int myValue : array)
{
System.out.print(myValue);
}
}
}
Well the newNum method looks very questionable. The loop is only going to around once since the loop counter test checks < 1. i will always be 0 and you will always be adding the new number to the [0] place in the array. You have no 'current index' variable to keep track of where your current place in the array is. The array is going to be 1 int and the rest of the values will be 0 - which looks like what you included in the posting.
In this interactive program, you will find a menu with options to perform different functions on an array. This array is taken from a file called "data.txt". The file contains integers, one per line. Obviously, I have not included the entire code (it was too long). However, I was hoping that someone could help me with the problem of finding the prime numbers in the array Right now, the console prints the address of the array for the primes ([I#4a13ccea). Any suggestions are welcome. Part of my program is below. Thanks.
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Calculation Program!\n");
startMenus(sc);
}
private static void startMenus(Scanner sc) throws FileNotFoundException {
while (true) {
System.out.println("(Enter option # and press ENTER)\n");
System.out.println("1. Display the average of the list");
System.out.println("2. Display the number of occurences of a given element in the list");
System.out.println("3. Display the prime numbers in a list");
System.out.println("4. Display the information above in table form");
System.out.println("5. Save the information onto a file in table form");
System.out.println("6. Exit");
int option = sc.nextInt();
sc.nextLine();
switch (option) {
case 1:
System.out.println("You've chosen to compute the average.");
infoMenu1(sc);
break;
case 2:
infoMenu2(sc, sc);
break;
case 3:
infoMenu3(sc);
break;
case 4:
infoMenu4(sc);
break;
case 5:
infoMenu5(sc);
break;
case 6:
System.exit(0);
default:
System.out.println("Unrecognized Option!\n");
}
}
}
private static void infoMenu3(Scanner sc) throws FileNotFoundException {
File file = new File("data.txt");
sc = new Scanner(file);
int[] numbers = new int[100];
int i = 0;
while (sc.hasNextInt()) {
numbers[i] = sc.nextInt();
++i;
}
for (int j = 0; j < i; ++j) {
System.out.print("The numbers in the file are: " + numbers[j] + " ");
}
}
public static boolean prime(int x) {
boolean answer = true;
for (int i = 2; i <= x / 2; i = i + 1) {
if (i != x) {
if (i % x == 0) {
answer = false;
}
}
}
return answer;
}
public static int[] primes(int[] numbers) {
int primesCount = 0;
for (int i : numbers) {
if (prime(i)) {
primesCount = (primesCount + 1);
}
}
if (primesCount == 0) {
return null;
}
int[] result = new int[primesCount];
int index = 0;
for (int i : numbers) {
if (prime(i)) {
result[index] = i;
index = index + 1;
}
}
return result;
}
}
Loop through your array and print every element, or use the java.util.Arrays.toString(int[]) method if its format suits your needs.
Two marks
If you print an array like this, you will get the address of the array, not the inside.
System.out.println("The primes in the file are: " + primes(numbers));
Replace this line with a loop that iterates over primes(numbers)
The second is, in your public static boolean prime(int x) function you have this line
for (int i = 2; i <= x / 2; i = i + 1)
Although this works, to find a prime yo do not need to iterate until x / 2 . For performance benefits Square root of x would suit better.