HI I was wondering what is wrong with this piece of code as it compiles but then
it prints java.lang.null exception in the terminal. and it prompts the red line. I am basically trying to create a ticketoffice class that would store information of 3 users who are only allowed to purchase tickets events for 3 events at most.


question 2:
how do i write a while loop for a switch statement that would terminates when say a command 'f' is entered and also a few other commands.

question 3:
I am supposed to store customer in a SortedLinkedList, which I already did...but I would need a while loop to check to see if the customer being input into the terminal is one of the existing customer and the number of tickets purchase are not more than the available tickets for events.
how do I do that??

public TicketOffice()throws IOException
      {

        Scanner inFile = new Scanner(new FileReader("H:\\csc2011\\data.txt"));
        int numOfEvents = 0;
        int numOfClients = 0;
        int tickets = 0;
        numOfEvents = inFile.nextInt();
        inFile.nextLine();

        for (int i=1; i<= numOfEvents; i++)
        {
            String name = inFile.nextLine();
            tickets = inFile.nextInt();
            Event e = new Event(name, tickets);
            lstEvent.add(e);
        }

        Iterator iter = lstEvent.iterator();

        while(iter.hasNext())
        {
            System.out.println((Event) iter.next());
        }

    }

many thanks!!

Is it possible that 'lstEvent' is null? That is where you should start with any NullPointerException.

Is it possible that 'lstEvent' is null? That is where you should start with any NullPointerException.

Sorry.. i don't follow... 'lstEvent' is a list of events that I'm trying to read off the data.txt using a scanner but the scanner wouldnt run..

the data.txt
are as follow, there's 6 events, and then followed by the first event (TENNIS) which has 8 tickets left, and last 3 names are the registered customer.

6
Tennis
8
Athletics
4
Handball
66
Equestrian Jumping
7
Football
2
Volleyball
41
3
Emma Williams
Anna Smith
John Williams

You miss my point. A NullPointException occurs when you attempt to call a method on or otherwise access an object reference that has not been initialized. I can guess pretty easily what 'lstEvents' is but that doesn't address whether or not that you initialized it. You need to verify that.

Member Avatar for hfx642

Example...

JLabel My_Label;
My_Label = new JLabel ();
My_Label.setText ("Label Text");

Line #1 declares/defines the JLabel.
Line #2 initializes/constructs/creates the JLabel.
Line #3 sets the text for the JLabel.
If you didn't have line #2, line #3 would NOT work.
Most people do it all in one line.

JLabel My_Label = new JLabel ("Label Text");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.