2013-11-04 69 views
2

我想找出一种方法来打印到JTextArea而不会引发异常。目前我正在抛出一个NullPointerException,但是当我尝试打印到JTextArea时。我不确定它出错的地方或原因。有人请帮助我。谢谢。试图打印到JTextArea

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<String> queue = new LinkedList<String>(); 
       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<String> ticketList = ticketList(); 

       while(queue.size() > 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<String> ticketList() { 
    Stack<String> tickets = new Stack<String>(); 
    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; 
} 
} 

在那里我有我没有任何问题,当我开关(t)左右,除了它不会在它打印出在Eclipse本身的结果JTextArea中打印的其他方式的行情。

+0

后整个例外(包括完整的调用堆栈) –

+0

解决,这是** **很多比实际的解决方案更重要的过程,因为你肯定会再次遇到NPE。学习的教训是仔细观察投掷NPE的线上使用的变量。其中一个是空的,一旦你发现,回溯你的代码,找出原因。 –

回答

6

你是shadowing变量textArea。更换

JTextArea textArea = new JTextArea(); 

textArea = new JTextArea(); 
+0

谢谢!这让我很头疼。 – user2929005

3

你永远不会初始化实例变量textArea,而是你声明并初始化一个地方之一:

JTextArea textArea = new JTextArea(); 

更改该行:

textArea = new JTextArea(); 
+0

谢谢你的帮助! – user2929005

-1

nextButton.addActionListener(新的ActionListener()) 前textarea的初始化; -__-

+0

这与手头的问题无关。 –