Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im new to java and below are the three diffrent classes to a project im working

ID: 658033 • Letter: I

Question

Im new to java and below are the three diffrent classes to a project im working on. Im having trouble with the main menu commands in the SupportCenter2 Class and DeviceList class 2,3,4,5. For example when I try to execute #2 remove a device by device number I get Exception in thread "main" java.lang.NullPointerException at DeviceList.remove(DeviceList.java:125 at SupportCenter2.input(SupportCenter2.java:73   at SupportCenter2.main(SupportCenter2.java:42). Im not sure what need to be done so that my menu commands stop throing these errors any help would be greatly appreciated.

**********************************************MAIN CLASS BELOW****************************************************

import java.util.Scanner;

/**
* This class runs the system.
*
* It holds the main for the entire program.
* Make a menu that can be used to call the methods
* in this class other than the main, input, constructor, and makeMenu.
*
* @author Your Name
* @version 0.0
*/
public class SupportCenter2 {

private final DeviceList dl;

/**
* No-arg constructor for SupportCenter class.
* Must initialize a dl as a new Device List
*
*/
public SupportCenter2()
{
    dl = new DeviceList();
}

/**
* Main method for the program.
* Needs to create a create a SupportCenter object, a scanner, and call helper methods
* to print the menu and take in input.
*
* @param args - command line arguments.
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
   Scanner sc = new Scanner(System.in);   
int i = 0;   
SupportCenter2 support = new SupportCenter2();
while(i == 0) {
support.makeMenu();
support.input(sc); }   
  
DeviceList dl = new DeviceList();
}

/**
* This method handles the input from the user.
*
* @param sc - scanner passed from main.
* @throws java.lang.Exception
*/
public void input(Scanner sc) throws Exception
{
   int user = sc.nextInt();
switch(user) {   
       case 1:
Device newDevice = new Device(0, "", "");
       System.out.print("Enter the Device Number:");
       int deviceNum = sc.nextInt();
newDevice.setDeviceNum(deviceNum);   
   System.out.print("Enter in the Owner Name:");   
       String ownerName = sc.next();   
       newDevice.setOwnerName(ownerName);
       System.out.print("Enter in the Telephone Number:");
       String phoneNum = sc.next();
       newDevice.setPhoneNum(phoneNum);   
       dl.add(newDevice);
       break;
case 2:
System.out.println("Enter the Device Number you wish to remove");   
int removeDev = sc.nextInt();   
dl.remove(removeDev);
break;
case 3:   
System.out.println("Enter the Phone Number or Owner Name you for which device you wish to remove");
String removeOwnerName = sc.next();
dl.remove(removeOwnerName);
String removePhoneNum = sc.next();
dl.remove(removePhoneNum);
break;
case 4:   
System.out.println("Enter the Device Number you wish to lookup");
int lookupdeviceNum = sc.nextInt();   
System.out.println(Integer.toString(lookupdeviceNum));

break;
case 5:
System.out.println("Enter Owner Name or Phone Number for which device you wish to lookup");
String lookupOwnerName = sc.next();   
System.out.println(lookupOwnerName);
String lookupPhoneNum = sc.next();   
System.out.println(lookupPhoneNum);
break;
case 6:
System.out.println(dl);
break;
case 0:
System.out.println("Quitting");
System.exit(1);
break;
  
}
}

/**
* This method prints the menu.
* It is called after each input is complete.
*
*/
public void makeMenu()
{
System.out.println("************************************************************");
System.out.println("1. Add a device");   
System.out.println("2. Remove a device by Device Number");   
System.out.println("3. Remove a device by Name or Phone Number");   
System.out.println("4. Lookup a device by Device Number");   
System.out.println("5. Lookup a device by Name or Phone Number");
System.out.println("6. Print all devices");
System.out.println("0. Quit");
System.out.println("************************************************************");
}

/**
* This method handles making and adding a new device to the DeviceList.
*
* @param deviceNum
* @param ownerName
* @param phoneNum
*/
public void add(int deviceNum, String ownerName, String phoneNum)
{
       dl.add(new Device(deviceNum, ownerName, phoneNum));
}

/**
* This removes a device by device number.
*
* @param deviceNum
*/
public void remove(int deviceNum)
{
       dl.remove(deviceNum);
}

/**
* Removes device by either phoneNum or ownerName.
*
* @param info
*/
public void remove(String info)
{
       dl.remove(info);
}

/**
* looks up and prints a device by its number.
*
* @param deviceNum
*/
public void lookup(int deviceNum)
{
       if (deviceNum == deviceNum )
{
System.out.print(Integer.toString(deviceNum));
}
else if (deviceNum != deviceNum)   
{
System.out.println("Error. Device not found.");
}   
}

/**
* looks up and prints a device by phoneNum or ownerName.
*
* @param info
*/
public void lookup(String info)
{
       if (!info.equals(info))
{
System.out.println("Error. Device not found.");
}
else
{
System.out.print(info);
}
}

/**
* Prints all of the devices.
*
*/
public void printAll()
{
       System.out.print(dl);
}

}

