This is the code I have been working on for the past four days. I keep running into a problem on two of the lines. On both "String command = console.nextLine();
" and "if (command.equals("Play music")) {
" I get the same message. "Null pointer access: The variable Command can only be null at this point
". I can't for the life of me figure out what is wrong with this code. Can anyone help me figure out what the problem is and how to solve it?
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;//import required header files
public class SMARTHOMES2 {
//define the class
public static void main(String args[]) throws Exception {
}
{
double temperature = 0;
Scanner console = new Scanner(System.in);
System.out.println("Welcome home! What would you like to do?");
while (true) {
String command = console.nextLine();
if (temperature < 68) {
System.out.println("AC ON");
}
else {
System.out.println("Heat ON");
}
break;
}
Object command = null;
if (command.equals("Play music")) {
System.out.println("What would you like to hear?");
String music = console.nextLine();
System.out.print("Now Playing:");
List<String> lines = Arrays.asList("The first line", "The second line");
Paths.get("Say So by Doja Cat.txt");
Path file1 = Paths.get("Tattoo Tears by Iyla.txt");
Paths.get("The Best In Me by Marvin Sapp.txt");
Paths.get("Fallin' for You by The Sax Pack.txt");
Paths.get("Love by Jhene Aiko.txt");
try {
Files.write(file1, lines, StandardCharsets.UTF_8);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(music);
}
if (command.equals("Turn on Television")) {
System.out.println("Which room would you like?");
String television = console.nextLine();
System.out.print("Would you like the living room light dimmed?");
System.out.println(television);
}
if (command.equals("Change Television Channel")) {
System.out.println("Okay");
List<String> lines = Arrays.asList("The first line", "The second line");
Paths.get("Channel 34.txt");
Path file1 = Paths.get("Channel 56.txt");
Paths.get("Channel 13.txt");
Paths.get("Channel 44.txt");
Paths.get("Channel 78.txt");
try {
Files.write(file1, lines, StandardCharsets.UTF_8);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (command.equals("Turn on lights")) {
System.out.println("Which room light would you like?");
String light = console.nextLine();
System.out.print("Light On");
System.out.println(light);
}
if (command.equals("Make a Call")) {
System.out.println("Who would you like me to call?");
String call = console.nextLine();
System.out.print("Now Calling: 298-654-8632");
System.out.println(call);
}
if(command.equals("Make a Call")) {
System.out.println("Who would you like to call?");
String call = console.nextLine();
System.out.print("Invalid number would you like to try again?");
System.out.println(call);
}
if (command.equals("Answer doorbell")) {
System.out.println("Answering doorbell");
System.out.println("What message would you like send?");
String doorbell = console.nextLine();
System.out.print("Be right there");
System.out.println(doorbell);
}
while (true) {
if (command.equals("Stop music")) {
System.out.println("Stopping music");
console.close();
//end the system and stop the loop
}
if (command.equals("Turn off Television")) {
System.out.println("Television off");
console.close();
break;
//end the system and stop the loop
}
if (command.equals("Turn off Light")) {
System.out.println("Light Off");
console.close();
break;
//end the system and stop the loop
}
if (command.equals("Close system")) {
System.out.println("Shutting down");
console.close();
break;
}
// end the system and stop the loop
}
}
}