NullPointerException

Help with NullPointerException error

Page 1 of 1

2 Replies - 841 Views - Last Post: 22 October 2010 - 09:15 PM Rate Topic: -----

#1 jo_robo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-June 10

NullPointerException

Posted 21 October 2010 - 10:16 PM

Hello. I am getting the following error when I run my code:

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 :code:

This post has been edited by pbl: 22 October 2010 - 09:08 PM
Reason for edit:: Code tags added for a newbie


Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: NullPointerException

Posted 21 October 2010 - 10:20 PM

Please put your code in the [ code][/code] tags.

This post has been edited by H3R3T1C: 21 October 2010 - 10:20 PM

Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: NullPointerException

Posted 22 October 2010 - 09:15 PM

I can't ever understand your program run and generate anr error
}

public class Main {


you do not need a class Main
you need a public static void main(String[] args) method in your master class

and a .java file cannot have public clas but the one that matches the filename
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1