Java getter无法正常工作?

时间:2013-05-01 11:03:24

标签: java nullpointerexception

好的,所以我有一个comboBox和一个JTextField,每当我选择数量时,它都会显示在文本字段中。我有另一个类,它将检索文本字段内的任何内容,但订单类不会从目录类中检索信息。

class Catalogue extends JPanel {

    String[] h1Quantity = {"0","1","2","3","4","5","6","7","8","9","10"};
    h1CBox = new JComboBox <String> (h1Quantity);
    h1CBox.setSelectedIndex(0);
    h1CBox.addActionListener (new Listener());
    h1CBox.setPreferredSize ( new Dimension (50,30));


    JLabel noBooks = new JLabel ("Quantity");

    booksF = new JTextField(8);


public class Listener implements ActionListener {
        public void actionPerformed (ActionEvent event) {            
            int total = h1CBox.getSelectedIndex();
            booksF.setText(Integer.toString(total));
        }
}


public String booksFText() {
       return booksF.getText();
    }
}


class Order extends JPanel {


   Catalogue catalogue ;

   public Order (Catalogue catalogue)
   {
      this.catalogue = catalogue; 

     JPanel panel = new JPanel (); 
     String text2= catalogue.booksFText();
     textArea1 = new JTextArea (text2, 20, 35);
     add(textArea1);
     add(panel); 
   }

}

我是java的新手,所以请保持简单。非常感谢。

2 个答案:

答案 0 :(得分:2)

Order类中有2个构造函数,而catalogue只在第一个中设置{{1}}。在第二个构造函数中也设置它,NPE应该消失(虽然没有堆栈跟踪很难确定!)

答案 1 :(得分:1)

始终尝试发布完整代码。和堆栈跟踪。

查看您的订单类。

class Order extends JPanel {

   public Order (Catalogue catalogue)
   {
     add(textArea);
   }

}

如果使用第二个构造函数,则不会为类变量目录提供内存。因此NULL POINTER EXCEPTION。第二个构造函数中的代码已移至第一个构造函数中。

另一个原因可能是未正确定义在顺序构造函数中传递的变量。应该这样做。

Catalogue catalogue = new Catalogue();
Order order = new Order(catalogue);

请参阅更新的目录类。

class Catalogue extends JPanel {

    String[] h1Quantity = {"0","1","2","3","4","5","6","7","8","9","10"};
    JComboBox<String> h1CBox ;  //Assuming you forgot to define it.
    JLabel noBooks ;
    JTextField booksF ;


    //Define a new constructor
    public Catalogue () {

      //set jlabel 
      noBooks = new JLabel ("Quantity");      

      //set combobox
      h1CBox = new JComboBox <String> (h1Quantity);
      h1CBox.setSelectedIndex(0);
      h1CBox.addActionListener (new Listener());
      h1CBox.setPreferredSize ( new Dimension (50,30));

      //set textfield 
      booksF = new JTextField(8);  

      //add UI items to your panel class
      add(h1CBox); //combobox
      add(noBooks); // label
      add(booksF); // textfield
    }


public class Listener implements ActionListener {
        public void actionPerformed (ActionEvent event) {            
            int total = h1CBox.getSelectedIndex();
            booksF.setText(Integer.toString(total));
        }
}


public String booksFText() {
       return booksF.getText();
    }
}

始终像这样定义您的UI。当然,有更好的方法。因此代码看起来干净,你理解的东西。学会发表评论,提醒你在某个地方尝试做什么。

主要课程

public class Main {
  static JTextArea textArea = new JTextArea(40,40);

  static class Order extends JPanel{
     public Order(){
        add(textArea);
     }
  }

  static class Catalogue extends JPanel{
    ....

    private ActionListener listener = new ActionListener(){
      public void actionPerformed(ActionEvent event){
              textArea.setText(h1CBox.getSelectedIndex()+"");
       }
    }; 
  }

  public static void main(String args[]){
    //Construct a frame and add panels and you are good to go.
  }

}

最后一个建议是,如果您打算不自行更改textarea的数据,请使用textfield或label而不是textarea。有时设置textarea中的文本,但由于边界不正确,用户无法看到。所以,确保只需用标签或文本字段替换textarea。干杯:)