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);...
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...
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.
What is Try and Catch in Java with example?
What is Try and Catch in Java with example?
Needs to be in basic JAVA Write a program that does basic encrypting of text. You...
Needs to be in basic JAVA Write a program that does basic encrypting of text. You will ask the user the filename of a text file that contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter...
a) According to Hayek, what is the problem we try to solve “to construct a rational...
a) According to Hayek, what is the problem we try to solve “to construct a rational economic order”? b) What does Hayek mean by the knowledge problem? c) What is the role prices in the market process?
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...
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT