1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How do I fix this Java NullPointerException?

  1. Dec 14, 2011 #1
    1. The problem statement, all variables and given/known data
    There are three classes:

    import java.math.BigDecimal;
    import java.util.Date;

    import javax.swing.JOptionPane;

    public class CreateTicketList {
    private ArrayObj aObj;
    public CreateTicketList() {
    aObj = new ArrayObj(new Ticket(), 100);
    }

    public void add(Ticket t1) {
    if(aObj.isFull()) {
    JOptionPane.showMessageDialog(null,"The list is full.");
    } else {
    aObj.add(t1);
    }
    }

    public void print() {
    for (int i=0; i<aObj.getCount(); i++) {
    Ticket rt = (Ticket) aObj.findAtIndex(i);
    System.out.println(rt.toString());
    }
    }

    public ArrayObj getaObj() {
    return aObj;
    }

    public void setaObj(ArrayObj aObj) {
    this.aObj = aObj;
    }
    }

    import javax.swing.*;
    import java.awt.FlowLayout;
    import java.math.BigDecimal;
    import java.util.Date;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    //inherited abstract method must be used

    public class createTicket extends JFrame implements ActionListener {
    private CreateTicketList ct = new CreateTicketList();
    private JTextField descTicket;
    private Date today;
    private JTextField costTicket;
    public int t1;
    public createTicket() {
    super("createTicket");
    today = new Date();
    FlowLayout experimentLayout = new FlowLayout();
    experimentLayout.setAlignment(FlowLayout.CENTER);
    JPanel panel = new JPanel();
    panel.setLayout(experimentLayout);
    descTicket = new JTextField(15);
    costTicket = new JTextField(5);
    JLabel todaylbl = new JLabel(today.toString());
    JLabel desclbl = new JLabel("Ticket Description");
    JLabel costlbl = new JLabel("Cost");
    JButton submit = new JButton("Add Ticket");
    submit.addActionListener(this);
    panel.add(todaylbl);
    panel.add(desclbl);
    panel.add(descTicket);
    panel.add(costlbl);
    panel.add(costTicket);
    panel.add(submit);
    add(panel);
    pack();
    //pack confirms GUI details
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
    Date t;
    BigDecimal c1 = new BigDecimal(costTicket.getText());
    String descript1 = descTicket.getText();

    t = today;
    Ticket t1 = new Ticket (t,c1,descript1);
    ct.add(t1);
    ct.print();
    }

    public CreateTicketList getCt() {
    Ticket t1 = new Ticket ();
    ct.add(t1);
    return ct;
    }
    public void setCt(CreateTicketList ct) {
    this.ct = ct;
    }
    public static void main(String[] args) {
    createTicket ct = new createTicket ();
    ct.getCt().print();
    }
    }

    import javax.swing.*;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.math.BigDecimal;
    import java.util.Date;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class SearchTicket extends JFrame implements ActionListener {
    private CreateTicketList tlist = new CreateTicketList();
    private JTextField search;
    private JButton searchButton;
    private createTicket createT;
    private String ticketNo;

    public SearchTicket () {
    super ("SearchTicket");
    FlowLayout experimentLayout = new FlowLayout();
    experimentLayout.setAlignment(FlowLayout.CENTER);
    JPanel panel = new JPanel();
    panel.setLayout(experimentLayout);
    JLabel searchlbl = new JLabel("Enter ticket Number: ");
    search = new JTextField (10);
    JButton searchButton = new JButton("Find Ticket");
    searchButton.addActionListener(this);
    panel.add(searchlbl);
    panel.add(search);
    panel.add(searchButton);
    add(panel);
    pack();
    //pack confirms GUI details
    setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    Ticket t = new Ticket();
    Date d1 = new Date();
    BigDecimal c1 = new BigDecimal(100);
    String descript1 = "Food";
    Ticket t1 = new Ticket(d1,c1,descript1);
    ticketNo = search.getText();
    long tNo = Long.parseLong(ticketNo);
    createT.getCt().add(t1);
    createT.getCt().print();


    }
    public static void main(String [] args) {
    SearchTicket st = new SearchTicket();

    }
    }

    Plus, I have a main class to connecting createTicket and SearchTicket.
    2. Relevant equations
    none

    3. The attempt at a solution
    I looked at where the exception occurred which was in createT.getCt().add(t1);. I figured that getCt was missing an add method, so I added two lines to refer to add(t1), which were:

    Ticket t1 = new Ticket ();
    ct.add(t1);

    That didn't work. What else am I missing?
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?