Results 1 to 9 of 9
  1. #1
    reader34 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default Close but help would be grateful

    Hey guys i am learning java at college and i have a task to do.. basically my task is to create a airline booking system, this consists of:

    Please select from the following options:

    1- Book a luxury seat.
    2 Book an executive seat.
    3- Display seats/availability
    4- Exit

    Now the code can book a luxury seat ( though i have only created one for the timebeing). When I enter in two types of information from two different customers, I press option 2 and it asks for name( I want the name to find the name that was entered before binded to that seat) It will throw a wobbley saying " NullpointerException:null" at the if (tks [ count] = 0; count < ticketTot; count ++. I think i know why this is happening because its trying to show a int but its a String and its that what I want to show a String, is there anyway of getting around this problem?
    Reader34

    Java Code:
    import java.util.Scanner;
    class Libary 
    {
            static final  int quit = 0;
            
                public static int getMenuOpt()
                {
                     
                    
                    Scanner menuOpt = new Scanner (System.in);
                    int option;
                    
                    System.out.println("Welcome to CCN jet");
                    System.out.println("Please select a option below.");
                   
                       System.out.println("------------------------------");
                        System.out.println("1.Book a luxury seat");
           
                       System.out.println("2. list bookings ");
              
                     
                                System.out.println(quit + ".Quit");
                                
                                option  = menuOpt.nextInt();
                                return option;
                                
                            }
                            
                            public static int addTicket (Ticket tks [], int ticketTot)
                            {
                                Scanner input = new Scanner (System.in);
                                String name;
                                String seatClass;
                       
                  
                          
                                
                              
                               System.out.println("You have chosen to luxury");
                    
                            
                                   
                                System.out.print ("Enter your name ");
                                name = input.nextLine();
                                System.out.print("Please comfirm booking type , Enter Luxury");
                                seatClass = input.nextLine();
                            
                               
                                
                                System.out.println("thank you your details are :"+ name + seatClass );
                                tks [ticketTot] = new Ticket( name, seatClass);
                                ticketTot++;
                                return ticketTot;      
                                
                               
                                
                                
    
                                
                   
                                
                            }
                               
                            public static void listByName (Ticket tks [], int ticketTot)
                            {
                                
                                
                                Scanner input = new Scanner (System.in);
                              
                                String name;
                                String seatClass;
                                System.out.print("Enter your name");
                                name = input.next();
                                
                                for  ( int count = 0 ; count < ticketTot;  count ++ )
                                {
    
                                    System.out.println( tks [count].getName ());
                                    System.out.println(tks [count].getSeatClass ());
                                    
                                       
                                        
    
                                                
                                          
                                        }
                               
             
                                }
                                public static void main (String [] args)
                                {
                                   
                                   Scanner input = new Scanner (System.in);
                                    
    
                               
                                    Ticket tickets [] = new Ticket [5]; 
                                
                            
                                    int ticketsInLibrary = 1;
                                    int option;
                                    int seatsInLibrary=1;
                                  
                                    
                                    
                                    
                                    option = getMenuOpt();
                                    
                                   
                     
                                    
                                    while (option != quit)
                                    {
                                        if (option == 1)
                                            ticketsInLibrary = addTicket (tickets, ticketsInLibrary );
                                            
                                            
                                  
                                            
                                          if (option == 2 )
                                           
                                            
                                            listByName ( tickets, seatsInLibrary);
                                                System.out.println(" Seat number " + seatsInLibrary++  );
                                          
                                                
                                          
                                                    
                                           
                           
                                             
                                                
                                                option = getMenuOpt();
                                            }
    
                                        }
    
                                    }
    
    
                                        
                                               
                                    
                                    
                                    
                                
                                
                                
    import java.util.Scanner;
    
    class Ticket
    {
            
        
                private String name;
                private String seatClass;
    
                
               
           
                
            
                
                Ticket ( String  n, String s)
                {
                    
                    name = n;
                    seatClass = s;
    
                  
                    
                 
    
                }
                
                            
                    String getName ()
                    {
                        return name;
                        
                    }
                    
                    String  getSeatClass()
                    
                    { return seatClass;
                        
                    }
                    
                  
                    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

    Default

    Can you bit clear it up?

    Do you want to convert a string into an int to fix this problem? How can NullPointerException solve by that? It's better if you can copy-paste here full error message and the inputs you used.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    8

    Default

    Array index start from 0, not 1....
    how can you add ticket to index 1 and remain index 0 as null?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    8

    Default

    Practice only

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

  7. #7
    reader34 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Ahhh thanks i changed both my values in the array from = 1 to = 0 and now when i enter in a name of someone who has booked a ticket there name type and seat number comes up. I have one question if anybody doesn't mind helping? I want to show a user how many seats are left and taken, would i use some sort of loop to do this? like count every seat and then show whats left.
    thanks
    reader34

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default Arrays?

    You can use an array to keep track of the seats. Since you're using arrays in your code, I'll assume you know the basics and don't need a tutorial, but here's the Array API so you can see all the methods that can be used:
    Array (Java Platform SE 6)
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. [SOLVED] Close Exe
    By smartsubroto in forum New To Java
    Replies: 11
    Last Post: 10-14-2008, 08:04 AM
  2. Close Active Window (?)
    By dcnorman07 in forum New To Java
    Replies: 2
    Last Post: 10-03-2008, 05:55 PM
  3. close a frame..
    By tajinvillage in forum New To Java
    Replies: 5
    Last Post: 04-27-2008, 10:22 PM
  4. Frame close operation
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 09:39 AM
  5. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •