Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 Arithmimage text in transcribedeticOperators.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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions