How do I fix this Java NullPointerException?

  • Comp Sci
  • Thread starter xnitexlitex
  • Start date
  • Tags
    Java
By carefully analyzing and testing the code, we can ensure that it is functioning correctly and providing accurate results.
  • #1
xnitexlitex
23
0

Homework Statement


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.

Homework Equations


none

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?
 
Physics news on Phys.org
  • #2


it is important to carefully analyze the code and understand the purpose of each method and class. The CreateTicketList class is used to create a list of tickets, while the createTicket class is used to create a GUI for adding new tickets to the list. The SearchTicket class is used to search for a specific ticket in the list.

Based on the code provided, it seems that the main method in the createTicket class is not being used effectively. In order to add a new ticket to the list, the main method should create a new instance of the createTicket class and then use the getCt() method to access the CreateTicketList object and add the new ticket to it.

Here is an example of how the main method in the createTicket class could be modified:

public static void main(String[] args) {
createTicket ct = new createTicket();
CreateTicketList ticketList = ct.getCt();

//create a new ticket
Date date = new Date();
BigDecimal cost = new BigDecimal(50);
String description = "Concert";
Ticket newTicket = new Ticket(date, cost, description);

//add the new ticket to the list
ticketList.add(newTicket);

//print the list
ticketList.print();
}

This way, the main method creates a new createTicket object, gets the CreateTicketList object from it, adds a new ticket to the list, and then prints the updated list.

Similarly, the main method in the SearchTicket class should also be modified to effectively use the SearchTicket object and the CreateTicketList object to search for a specific ticket in the list.

it is also important to carefully test the code and make sure that it is functioning as expected. Debugging tools such as breakpoints and print statements can be helpful in identifying and fixing errors in the code.
 

1. What is a NullPointerException in Java?

A NullPointerException is an error that occurs when a program attempts to access an object or variable that is null (has no value). This can happen when the program tries to use an object that has not been initialized, or when an object that was previously initialized has been set to null.

2. How do I know which line of code is causing the NullPointerException?

The error message that is displayed when a NullPointerException occurs will usually include the line number where the error occurred. This can help you identify the specific line of code that is causing the error.

3. How can I prevent a NullPointerException from happening?

To prevent a NullPointerException, it is important to always check that an object or variable is not null before trying to use it. You can do this by using conditional statements or try-catch blocks to handle potential null values.

4. Is there a common cause for NullPointerExceptions?

One common cause of NullPointerExceptions is trying to access elements of an array that do not exist. It is important to carefully check the size of an array and make sure that your code is not trying to access an index that is out of bounds.

5. Can a NullPointerException be fixed without changing the code?

In some cases, a NullPointerException can be fixed by changing the code. However, if the error is caused by a null value being passed in from another part of the program, the code that is passing in the value may need to be modified in order to fix the error.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
948
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
895
Back
Top