****************************************DEVICELIST CLASS BELOW*************************************

/**
* This class will be the data structure used in this program.
*
* It uses an array of Devices as the storage, and uses numDevices
* to track how many Devices have been entered into the array.
* This allows only the devices to be iterated through rather than the
* entire array.
*
* @author Your Name
* @version 0.0
*/

public class DeviceList
{

   private Device[] array;
   private int numDevices;

   /**
   * No-arg constructor.
   * Set fields to defaults.
   * array should be of size 50.
   *
   */
   public DeviceList()
   {
       numDevices = 50;
array = new Device[numDevices];
   }
   /**
   * args Constructor.
   * set fields equal to parameters.
   *
   * @param array - previously created array.
   * @param numDevices - number of devices in array.
   */
   public DeviceList(Device[] array, int numDevices)
   {
       this.numDevices = numDevices;
array = new Device[numDevices];
Device[] devicelist = null;

System.arraycopy(devicelist, 0, array, 0, numDevices);
   }

   /**
   * Accessor for array. Needed in SupportCenter.
   *
   * @return Device[]
   */
   public Device[] getArray()
   {
       return array;
   }

   /**
   * Add a new device from a device that is already made.
   * Check to see if the new device already exists in the array,
   * if not, add it to the end and increment numDevices.
   *
   * @param dev
   */
   public void add(Device dev)
   {
       if(lookup(dev.getDeviceNum())==null &&
lookup(dev.getOwnerName())==null)
{
Device[] deviceList= new Device[array.length];

deviceList=array;
numDevices=numDevices+1;
array=new Device[numDevices];

System.arraycopy(deviceList, 0, array, 0, deviceList.length);

array[numDevices-1]=dev;

}
   }

   /**
   * Removes a device that is equal to dev.
   * Uses its own lookup to check if dev is equal to anything in the array.
   * if so, it removes it in the same way as the other removes
   * Check to see if there are any devices in the list first.
   *
   * @param dev
   */
   public void remove(Device dev)
   {
       boolean found=false;

for (int index = 0; index < numDevices && !found; index++)
{
if(dev.getDeviceNum()==array[index].getDeviceNum() &&
dev.getOwnerName().equalsIgnoreCase(array[index].getOwnerName()) &&
dev.getPhoneNum().equalsIgnoreCase(array[index].getPhoneNum()))
{
found=true;
array[index]=null;
}
}

if(!found)
System.out.println("Device not found");

   }

   /**
   * Removes a device with the same device deviceNum
   * Looks up the device based off deviceNum provided, if not found it prints an error
   * if it is not the last device in the list, it is replaced by the last device in the list
   * if it is the last device, it makes that index null in the array
   * it decrements numDevices if the device is found.
   *
   * @param deviceNum
   */
   public void remove(int deviceNum)
   {
       boolean found=false;
  

for (int index = 0; index < numDevices && !found; index++)
{
if(array[index].getDeviceNum()== array[index].getDeviceNum())
{
found=true;
array[index]= null;
}
}

if(!found)
System.out.println("Device not found");

   }

