-1

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
            }
         }
        }
Bashir
  • 2,057
  • 5
  • 19
  • 44
  • I can't see why `String command = console.nextLine();` would show such error. Obviously there is the error others mentioned. – Amongalen Mar 12 '20 at 11:35

3 Answers3

0
Object command = null;
if (command.equals("Play music")) {

This is guaranteed to result in a NullPointerException, crashing your program. So you get a warning about it.

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

You introduced two different variables command where probably one was intended.

   String command = "";
   while (true) {
        command = console.nextLine();
        if (temperature < 68) {
              System.out.println("AC ON");
            }
            else {
              System.out.println("Heat ON");
            }
        break;
    }

        if (command.equals("Play music")) {
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

The variable Command can only be null at this point because you have declared command variable as null here

Object command = null;
if (command.equals("Play music")) {}

Please remove Object command = null; and declare command variable outside first while loop like following:

String command = console.nextLine();
while (true) {.. }
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24