User interface class

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1055 Views - Last Post: 01 November 2010 - 02:40 PM Rate Topic: -----

#1 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

User interface class

Posted 01 November 2010 - 10:37 AM

I have been asked to design a basic user inerface as part of my course the code below comes back with an error message exception null.


public void printCustomerInfo()
    
    {
        System.out.println(name.getCustomerName());
    }




I have a name class which is linked in and that class contains the CustomerName part my user interface allows you to creat a new order which you press 1 and enter the various details when the user presses 2 it should print the customers name yet it comes up with the error.

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: User interface class

#2 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: User interface class

Posted 01 November 2010 - 10:53 AM

well your error lies in your getCustomerName() method because there nothing wrong with your println :P
Was This Post Helpful? 0
  • +
  • -

#3 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: User interface class

Posted 01 November 2010 - 11:02 AM

View PostHandler, on 01 November 2010 - 09:53 AM, said:

well your error lies in your getCustomerName() method because there nothing wrong with your println :P


very strange as its not working
Was This Post Helpful? 0
  • +
  • -

#4 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: User interface class

Posted 01 November 2010 - 11:12 AM

would you please post up getCustomerName() because thats where your error is
Was This Post Helpful? 0
  • +
  • -

#5 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: User interface class

Posted 01 November 2010 - 11:47 AM

View PostHandler, on 01 November 2010 - 10:12 AM, said:

would you please post up getCustomerName() because thats where your error is


  public String getCustomerName()
    {
        String name = "";
        if ( firstName != null )
           name = name + firstName;
        if ( secondName != null )
           name = name + " " + secondName;
        if ( lastName != null )
           name = name + " " + lastName;
        return name;
    }


this is in a class named "Name" the other class is named CustomerUI and I am trying to print the fullname via the user interface. if the user enters 2.

This post has been edited by sammy123: 01 November 2010 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: User interface class

Posted 01 November 2010 - 11:59 AM

blink.... ok weird... would you mind posting up all your code so i can try to find the error that way?
Was This Post Helpful? 0
  • +
  • -

#7 SarumanTheWhite  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • View blog
  • Posts: 339
  • Joined: 04-November 08

Re: User interface class

Posted 01 November 2010 - 12:04 PM

Try


public void printCustomerInfo()
{
 name = new Name();
 System.out.println(name.getCustomerName());
}



Assuming you haven't already initalized the object and that your constructor doesn't take any parameters. I don't know what your Name class looks like. THat's more than likely the reason name is null.

However this is probably not the best place to initialize Name because then you'll be dealing with default data. Perhaps in the constructor of your CustomerUI class would be best?

This post has been edited by SarumanTheWhite: 01 November 2010 - 12:08 PM

Was This Post Helpful? 2
  • +
  • -

#8 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: User interface class

Posted 01 November 2010 - 12:13 PM

Booking Class


import java.util.Scanner;

public class BookingUI
{
    // instance variables 
    private Scanner keyboard;
    private Booking aBooking;
    private Name name;

    public BookingUI()

    {
        keyboard = new Scanner(System.in);
    }

    /**
     * The top level menu
     */
    public void mainMenu()    
    {
        int command = -1;
        while(command != 0)        
        {
            displayMainMenu();
            command = getCommand();
            executeMain(command);
        }
    }

    /**
     * Displays the instructions for the top level menu
     */
    private void displayMainMenu()
    {
        System.out.println("Options are");
        System.out.println("    Create a new booking     [enter 1] ");
        System.out.println("    print customer name      [enter 2] ");
        System.out.println("    Set the car registration [enter 3] ");
        System.out.println("    Prints the car registration [enter 4] ");
        System.out.println("    Set the room type        [enter 5] ");
        System.out.println ("   Print Booking details    [enter 6] ");
        System.out.println("    End program              [enter 0] ");
               
    }
    /**
     * Receives the specified command
     */
    private int getCommand(){
        System.out.print("Enter command: ");
        return keyboard.nextInt();
    }

    /**
     * Calls the method appropriate to the command received
     */
    private void executeMain(int command)
    {
        if(command == 1)
        {
            createBooking();
        }
        

        else if (command == 2)
        
        {
                printCustomerName() ;
            }
        

        else if(command == 0)
        {
            System.out.println(" Program closing down");
        }
        else
        {
            System.out.println("Unknown command");
        }
    }