   /**
   * Removes a device with the same name or phone.
   * Looks up the device based off of info provided, if not found it prints an error
   * if it is not the last device in the list, it is replaced by the last device in the list
   * if it is the last device, it makes that index null in the array
   * it decrements numDevices if the device is found.
   *
   * @param info - name or phone number.
   */
   public void remove(String info)
   {
       boolean found=false;

for (int index = 0; index < numDevices && !found; index++)
{
if(array[index].getOwnerName().equalsIgnoreCase(info)|| array[index].getPhoneNum().equals(info))
{
found=true;
array[index]=null;
}
}

if(!found)
System.out.println("Device not found");

   }

   /**
   * Returns a device with a given deviceNum.
   * This should search the array and return the index of the last device with
   * the given deviceNum.
   *
   * @param deviceNum
   * @return Device
   */
   public Device lookup(int deviceNum)
   {
       if(deviceNum>0 && deviceNum<numDevices)
return array[deviceNum];
else
return null;
   }

   /**
   * Returns a device with a given phone number or name.
   * This should search the array and return the index of the last device with
   * the given ownerName or phoneNum.
   *
   * @param info
   * @return Device
   */
   public Device lookup(String info)
   {
       for (int index = 0; index < numDevices; index++)
{
if(array[index]!=null)
if(array[index].getOwnerName().equalsIgnoreCase(info)|| array[index].getPhoneNum().equals(info))
return array[index];
}

return null;
   }

   /**
   * toString for DeviceList.
   * This should display the return a string made up of the strings from each Device in the array.
   *
   * @return String
   */
@Override
   public String toString()
   {
       String deviceList="";
int index;
for (index = 0; index < numDevices; index++)
{
if(array[index]==null)
deviceList+="Null ";
else
deviceList+=array[index].toString()+" ";

}

return deviceList;
}
   }

****************************************DEVICE CLASS BELOW***********************************************

/**
* The Device class for the device object.
*
* The Device class creates a device object with a
* device number, owner name, and a phone number.
*
* @author Your Name
* @version 0.0
*/
public class Device
{
public int deviceNum;
public String ownerName;
public String phoneNum;

/**
* The Device Constructor.
*
* It sets the fields from the parameters
*
* @param deviceNum The Device's number.
* @param ownerName The Owner's name.
* @param phoneNum The Owner's phone number.
*/
public Device(int deviceNum, String ownerName, String phoneNum)
{
       this.deviceNum = deviceNum;
       this.ownerName = ownerName;
       this.phoneNum = phoneNum;
}

/**
* Mutator for DeviceNum.
*
* Allows you to set deviceNum from another class.
* Should ensure that it is not 0 or below.
* If it is below 0, it should throw an exception.
*
* @param deviceNum The Device Number.
* @throws java.lang.Exception
*/
public void setDeviceNum(int deviceNum) throws Exception
{
       this.deviceNum = deviceNum;
}

/**
* Mutator for ownerName.
*
* Allows you to set ownerName from another class.
*
* It ensures that the ownerName is not null before it puts it in,
* Throws an exception if it is null.
*
* @param ownerName The Owner's Name.
* @throws java.lang.Exception
*/
public void setOwnerName(String ownerName) throws Exception
{
       this.ownerName = ownerName;
}

/**
* Mutator for PhoneNum.
*
* Allows you to set phoneNum from another class.
*
* It ensures that the phoneNum is not null before it puts it in,
* Throws an exception if it is null.
*
* @param phoneNum the owner's phone number.
* @throws java.lang.Exception
*/
public void setPhoneNum(String phoneNum) throws Exception
{
       this.phoneNum = phoneNum;
}

/**
* Accessor for device number.
*
* Returns deviceNum.
*
* @return returns device number.
*/
public int getDeviceNum()
{
return 0;
}

/**
* Accessor for owners name
*
* returns ownerName.
*
* @return returns owner's name.
*/
public String getOwnerName()
{
return ownerName;
}

/**
* Accessor for phone number.
*
* returns phoneNum
*
* @return returns phone number.
*/
public String getPhoneNum()
{
return phoneNum;
}

/**
* To String method.
*
* This is what you will do to display each device.
* They should display as such.
* deviceNum ownerName phoneNum
* Do not forget the new line character at the end.
*
* @return returns the string.
*/
@Override
public String toString()
{
return "deviceNum: " + deviceNum + " " +
       "ownerName: " + ownerName + " " +
       "phoneNum: " + phoneNum + " ";
}

/**
* equals method.
*
* use this to see if two devices are equal.
* Compare the fields of each, if one is different, return false
* otherwise, return true.
*
* @param dev
* @param deviceNum The Device's number.
* @param ownerName The Owner's name.
* @param phoneNum The Owner's phone number.
* @return returns logic based on it's equality.
*/
public boolean equals(Device dev)
{
if (dev instanceof Device)
{
Device device = (Device) dev;
if
(
deviceNum==(device.getDeviceNum()) &&
ownerName.equals(device.getOwnerName()) &&
phoneNum.equals(device.getPhoneNum())
) {
return true;
}

}
return false;

}
}

