2012-11-10 54 views
0

基於另一個Headfirst練習,我在使用車輛數據填充GUI時遇到了問題。我使用一個控制器類來管理我的車輛對象類。出於某種原因,我得到一個索引超出範圍例外。將值/字符串返回給JTextField,IndexoutofboundsException

GUI類

public class ShowroomDriver{ 
    public static Showroom Cars = new Showroom("Cars"); 
    public static void main(String[] args) {   
     Showroom Cars = new Showroom("Cars"); 
     Vehicle vechicle1 = new Vehicle("Ford"); 

     Cars.addVehicle(vechicle1); 
     GuiInterface gui = new GuiInterface("Car Showroom"); 
    } 

    private static class GuiInterface extends JFrame { 
     private JButton saleButton, previousButton, nextButton; 
     private static JTextField textField1; 
     private JLabel label1; 
     private JPanel[] p = new JPanel[5]; 
     public GuiInterface(String sTitle) { 
      super(sTitle); 
      setLayout(new FlowLayout()); 
      previousButton = new JButton("Previous Car"); 
      nextButton = new JButton("Next Car"); 
      saleButton = new JButton("Sale");   

      for(int i = 0; i < 5; i++){ 
       p[i] = new JPanel(); 
      } 

      Container contentPane = getContentPane(); 
      contentPane.setLayout(new BorderLayout()); 
      JPanel formPanel = new JPanel(new GridLayout(1, 2)); 


      textField1 = new JTextField(10);    
      label1 = new JLabel("Manufacture"); 

      p[0].add(label1); 
      p[1].add(textField1); 


      for(int i = 0; i < 2; i++){ 
       formPanel.add(p[i]); 
      } 

      contentPane.add(formPanel, BorderLayout.CENTER); 

      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      this.setSize(300,300); 
      this.setResizable(false); 
      this.setLocationRelativeTo(null); 
      getField(); 

      this.setVisible(true); 
     } 

     private void getField(){ 
      textField1.setText(Cars.currentVehicle().getManufacutre()); 
     } 
    } 
} 

控制器類

public class Showroom{ 
    private ArrayList<Vehicle> vehiclesSold = new ArrayList(); 
    private ArrayList<Vehicle> theVehicles; 
    private String vechicleType; 
    private int arrayPosition = 0; 

    public Showroom(String type){ 
     vechicleType = type; 
     theVehicles = new ArrayList<Vehicle>(); 
    } 

    public boolean addVehicle(Vehicle newVehicle){ 
     theVehicles.add(newVehicle); 
     return true; 
    } 

    public Vehicle currentVehicle(){ 
     return theVehicles.get(arrayPosition); 
    } 

    public void getVehicles(){ 
     System.out.println("---Vehicle Type: " + vechicleType +"---"); 
     for(Vehicle nextVehicle : theVehicles){ 
      System.out.println(nextVehicle.toString()); 
     } 
    } 
} 

車輛類別

public class Vehicle{ 
    private String Manufacture 
    Vehicle(String Manufacture){ //There are more 
     this.Manufacture = Manufacture; 
     } 
    } 

    @Override 
    public String toString(){ 
     String s = "Maufacture: " + getManufacutre() 
       "\n"; 
     return s; 
    } 

    public String getManufacutre() { return this.Manufacture; } 
} 
+0

對不起,沒想法,爲了更快的幫助發佈[SSCCE](http://sscce.org/),只顯示了帶有JTextField的isuue,short,runnable,complilable – mKorbel

+0

錯誤是IndexOutOfBoundsException:Index:0,Size :0.即使我知道陣列中有4個。我已經用上面顯示的getVehicles方法證明了它。 – Melky

+0

這個異常可能對我寫的代碼很有趣,看到並在我的屏幕上調試.... – mKorbel

回答

1

沒有更多的代碼,它是不可能要我說出錯誤的來源。但是從這段代碼,一個IndexOutOfBoundsException可以從COM的唯一地方是

return theVehicles.get(arrayPosition); 

你的問題是,arrayPosition是錯誤的。
嘗試調試代碼,找出究竟是什麼出了問題,或發佈更多的代碼

編輯: 你似乎對static關鍵字做什麼誤解。
static對象或方法是在運行時僅實例化的東西。
例如您在class ShowhroomDriver手段Cars屬性的聲明,該類ShowroomDriver有一個名爲Cars一個類屬性(順便說一句 - 不要讓屬性以大寫字母開始這是非常混亂) 。

你想,雖然是什麼的ShowRoom(您Cars屬性)的實例傳遞給你的GuiInterface類通過其構造函數(也去掉static關鍵字出現),像這樣:

// ... 
private Showroom cars; 
public GuiInterface(String sTitle, Showroom cars) { 
    // ... 
    this.cars = cars; 
    // ... 
} 

然後,而不是

private void getField(){ 
    textField1.setText(Cars.currentVehicle().getManufacutre()); 
} 

你寫

private void getField(){ 
    textField1.setText(this.cars.currentVehicle().getManufacutre()); 
} 

同樣刪除全部static關鍵字,除了在main方法的關鍵字。

+0

arrayPosition默認設置爲0.我知道Vehicle存在於該位置。看到我添加的新方法。 – Melky

+0

'getField()'是靜態的。這很可能是問題所在。刪除它,它應該工作 – Chris

+0

可悲的是沒有改變,只要說出你需要什麼代碼,我會發布它,因爲它有很大的優點。 – Melky