Question

In: Computer Science

Basic JAVA Question When we try to run this program, a problem occurs. What is this...

Basic JAVA Question

When we try to run this program, a problem occurs. What is this problem called? And why does this problem occur here?
public class Main {
   public static void main(String[] args) {
   PhoneNumber pn = new PhoneNumber();
       System.out.println(pn.isTollFree());
   }
}

class PhoneNumber {
String number;
  
public boolean isTollFree() {
return number.charAt(1) == '8';
}
}

Solutions

Expert Solution

-The problem is called the NullPointer Exception.

-The problem occurs here because String number hasn't been assigned any value .It contains Null. But an object reference is being used here :

number.charAt(1) == '8'

If String Number contained some value, this error wouldn't occur.

Proof:

Without Assignment :

With Assignment :

Please comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks :)


Related Solutions

Java programming: Save the program as DeadlockExample.java   Run the program below.  Note whether deadlock occurs.  Then modify the...
Java programming: Save the program as DeadlockExample.java   Run the program below.  Note whether deadlock occurs.  Then modify the program to add two more threads: add a class C and a class D, and call them from main.  Does deadlock occur? import java.util.concurrent.locks.*; class A implements Runnable {             private Lock first, second;             public A(Lock first, Lock second) {                         this.first = first;                         this.second = second;             }             public void run() {                         try {                                     first.lock();                                     System.out.println("Thread A got first lock.");                                     // do something                                     try {                                                 Thread.sleep( ((int)(3*Math.random()))*1000);...
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
This is just a very basic program/problem in Java I'm looking for some second opinions on....
This is just a very basic program/problem in Java I'm looking for some second opinions on. 1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive). 2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked. 3. Take a year input from the user (should be between 1500 and 2019) 4. If the input year is a...
Write a guessing game java program (basic program, we didn't study further than loops and files)...
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program: Enter your secret number...
/* Problem 1 * Write and run a java program that prints out two things you...
/* Problem 1 * Write and run a java program that prints out two things you have learned * so far in this class, and three things you hope to learn, all in different lines. */ You could write any two basic things in Java. /* Problem 2 * The formula for finding the area of a triangle is 1/2 (Base * height). * The formula for finding the perimeter of a rectangle is 2(length * width). * Write a...
Long-run macroeconomic equilibrium occurs when
Long-run macroeconomic equilibrium occurs whenSelect one:a. Aggregate demand equals short-run aggregate supply.b. Aggregate demand equals short-run aggregate supply and they intersect at a point on the long-run supply curve.c. Structural and frictional unemployment equals zero.d. Output is above potential GDP.
This java program try to count all of the 1 at the rim of the block...
This java program try to count all of the 1 at the rim of the block of 1 surrounded by 0. The total of the count also includes all the 1 surrounded two pockets of 0 within the block. The correct answer fro the total count is 154. However, this program gives 167. Where is the fault of the program?    public class Cloture {     public static void main(String[] args) { int[][] carte = {                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0},                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
QUESTION 1 A Type II error occurs when we ________. reject the null hypothesis when it...
QUESTION 1 A Type II error occurs when we ________. reject the null hypothesis when it is actually true reject the null hypothesis when it is actually false do not reject the null hypothesis when it is actually false do not reject the null hypothesis when it is actually true QUESTION 2 A lower level of significance makes it harder to reject the null hypothesis. True False QUESTION 3 A professional sports organization is going to implement a test for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT