Question: Hello, in my code I'm trying to implement my scanner into my if statement so that if the user responds with add then it will

Hello, in my code I'm trying to implement my scanner into my if statement so that if the user responds with "add" then it will cause my addition method to run. However I keep getting this runtime error. "Exception in thread "main" java.lang.NullPointerException at ArithmHello, in my code I'm trying to implement my scanner into myeticOperators.main(ArithmeticOperators.java:14)

I have no idea what causing this exception and was wondering if someone could tell me. Below is a copy of my code.

import java.util.*;

public class ArithmeticOperators{

private static Scanner userChoice;

public static void main(String[]args){

System.out.println("Please choose to add, subtract,"); System.out.println("multiply, or divide!"); if(userChoice.equals("add")){ Addition(result);} else{ System.out.println("thanks for trying"); }

} public static int result; public static int Addition(int list){ userChoice = new Scanner(System.in); LinkedList aL = new LinkedList(); int a = userChoice.nextInt(); int b = userChoice.nextInt(); int c = userChoice.nextInt(); aL.add(a); aL.add(b); aL.add(c); result = a + b + c; System.out.println("The elements of your list are:" ); System.out.println(result); return result; }

}

e eclipse ava Arithmeticop java Pse erators/src/Arithmeticoperators File Edit Source Refactor Navigate Search Project Run Window Help project eticoperators java Package Exp 19 20 public static int result; AminoAcids 21 ArithmeticOperators 22 public static int Addition (int list) 23 v deraul package 24 user Choice new Scanner (System. in 25 etioope LinkedList Ari ava Apylialion] CAPI e 18.0 121M avaw.exe (Feb 22, 2017 4:3449 PM) Please choose to add, subtract, multiply, or divide Exception in thread "main" ava lan NullPointerException at Arithmeticoperators .main (Arithmeticoperators iava-14) Ask me anything Quick Access 4:4. 2/22/2017

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!