Exception in thread "main" java.lang.NullPointerException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    Exception in thread "main" java.lang.NullPointerException

    Hi everyone;
    My Class has ended and I was not able to solve this problem in time, and I would still like to solve it.
    I got these error code.
    Exception in thread "main" java.lang.NullP ointerException
    at ticketSales.Tic ketSales.makeEv ent(TicketSales .java:185)
    at ticketSales.Tic ketInput.main(T icketInput.java :56)

    Anyway I have several Class

    This one is called TransAction
    Code:
    public  class TransAction {
    	   private TicketSales makeEvent;
    	   private BuyerClass makeBuyer;
    	   public TransAction(TicketSales ticket, BuyerClass buyer) {
    	      this.makeEvent= ticket;
    	      this.makeBuyer= buyer;
    	   }
    	   public TicketSales getTicket() { return makeEvent; }
    	   public BuyerClass getBuyer() { return makeBuyer; }
    }
    This is part of my TicketSales Class This is the main section which has all the data types . which has error code
    Line 185 is this:
    System.out.prin t("Enter Event ID Number (CL123, DL123, PL123) ==>");
    id = kbd.next();

    Code:
    public class TicketSales {
    
    	static Scanner kbd;
    
    	public static EventClass makeEvent() {
    		EventClass ticket = null;
    
    		// prompt for data
    		String id;
    		String en;
    		String ed;
    		String vt;
    		String sl;
    		double pr;
    
    		System.out.print("Enter Event ID Number (CL123, DL123, PL123) ==>");
    		id = kbd.next();
    
    		System.out.print("Enter Event Name (Concert, Dinner, Play) ==>");
    		en = kbd.next();
    
    		System.out.print("Enter Event Date (05/12/2007) ==>");
    		ed = kbd.next();
    
    		System.out.print("Enter Event Time (4:00pm==>");
    		vt = kbd.next();
    
    		System.out.print("Enter Seatlayout (3500, 2000, 1500,==>");
    		sl = kbd.next();
    
    		System.out.print("Enter Price (100.00)==>");
    		pr = kbd.nextDouble();
    
    		// make an object
    		ticket = new EventClass(id, en, ed, vt, sl, pr);
    
    		return ticket;
    	}
    
    }// close TicketSales
    This is my TicketInput
    This has error code which is at
    EventClass ticket = TicketSales.mak eEvent();
    Code:
    class TicketInput {
    
    	public static void main(String args[]) {
    
    		// make array list object
    
    		ArrayList<EventClass> arlist = new ArrayList<EventClass>();
    		arlist.add(new EventClass("CL123", "Concert", "05/12/2007", "12:00pm", "3500 Seats", 55.00));
    		arlist.add(new EventClass("PL123", "Play", "06/14/2007", "2:00pm", "2000 Seats", 75.00));
    		arlist.add(new EventClass("DL123", "Dinner", "05/29/2007", "7:00pm","1500 Seats", 100.00));
    		//arlist.addAll((Collection<? extends EventClass>) new EventClass("DL123", "Dinner", "05/29/2007", "7:00pm", 200.00, "Dan Smith", "1234 East ST Fullerton, CA 90000", "Row 1 Seat 14, 15"));
    	//arlist.addAll((Collection<? extends EventClass>) new EventClass("PL123", "Play", "06/14/2007", "2:00pm", 150.00, "Jon Doe", "1234 East ST", "Row 12 Seat 18, 19"));
    		System.out.println(arlist);
    
    
    		// make a scanner
    		Scanner kbd = new Scanner(System.in);
    
    		int choice;
    		System.out.println("Make a Section: ");
    		System.out.println("1. Enter Event ");
    		System.out.println("2. Enter Buyer Info ");
    		System.out.println("3. Print Event ");
    		System.out.println("4. Print Buyer Inof ");
    		System.out.println("5. Exit this Program ");
    		System.out.print("\nPlease press Enter afer each response");
    		System.out.println("\nEnter your choose please: ");
    		choice = kbd.nextInt();
    		kbd.nextLine();
    		if (choice == 1) { // if 1 is select go to makeEvent
    
    			boolean endData = false;
    
    			while (!endData) {
    				EventClass ticket = TicketSales.makeEvent();
    				arlist.add(ticket);
    				System.out.println("Add More Events (Y/N)-->");
    
    				String ans = kbd.next();
    
    				if (ans.equalsIgnoreCase("N")) {
    					System.out.println(ticket);
    					endData = true;
    				}
    			}// close while loop
    		}// close choice 1
    ....
    Any help would be great. Each Class is it open file.

    nomad
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    I just quickly browsed through your code but have you instantiated your 'kbd'
    scanner? I couldn't find it (but I may be wrong of course).

    kind regards,

    Jos

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Originally posted by JosAH
      I just quickly browsed through your code but have you instantiated your 'kbd'
      scanner? I couldn't find it (but I may be wrong of course).

      kind regards,

      Jos
      That was it Jos....
      Thanks. I looked at this for about 45 mins. Looked at the arraylist and pointers.
      Darn...
      I wish I could turn it in some how.
      He has the assignments uploading locked.

      nomad

      Comment

      • nomad
        Recognized Expert Contributor
        • Mar 2007
        • 664

        #4
        Originally posted by JosAH
        I just quickly browsed through your code but have you instantiated your 'kbd'
        scanner? I couldn't find it (but I may be wrong of course).

        kind regards,

        Jos
        Don't know if I should be PMs you.
        need help with this part for the article.

        Code:
        for( int i=0; i<arlist.size(); i++) {
        PersonClass e = arlist.get( i );
        System.out.println(“The info  is “+e.getEmpID(), +e.getLname() );
        }
        Can not get e.getLname() to print.
        here is the top section of class
        Code:
        class PersonClass {
            private String empid;
            private String name;
            public PersonClass(String id) {
        		empid = id;
            }
        
            public PersonClass(String id, String name) {
                this.empid = id;
                this.name = name;
            }
        
            public String getID() {return empid;}
            public String getName() {return name;}
        
            public String toString() {
                return "(" + empid + ", " + name + ")";
            }
        thanks
        nomad

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Maybe it's just me again but I don't see a getLname() method in your Person class.

          kind regards,

          Jos

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by JosAH
            Maybe it's just me again but I don't see a getLname() method in your Person class.

            kind regards,

            Jos
            Got rid of the L in getName() in the method. but it still does not work. I get an error on
            The operator + is undefined for the argument type(s) String

            thanks
            nomad

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by nomad
              Got rid of the L in getName() in the method. but it still does not work. I get an error on
              The operator + is undefined for the argument type(s) String

              thanks
              nomad
              Can't be; the '+' operator is overloaded for at least one String operand. What
              is the exact error diagnostic message and the relevant code?

              kind regards,

              Jos

              Comment

              • nomad
                Recognized Expert Contributor
                • Mar 2007
                • 664

                #8
                Originally posted by JosAH
                Can't be; the '+' operator is overloaded for at least one String operand. What
                is the exact error diagnostic message and the relevant code?

                kind regards,

                Jos
                Jos
                this is the only error I get.
                The operator + is undefined for the argument type(s) String

                Ps Im using eclipe...
                +e.getName() ); is underline with red.
                it has to be comma after
                System.out.prin tln("The info is "+e.getID() ,
                if I get rid of the comma it works.

                nomad
                or I could just do this
                System.out.prin tln("Event info is " + e);

                But still would like to know what there is an error.
                nomad

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by nomad
                  Jos
                  this is the only error I get.
                  The operator + is undefined for the argument type(s) String

                  Ps Im using eclipe...
                  +e.getName() ); is underline with red.
                  it has to be comma after
                  System.out.prin tln("The info is "+e.getID() ,
                  if I get rid of the comma it works.

                  nomad
                  or I could just do this
                  System.out.prin tln("Event info is " + e);

                  But still would like to know what there is an error.
                  nomad
                  Can you show exactly what you're trying to do? And what's that comma doing there?

                  kind regards,

                  Jos

                  Comment

                  • nomad
                    Recognized Expert Contributor
                    • Mar 2007
                    • 664

                    #10
                    Originally posted by JosAH
                    Can you show exactly what you're trying to do? And what's that comma doing there?

                    kind regards,

                    Jos
                    would like to print id number and name.
                    Comma is just a seperator between id number and name
                    ie
                    ds, James R Dean.

                    I need to do it this way instead
                    System.out.prin tln(" Id is " + e.getID());
                    System.out.prin tln("Name is " + e.getName());

                    Thanks.
                    nomad

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by nomad
                      would like to print id number and name.
                      Comma is just a seperator between id number and name
                      ie
                      ds, James R Dean.

                      I need to do it this way instead
                      System.out.prin tln(" Id is " + e.getID());
                      System.out.prin tln("Name is " + e.getName());

                      Thanks.
                      nomad
                      Just print the comma in between; it's just text:[code=java]
                      System.out.prin tln(e.getID()+" , "+e.getName ());[/code]
                      kind regards,

                      Jos

                      Comment

                      • nomad
                        Recognized Expert Contributor
                        • Mar 2007
                        • 664

                        #12
                        Originally posted by JosAH
                        Just print the comma in between; it's just text:[code=java]
                        System.out.prin tln(e.getID()+" , "+e.getName ());[/code]
                        kind regards,

                        Jos
                        I tried that but I forgot to add the + sign before ","
                        darn

                        nomad
                        Thanks a million

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          You're welcome. Problem solved now?

                          kind regards,

                          Jos

                          Comment

                          Working...