2012-11-10 4 views
0

다른 Headfirst 연습을 기반으로 차량의 데이터로 GUI를 채우는 데 문제가 있습니다. Controller 클래스를 사용하여 Vehicle Object 클래스를 관리합니다. 어떤 이유로 나는 예외 범위를 벗어난 인덱스를 얻고있다.JTextField에 값/문자열 반환, IndexoutofboundsException

구이 클래스

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가 shortly, runnable, complilable 인 경우에만 시연합니다. – mKorbel

+0

오류는 IndexOutOfBoundsException : Index : 0, Size입니다. : 0. 비록 배열 내에 4가 있다는 것을 알고 있습니다. 위와 같이 getVehicles 메서드를 사용하여이를 증명했습니다. – Melky

+0

이 예외는 필자가 작성한 코드에 대해 흥미로울 수 있으며 내 화면에서보고 디버그합니다. – mKorbel

답변

1

. 그러나이 코드 조각에서, IndexOutOfBoundsException부터 co.kr에서 할 수있는 유일한 장소는 arrayPosition 잘못이다,

return theVehicles.get(arrayPosition); 

귀하의 문제가있다.
는 더 많은 코드를

편집을 정확히 무엇이 잘못되었는지 알아 내기위한 코드를 디버깅을 시도하거나 게시 : 당신은 static 키워드가하는 일에 대한 오해를 갖고있는 것 같다.
static 개체 또는 메서드는 런타임에 한 번만 인스턴스화되는 무언가입니다. 예를 들어
클래스 ShowroomDriverCars라는 단일 class 속성을 가지고 class ShowhroomDriver 수단의 Cars 속성의 당신의 선언 (그건 그렇고 -. 속성은 대문자로 시작하지 않는 이것은 혼란 매우입니다) . 그런 다음

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

을 대신 :

는 당신이 비록 원하는 것은 클래스 GuiInterfaceShowRoom (당신의 Cars 속성)의 인스턴스를 전달하는 것입니다과 같이 생성자를 통해 (도 거기 static 키워드를 제거)

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

당신은 쓰기

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

main 방법을 제외한 모두static 키워드를 삭제하십시오.

+0

arrayPosition의 기본값은 0으로 설정되어 있습니다. 해당 위치에 차량이 있다는 것을 알고 있습니다. 내가 추가 한 새로운 방법을 보아라. – Melky

+0

'getField()'는 정적입니다. 그게 문제 일거야. 그것을 제거하고, 그것은 작동해야합니다 – Chris

+0

슬프게도 변화가 없다, 단지 당신이 필요로하는 코드를 말하면 나는 꽤 abit입니다 게시 할게. – Melky