2013-10-02 45 views
0

请参阅this link更新 - 更好的格式! - 问题的版本。JTextField忽略nextLine()? [第二部分]


所以这是从first question的后续行动。我采纳了porfiriopartida的建议,并将用户界面的逻辑分开,并将逻辑放入一个名为Simulator的单独文件中。因此,这里的GUI程序的外观如何,现在:

import java.util.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class GUI extends JPanel implements ActionListener { 
    LinkedList<FoodItem> foodItemList = new LinkedList<FoodItem>(); 
    CategoryList<String> categoryList = new CategoryList<String>(); 
    Scanner in = new Scanner(System.in); 
    protected JButton[] buttons = new JButton[6]; 
    String[] buttonNames = {"Add Item", "Remove Item", "Search for Item", "Update Item", "Check if List is Empty", "Print Items"}; 
    String[] buttonCommands = {"add", "remove", "search", "update", "isEmpty", "print"}; 
    protected JTextField textField; 
    public GUI() { 
     textField = new JTextField(20); 
     textField.addActionListener(this); 
     add(textField); 
     for (int i = 0; i < 6; i++) { 
      buttons[i] = new JButton(buttonNames[i]); 
      buttons[i].setActionCommand(buttonCommands[i]); 
      buttons[i].setVerticalTextPosition(AbstractButton.CENTER); 
      buttons[i].setHorizontalTextPosition(AbstractButton.LEADING); 
      buttons[i].addActionListener(this); 
      add(buttons[i]); 
     } 
    } 
    public void actionPerformed(ActionEvent e) { 
     String command = e.getActionCommand(); 
     Simulator helper = new Simulator(textField); 
     if (command.equals(buttonCommands[0])) 
      helper.add(foodItemList); 
     else if (command.equals(buttonCommands[1])) 
      helper.remove(foodItemList); 
     else if (command.equals(buttonCommands[2])) 
      helper.search(foodItemList); 
     else if (command.equals(buttonCommands[3])) 
      helper.update(foodItemList); 
     else if (command.equals(buttonCommands[4])) 
      helper.isEmpty(foodItemList); 
     else 
      helper.print(foodItemList); 
    } 
    public static void createAndShowGUI() { 
     JFrame frame = new JFrame("Vendor Interface"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     GUI newContentPane = new GUI(); 
     newContentPane.setOpaque(true); 
     frame.setContentPane(newContentPane); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

这是Simulator程序:

import javax.swing.JTextField; 
import javax.swing.text.JTextComponent; 

public class Simulator { 
    private JTextComponent tf; 
    public Simulator(JTextField textField) { 
     this.tf = textField; 
    } 
    public void add(LinkedList<FoodItem> fi) { 
     System.out.println("Please enter the name of the product you want to add."); 
     String text = tf.getText(); 
     try { 
      if (fi.containsName(text)) 
       throw new DuplicateProductException(); 
      else 
       fi.add(enterFoodItem(text)); 
     } 
     catch(DuplicateProductException ex) { 
      System.out.println("The product has already been added to the list."); 
      System.out.println("Would you like to update or remove this item? (U for update, R for remove, N for neither)"); 
      String response = tf.getText(); 
      if (response.equals("U")) 
       update(fi); 
      if (response.equals("R")) 
       remove(fi); 
     } 
    } 
    public void remove(LinkedList<FoodItem> fi, String s) { 
     if (fi.size() == 1) 
      fi.list = null; 
     else 
      fi.oneBeforeContainingName(s).setLink(fi.oneBeforeContainingName(s).getLink().getLink()); 
     System.out.println("The product has been removed."); 
    } 

    public void remove(LinkedList<FoodItem> fi) { 
     System.out.println("Please enter the name of the product you want to remove."); 
     String text = tf.getText(); 
     try { 
      if (fi.containsName(text)) 
       remove(fi, text); 
      else 
       throw new NonexistentProductException(); 
     } 
     catch(NonexistentProductException ex) { 
      System.out.println("The product does not exist in the list."); 
      System.out.println("Would you like to add this product? (Y/N)"); 
      String response = tf.getText(); 
      if (response.equals("Y")) 
       add(fi); 
     } 
    } 
    public void search(LinkedList<FoodItem> fi) { 
     System.out.println("Please enter the name of the product you want to search for."); 
     String text = tf.getText(); 
     if (fi.containsName(text)) { 
      System.out.println("The product is found."); 
      System.out.println("Would you like to print it or update it? (P for print, U for update, N for neither)"); 
      String response = tf.getText(); 
      if (response.equals("P")) 
       print(fi, text); 
      if (response.equals("U")) 
       update(fi, text); 
     } 
     else 
      System.out.println("The product does not exist in the list."); 
    } 
    public void update(LinkedList<FoodItem> fi, String s){ 
     fi.nodeContainingName(s).setElement(enterFoodItem(s)); 
    } 
    public void update(LinkedList<FoodItem> fi) { 
     System.out.println("Please enter the name of the product you want to update."); 
     String text = tf.getText(); 
     try { 
      if (fi.containsName(text)) 
       update(fi, text); 
      else 
       throw new NonexistentProductException(); 
     } 
     catch(NonexistentProductException ex) { 
      System.out.println("The product does not exist in the list."); 
      System.out.println("Would you like to add this product? (Y/N)"); 
      String response = tf.getText(); 
      if (response.equals("Y")) 
       add(fi); 
     } 
    } 
    public void isEmpty(LinkedList<FoodItem> fi) { 
     if (fi.isEmpty()) 
      System.out.println("The list is empty."); 
     else 
      System.out.println("The list is not empty."); 
    } 
    public void print(LinkedList<FoodItem> fi, String s){ 
     LinkedListNode<FoodItem> temp = fi.list; 
     while (!temp.getElement().name.equals(s)) 
      temp = temp.getLink(); 
     System.out.println(temp.getElement().toString()); 
    } 
    public void print(LinkedList<FoodItem> fi) { 
     if (fi.isEmpty()) 
      System.out.println("The list is empty."); 
     else 
      System.out.println("Here is the list:" + fi.toString()); 
    } 
    public FoodItem enterFoodItem(String input) { 
     String[] requiredEntry = {"price", "quantity", "description", "size", "special order", "category"}; 
     double p = 0.0; 
     int q = 0; 
     String d = ""; 
     double s = 0.0; 
     String so = ""; 
     String c = ""; 
     for (int i = 0; i < 6; i++) { 
      System.out.println("Please enter the " + requiredEntry[i] + " of the product."); 
      if (i == 0) 
       p = Double.parseDouble(tf.getText()); 
      if (i == 1) 
       q = Integer.parseInt(tf.getText()); 
      if (i == 2) 
       for (int j = 0; j < 2; j++) 
        d = tf.getText(); 
      if (i == 3) 
       s = Double.parseDouble(tf.getText()); 
      if (i == 4) 
       for (int j = 0; j < 2; j++) 
        so = tf.getText();; 
      if (i == 5) 
       c = tf.getText(); 
     } 
     return new FoodItem(input,p,q,d,s,so,c); 
    } 

    public class DuplicateProductException extends Exception { 
     public DuplicateProductException() {;} 
    } 
    public class NonexistentProductException extends Exception { 
     public NonexistentProductException() {;} 
    } 
} 

当我运行程序,之后我按01​​按钮,它说:“请输入要添加的产品的名称。“ (在控制台上),但即使是这样我才可以输入任何东西到文本框中,它会立即显示此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at Simulator.add(Simulator.java:12) 
    at GUI.actionPerformed(GUI.java:31) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$200(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

这是怎么回事这个时间,我怎么得到这个固定的?

+2

这是疯狂的代码,将变量设置为null,然后尝试使用它。 –

+0

为了尽快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 –

回答

2

代码包括:

JTextComponent tf = null; 
String text = tf.getText(); 

你不能叫一个空值对象的一个​​方法。这导致NullPointerException

也许你想从现有的JTextField文:当你创建一个新的JTextField参考与JTextComponent tf = null;

String text=tf.getText(); 

,你影子的实例变量与局部变量,使其无法访问。

编辑:在你的构造函数中,请参阅Reimus对其他(同样重要的)事情错误的回答。

2

你在Simulator的构造函数中隐藏着textField。更换

JTextField tf = textField; 

tf = textField; 
0

当你使用这样的:

String text = tf.getText(); //null.getText() !! 

public void add(LinkedList<FoodItem> fi) { 
    System.out.println("Please enter the name of the product you want to add."); 
    JTextComponent tf = null; // tf points to NULL, you might remove this line. 
    String text = tf.getText(); 

你的TF参考值设置为空,后立即与抛出异常在这里,你正在设置文本字段构造函数中的局部变量,但全局变量设置为null。

private JTextComponent tf;公共模拟器(JTextField textField){ JTextField tf = textField; } 将其更改为:

private JTextComponent tf; 
    public Simulator(JTextField textField) { 
    //you don't need "this" but that way you make sure you refeer to the class 
    this.tf = textField; variable. 
    } 

我不知道这是否足够,因为代码不编译原样。

  • CategoryList类缺失。
  • FoodItem类缺失。
  • LinkedListNode类缺失。

...

public void print(LinkedList<FoodItem> fi, String s) { 
    LinkedListNode<FoodItem> temp = fi.list; 

我不认为这是一个保证,LinkedList的对象总是一个LinkedListNode的实例;最有可能你应该使用转换或接收LinkedListNode作为参数而不是LinkedList。

+0

感谢您的帮助!它仍然有不让我输入任何值的几乎相同的错误。 顺便说一句,你怎么知道,也有应该是一个'CategoryList'类? o_o – 2012ssohn

+0

因为我复制了你的代码,并且它不能被编译,因为我没有你在那里引用的那3个类。 – porfiriopartida

+0

哦,我忘了那个。当我准备好实现时,它是一种占位符,并没有在代码中做任何事情。 – 2012ssohn