    /**
     * Create an account for a customer 
     */
    private void createBooking()
    {
        String title;
        String firstName;
        String lastName;
        String bookingNo;
        String roomType;

        System.out.print("Enter title: ");
        title = keyboard.next();
        System.out.print("Enter first name: ");
        firstName = keyboard.next();
        System.out.print("Enter last name: ");
        lastName = keyboard.next();
        System.out.print("Enter booking number: ");
        bookingNo = keyboard.next();
        System.out.print("Enter room type: ");
        roomType = keyboard.next();

        // Create the Booking object 
        aBooking = new Booking (title, firstName, lastName, bookingNo, roomType);

    }
    
    public void printCustomerName()
    
    {
        System.out.println(name.getCustomerName());
    }

    // remaining methods

}




Please ignore the other command as I ain't added them yet just doing one at a time.
Was This Post Helpful? 0
  • +
  • -

#9 Handler  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 199
  • Joined: 01-April 10

Re: User interface class

Posted 01 November 2010 - 12:27 PM

Ah! SarumanTheWhite is right! you need to initialize name before using it
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9031
  • View blog
  • Posts: 33,506
  • Joined: 27-December 08

Re: User interface class

Posted 01 November 2010 - 12:42 PM

Also, in the future, please post your error message from the compiler, as it makes it easier for us to help you debug. :)
Was This Post Helpful? 0
  • +
  • -

#11 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: User interface class

Posted 01 November 2010 - 12:47 PM

this still does not work now say cannot find symbol - constructor name
Was This Post Helpful? 0
  • +
  • -

#12 SarumanTheWhite  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • View blog
  • Posts: 339
  • Joined: 04-November 08

Re: User interface class

Posted 01 November 2010 - 12:48 PM

What does your Name class look like and what was your change?


Also, it would be helpful to post the EXACT error message

This post has been edited by SarumanTheWhite: 01 November 2010 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#13 sammy123  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 86
  • Joined: 23-October 10

Re: User interface class

Posted 01 November 2010 - 01:09 PM

here is the name class

 */
public class Name
{
    private String firstName;
    private String secondName;
    private String lastName;

    
     */
   
    /**
     *  Constructor for objects of class Name
     *  Only a first name and last name
     */
    public Name(String firstName, String lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    /**
     *  Constructor for objects of class Name
     *  first name, second name and last name
     */
    public Name(String firstName, String secondName, String lastName)
    {
        this.firstName = firstName;
        this.secondName = secondName;
        this.lastName = lastName;
    }

    /**
     * returns the first name
     * 
     * @return     the first name 
     */
    public String getFirstName()
    {
        return firstName;
    }
    
    /**
     * returns the second name
     * 
     * @return     the second name 
     */
    public String getSecondName()
    {
        return secondName;
    }
    
    /**
     * returns the last name
     * 
     * @return     the last name 
     */
    public String getLastName()
    {
        return lastName;
    }
    
    /**
     * set the first name
     * 
     * @param  first   the first name
     */
    public void setFirstName( String firstName )
    {
       this.firstName = firstName;
    }
    
    /**
     * set the second name
     * 
     * @param  second   the second name
     */
    public void setSecondName( String secondName )
    {
       this.secondName = secondName;
    }
    
    /**
     * set the last name
     * 
     * @param  last   the last name
     */
    public void setLastName( String lastName )
    {
       this.lastName = lastName;
    }
    
    /**
     * returns the full name name
     * 
     * @return     the full name name 
     */
    public String getCustomerName()
    {
        String name = "";
        if ( firstName != null )
           name = name + firstName;
        if ( secondName != null )
           name = name + " " + secondName;
        if ( lastName != null )
           name = name + " " + lastName;
        return name;
    }
    
}



This post has been edited by sammy123: 01 November 2010 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9031
  • View blog
  • Posts: 33,506
  • Joined: 27-December 08

Re: User interface class

Posted 01 November 2010 - 01:13 PM

Where do you instantiate the Name class? Where is your exact error message? Contrary to popular belief, our crystal balls do not allow us to see relevant and necessary information you do not post. :)
Was This Post Helpful? 0
  • +
  • -

#15 SarumanTheWhite  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • View blog
  • Posts: 339
  • Joined: 04-November 08

Re: User interface class

Posted 01 November 2010 - 01:14 PM

There's no constructor that takes 0 parameters, that's why. When you initialize the Name class pass in the variables it needs.

At least that's what I'm assuming it is since you haven't shown us what your change was or your error message for that matter.

This post has been edited by SarumanTheWhite: 01 November 2010 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2