Explanation / Answer

import java.util.Scanner;

public class SupportCenter2();

private final DeviceList dl();

public SupportCenter2()
{
dl = new DeviceList();
}

public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);   
int i = 0;   
SupportCenter2 support = new SupportCenter2();
while(i == 0) {
support.makeMenu();
support.input(sc); }   
  
DeviceList dl = new DeviceList();
}

public void input(Scanner sc) throws Exception
{
int user = sc.nextInt();
switch(user) {   
case 1:
Device newDevice = new Device(0, "", "");
System.out.print("Enter the Device Number:");
int deviceNum = sc.nextInt();
newDevice.setDeviceNum(deviceNum);   
System.out.print("Enter in the Owner Name:");   
String ownerName = sc.next();   
newDevice.setOwnerName(ownerName);
System.out.print("Enter in the Telephone Number:");
String phoneNum = sc.next();
newDevice.setPhoneNum(phoneNum);   
dl.add(newDevice);
break;
case 2:
System.out.println("Enter the Device Number you wish to remove");   
int removeDev = sc.nextInt();   
dl.remove(removeDev);
break;
case 3:   
System.out.println("Enter the Phone Number or Owner Name you for which device you wish to remove");
String removeOwnerName = sc.next();
dl.remove(removeOwnerName);
String removePhoneNum = sc.next();
dl.remove(removePhoneNum);
break;
case 4:   
System.out.println("Enter the Device Number you wish to lookup");
int lookupdeviceNum = sc.nextInt();   
System.out.println(Integer.toString(lookupdeviceNum));

break;
case 5:
System.out.println("Enter Owner Name or Phone Number for which device you wish to lookup");
String lookupOwnerName = sc.next();   
System.out.println(lookupOwnerName);
String lookupPhoneNum = sc.next();   
System.out.println(lookupPhoneNum);
break;
case 6:
System.out.println(dl);
break;
case 0:
System.out.println("Quitting");
System.exit(1);
break;
  
}
}
public void makeMenu()
{
System.out.println("************************************************************");
System.out.println("1. Add a device");   
System.out.println("2. Remove a device by Device Number");   
System.out.println("3. Remove a device by Name or Phone Number");   
System.out.println("4. Lookup a device by Device Number");   
System.out.println("5. Lookup a device by Name or Phone Number");
System.out.println("6. Print all devices");
System.out.println("0. Quit");
System.out.println("************************************************************");
}

public void add(int deviceNum, String ownerName, String phoneNum)
{
dl.add(new Device(deviceNum, ownerName, phoneNum));
}


public void remove(int deviceNum)
{
dl.remove(deviceNum);
}

public void remove(String info)
{
dl.remove(info);
}


public void lookup(int deviceNum)
{
if (deviceNum == deviceNum )
{
System.out.print(Integer.toString(deviceNum));
}
else if (deviceNum != deviceNum)   
{
System.out.println("Error. Device not found.");
}   
}

