Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to print to JTextArea
    primarykey
    data
    text
    <p>I am trying to figure out a way to print to a JTextArea without throwing an exception. Currently I am throwing a NullPointerException but on when I try to print to the JTextArea. I am not sure where it goes wrong or why. Someone please help me. Thank you.</p> <pre><code> import java.awt.EventQueue; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.SwingConstants; import javax.swing.JTextField; import javax.swing.JScrollBar; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JTextArea; public class TicketLine { private JFrame frame; private JTextArea textArea; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TicketLine window = new TicketLine(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public TicketLine() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JButton nextButton = new JButton("Next Person"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Queue&lt;String&gt; queue = new LinkedList&lt;String&gt;(); queue.offer("Megan"); queue.offer("Kate"); queue.offer("Conan"); queue.offer("Jay"); queue.offer("Bert"); queue.offer("Ernie"); queue.offer("Mickey"); queue.offer("Goofy"); queue.offer("Optimus"); queue.offer("Megatron"); Stack&lt;String&gt; ticketList = ticketList(); while(queue.size() &gt; 0) // System.out.println(queue.remove() + " wins tickets to " + ticketList.pop()); // NullPointerException HAPPENS HERE! textArea.setText(queue.remove() + "wins tickets to " + ticketList.pop()); } }); nextButton.setBounds(165, 197, 106, 23); frame.getContentPane().add(nextButton); JLabel lblNewLabel = new JLabel("To Display Next Winner Press the Button"); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setBounds(90, 156, 245, 30); frame.getContentPane().add(lblNewLabel); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(0, 0, 2, 2); frame.getContentPane().add(scrollPane); JTextArea textArea = new JTextArea(); textArea.setBounds(90, 38, 245, 91); frame.getContentPane().add(textArea); } public static Stack&lt;String&gt; ticketList() { Stack&lt;String&gt; tickets = new Stack&lt;String&gt;(); tickets.push("Olympus Has Fallen"); tickets.push("Jurassic Park"); tickets.push("The Patriot"); tickets.push("Matrix"); tickets.push("Gettysburg"); tickets.push("Gods and Generals"); tickets.push("White House Down"); tickets.add((int) (Math.random() * 5), "Star Wars"); tickets.add((int) (Math.random() * 5), "Star Wars"); tickets.add((int) (Math.random() * 5), "Star Wars"); return tickets; } } </code></pre> <p>Where I have the Quotes I don't have any issues when I switch t around the other way except that it wont print in the JTextArea it prints out the results in Eclipse itself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload