Exception in thread "main" java.lang.NullPointerException
at customer.customer.update(Main.java:149)
at customer.customer.menu(Main.java:183)
at customer.Main.main(Main.java:237)
Java Result: 1
I am not not understanding what it wants. It works ok (or seems to) for Adding a customer, but will not find or update a customer.
I have attached my code. Any help would be appreciated.
I am using Netbeans IDE 6.9.1.
**********************
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package customer;
/**
*
* @author
*/
import java.util.Scanner;
class customer {
static int n=0;
static customer newCustomer[] = new customer[10];
private int ID;
private String firstname;
private String lastname;
private String address;
private String city;
private int phone;
private int fax;
private String menu;
private String exitMenu;
customer(){
ID=0;
firstname=null;
lastname=null;
address=null;
city=null;
phone=0;
fax=0;
}
public void SetID(){
System.out.println("Please enter ID: ");
Scanner in= new Scanner(System.in);
ID=in.nextInt();
in.close();
}
public void setfirstName(){
System.out.println("Please enter firstname: ");
Scanner in= new Scanner(System.in);
firstname=in.nextLine();
in.close();
}
public void setlastName(){
System.out.println("Please enter lastname: ");
Scanner in= new Scanner(System.in);
lastname=in.nextLine();
in.close();
}
public void setaddress(){
System.out.println("Please enter Address: ");
Scanner in= new Scanner(System.in);
address=in.nextLine();
in.close();
}
public void setcity(){
System.out.println("Please enter City: ");
Scanner in= new Scanner(System.in);
city=in.nextLine();
in.close();
}
public void Setphone(){
System.out.println("Please enter Phone Number: ");
Scanner in= new Scanner(System.in);
phone=in.nextInt();
in.close();
}
public void Setfax(){
System.out.println("Please enter Fax Number: ");
Scanner in= new Scanner(System.in);
fax=in.nextInt();
in.close();
}
public void setAll(){
System.out.println("Please enter ID = ");
Scanner scanID= new Scanner(System.in);
ID=scanID.nextInt();
System.out.println("Please enter firstname: ");
Scanner scanFName= new Scanner(System.in);
firstname=scanFName.nextLine();
System.out.println("Please enter lastname: ");
Scanner scanLName= new Scanner(System.in);
lastname=scanLName.nextLine();
System.out.println("Please enter Address: ");
Scanner scanAddress= new Scanner(System.in);
address=scanAddress.nextLine();
System.out.println("Please enter City: ");
Scanner scanCity= new Scanner(System.in);
city=scanCity.nextLine();
System.out.println("Please enter Phone Number: ");
Scanner scanPhone= new Scanner(System.in);
phone=scanPhone.nextInt();
System.out.println("Please enter Fax Number: ");
Scanner scanFax= new Scanner(System.in);
fax=scanFax.nextInt();
}
public void showAll(){
System.out.println("ID = " + ID);
System.out.println("FirstName = " + firstname);
System.out.println("LastName = " + lastname);
System.out.println("Address = " + address);
System.out.println("City = " + city);
System.out.println("Phone = " + phone);
System.out.println("Fax = " + fax);
}
public void showDetails(){
System.out.println("FirstName = " + firstname);
System.out.println("LastName = " + lastname);
System.out.println("Address = " + address);
System.out.println("City = " + city);
System.out.println("Phone = " + phone);
System.out.println("Fax = " + fax);
}
public void add(){
customer c = new customer();
c.setAll();
c.showAll();
n++;
}
public void update(){
System.out.println("Please enter customer ID: ");
Scanner scanID=new Scanner(System.in);
ID=scanID.nextInt();
//adjust for loop to how many added customers
for(int i=0; i<1; i++)
if (newCustomer[i].getID()==ID){
newCustomer[i].setAll();
}
}
public void find(){
System.out.println("Please enter customer ID: ");
Scanner scanID=new Scanner(System.in);
ID=scanID.nextInt();
//adjust for loop to how many added customers
for(int a=0; a<1; a++)
if(newCustomer[a].getID()==ID){
newCustomer[a].showDetails();
}
}
public void menu(){
do
{
System.out.println("Please enter A (uppercase)if you would like to add a customer ");
System.out.println("Please enter U if you would like to update a customer: ");
System.out.println("Please enter F if you would like to find a customer");
Scanner scanAdd=new Scanner(System.in);
menu=scanAdd.nextLine();
if(menu.equals("A")){
add();
System.out.println("Would you like anything else, enter y or n?");
Scanner scanMenu=new Scanner(System.in);
exitMenu=scanMenu.nextLine();
if(exitMenu.equals("y")){
exitMenu="y";
}
}
if(menu.equals("U")){
update();
System.out.println("Would you like anything else, enter y or n?");
Scanner scanMenu2=new Scanner(System.in);
exitMenu=scanMenu2.nextLine();
if(exitMenu.equals("y")){
exitMenu="y";
}
}
if(menu.equals("F")){
find();
System.out.println("Would you like anything else, enter y or n?");
Scanner scanMenu3=new Scanner(System.in);
exitMenu=scanMenu3.nextLine();
if(exitMenu.equals("y")){
exitMenu="y";
}
}
}while (exitMenu.equals("y"));
}
public int getID(){
return ID;
}
public String getfirstName(){
return firstname;
}
public String getlastName(){
return lastname;
}
public String getaddress(){
return address;
}
public String getcity(){
return city;
}
public int getphone(){
return phone;
}
public int getfax(){
return fax;
}
}
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws java.io.IOException {
// TODO code application logic here*/
customer c = new customer();
c.menu();
}
}
Please
This post has been edited by pbl: 22 October 2010 - 09:08 PM
Reason for edit:: Code tags added for a newbie

New Topic/Question
Reply




MultiQuote




|