public void lookup(String info)
{
if (!info.equals(info))
{
System.out.println("Error. Device not found.");
}
else
{
System.out.print(info);
}
}
public void printAll()
{
System.out.print(dl);
}

}


public class DeviceList
{

private Device[] array;
private int numDevices;


public DeviceList()
{
numDevices = 50;
array = new Device[numDevices];
}
  
public DeviceList(Device[] array, int numDevices)
{
this.numDevices = numDevices;
array = new Device[numDevices];
Device[] devicelist = null;

System.arraycopy(devicelist, 0, array, 0, numDevices);
}
public Device[] getArray()
{
return array;
}


public void add(Device dev)
{
if(lookup(dev.getDeviceNum())==null &&
lookup(dev.getOwnerName())==null)
{
Device[] deviceList= new Device[array.length];

deviceList=array;
numDevices=numDevices+1;
array=new Device[numDevices];

System.arraycopy(deviceList, 0, array, 0, deviceList.length);

array[numDevices-1]=dev;

}
}

  
public void remove(Device dev)
{
boolean found=false;

for (int index = 0; index < numDevices && !found; index++)
{
if(dev.getDeviceNum()==array[index].getDeviceNum() &&
dev.getOwnerName().equalsIgnoreCase(array[index].getOwnerName()) &&
dev.getPhoneNum().equalsIgnoreCase(array[index].getPhoneNum()))
{
found=true;
array[index]=null;
}
}

if(!found)
System.out.println("Device not found");

}

public void remove(int deviceNum)
{
boolean found=false;
  

for (int index = 0; index < numDevices && !found; index++)
{
if(array[index].getDeviceNum()== array[index].getDeviceNum())
{
found=true;
array[index]= null;
}
}

if(!found)
System.out.println("Device not found");

}

  
public void remove(String info)
{
boolean found=false;

for (int index = 0; index < numDevices && !found; index++)
{
if(array[index].getOwnerName().equalsIgnoreCase(info)|| array[index].getPhoneNum().equals(info))
{
found=true;
array[index]=null;
}
}

if(!found)
System.out.println("Device not found");

}

public Device lookup(int deviceNum)
{
if(deviceNum>0 && deviceNum<numDevices)
return array[deviceNum];
else
return null;
}
public Device lookup(String info)
{
for (int index = 0; index < numDevices; index++)
{
if(array[index]!=null)
if(array[index].getOwnerName().equalsIgnoreCase(info)|| array[index].getPhoneNum().equals(info))
return array[index];
}

return null;
}
public String toString()
{
String deviceList="";
int index;
for (index = 0; index < numDevices; index++)
{
if(array[index]==null)
deviceList+="Null ";
else
deviceList+=array[index].toString()+" ";

}

return deviceList;
}
}

public class Device
{
public int deviceNum;
public String ownerName;
public String phoneNum;
public Device(int deviceNum, String ownerName, String phoneNum)
{
this.deviceNum = deviceNum;
this.ownerName = ownerName;
this.phoneNum = phoneNum;
}
public void setDeviceNum(int deviceNum) throws Exception
{
this.deviceNum = deviceNum;
}
public void setOwnerName(String ownerName) throws Exception
{
this.ownerName = ownerName;
}
public void setPhoneNum(String phoneNum) throws Exception
{
this.phoneNum = phoneNum;
}


public int getDeviceNum()
{
return 0;
}
public String getOwnerName()
{
return ownerName;
}
public String getPhoneNum()
{
return phoneNum;
}
public String toString()
{
return "deviceNum: " + deviceNum + " " +
"ownerName: " + ownerName + " " +
"phoneNum: " + phoneNum + " ";
}

public boolean equals(Device dev)
{
if (dev instanceof Device)
{
Device device = (Device) dev;
if
(
deviceNum==(device.getDeviceNum()) &&
ownerName.equals(device.getOwnerName()) &&
phoneNum.equals(device.getPhoneNum())
) {
return true;
}

}
return false;

}
}