By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

java application error: java.lang.NullPointerException

P: 2
I'm having trouble with a java application. When I try and run the program it shows a java.lang.NullPointerException in the "tic.getClient().getClientTicketList().add(tic );" line (it's in bold). I know it has something to do with an object being null, but I just can't figure out which... Can anyone help? Any suggestions are greatly appreciated :) (By the way, I translated the code to english but I might have forgotten to translate a word, so if there's something that doesn't make sense please tell me so I can fix it. Oh and I didn't include the Ticket class code because it's a lot, but if it's necessary I'll post it)

rentACar Class(only the method that's not working)

Expand|Select|Wrap|Line Numbers
  1. public void makeTicket(){
  2.        String clientNum=JOptionPane.showInputDialog("Client's number");
  3.        if (foundClient(clientNum)){
  4.            findState();
  5.            if (availableCars.size()!=0){
  6.                Transportation vehic=(Transportation)JOptionPane.showInputDialog(null,"Choose vehicle","Vehicles",JOptionPane.PLAIN_MESSAGE,null,availableCars.toArray(), 0);
  7.                int days=0;
  8.                do{
  9.                    try {
  10.                        days=Integer.parseInt(JOptionPane.showInputDialog("Number of days"));
  11.                        if (days<1){
  12.                            JOptionPane.showMessageDialog(null,"ERROR");
  13.                        }
  14.                    }
  15.                    catch(NumberFormatException nFE){
  16.                        JOptionPane.showMessageDialog(null,"ERROR");
  17.                    }
  18.                }
  19.                while (days<1);
  20.                Ticket tic=new Ticket();
  21.                tic.setDays(days);
  22.                tic.setDateTaken(Calendar.getInstance());
  23.                tic.getFinalDate().add(Calendar.DATE, days);
  24.                tic.setState("Not Cancelled");
  25.                tic.setVehicle(vehic);
  26.                tic.setNum(ticketList.size()-1);
  27.  
  28.                if (vehic instanceof notBike){
  29.                    if (((notBike)tic.getVehicle()).getType().equals("Car")){
  30.                        tic.setAmount(days*125);
  31.                    }else{
  32.                        if (((notBike)tic.getVehicle()).getType().equals("Bus")){
  33.                            tic.setAmount(days*200);
  34.                        }else{
  35.                            if (((notBike)tic.getVehicle()).getType().equals("Motorcycle")){
  36.                                tic.setAmount(days*80);
  37.                            }else{
  38.                                tic.setAmount(days*60);
  39.                            }
  40.  
  41.                        }
  42.  
  43.                    }
  44.  
  45.                }else{
  46.                    tic.setAmount(days*30);
  47.                }
  48.  
  49.                for (int i=0; i<clientList.size();i++){
  50.                    if (((Client)clientList.get(i)).getClientNum().equalsIgnoreCase(clientNum)){
  51.                        tic.setClient((Client)clientList.get(i));
  52.  
  53.                        tic.getClient().getClientTicketList().add(tic);
  54.  
  55.                        ticketList.add(tic);
  56.                        i=clientList.size();
  57.                    }
  58.                }
  59.  
  60.            }else{
  61.                JOptionPane.showMessageDialog(null,"There are no vehicles available");
  62.            }
  63.        }else{
  64.            JOptionPane.showMessageDialog(null,"The client does not exist");
  65.        }
  66.  
  67.    }
Client Class

Expand|Select|Wrap|Line Numbers
  1. package project;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public abstract class Client {
  6.     protected String clientNum;
  7.     protected String name;
  8.     protected String tel;
  9.     protected String address;
  10.     protected ArrayList clientTicketList=new ArrayList();
  11.  
  12.     public Client() {
  13.     }
  14.  
  15.     public Client(String clientNum, String name, String tel, String address, ArrayList clientTicketList) {
  16.         this.clientNum = clientNum;
  17.         this.name = name;
  18.         this.tel = tel;
  19.         this.address = address;
  20.         this.clientTicketList = clientTicketList;
  21.     }
  22.  
  23.     public String getClientNum() {
  24.         return clientNum;
  25.     }
  26.  
  27.     public void setClientNum(String clientNum) {
  28.         this.clientNum = clientNum;
  29.     }
  30.  
  31.     public String getName() {
  32.         return name;
  33.     }
  34.  
  35.     public void setName(String name) {
  36.         this.name = name;
  37.     }
  38.  
  39.     public String getTel() {
  40.         return tel;
  41.     }
  42.  
  43.     public void setTel(String tel) {
  44.         this.tel = tel;
  45.     }
  46.  
  47.     public String getAddress() {
  48.         return address;
  49.     }
  50.  
  51.     public void setAddress(String address) {
  52.         this.address = address;
  53.     }
  54.  
  55.     public ArrayList getClientTicketList() {
  56.         return clientTicketList;
  57.     }
  58.  
  59.     public void setClientTicketList(ArrayList clientTicketList) {
  60.         this.clientTicketList = clientTicketList;
  61.     }
  62.  
  63.     public String displayInfo(){
  64.         return "Name: "+name+"\nClient Number: "+clientNum+"\nPhone Number: "+tel;
  65.     }
  66.  
  67.     public String toString(){
  68.         return Name: "+name+"\nClient Number: "+clientNum+"\nPhone Number: "+tel+"\nAddress: "+address;
  69.     }
  70. }
Mar 19 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 539
Can you post the Ticket class?

Maybe you are adding a null value.

Update us...
sukatoa
Mar 19 '08 #2

P: 2
Nevermind I found the problem! thanks! :)
Mar 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.