2014-02-14 100 views
-2

一切正常,直到當我按下我的任何按鈕時彈出錯誤。我正在按照指定的教程和做, 我不明白什麼是錯的。線程異常AWT EVENTQUEUE 1

「異常在線程 」AWT-EventQueue的-0「 java.lang.ArrayIndexOutOfBoundsException:1」

而這是代碼。

public class Med implements Comparable<Med>{ 
    private String nume, comp, ind, cind, mod, pret; 

    public Med(String nume, String comp, String ind, String cind, String mod, String pret) { 
     this.nume = nume; 
     this.comp = comp; 
     this.ind = ind; 
     this.cind = cind; 
     this.mod = mod; 
     this.pret = pret; 
} 
public String getNume(){ return nume; } 
public String toString(){return nume+"_"+nume+"_"+comp+"_"+ind+"_"+pret; } 
public int compareTo(Med o){ return (nume).compareTo(o.getNume()); } 
} 

import java.util.*;import java.io.*; 
import javax.swing.*; 
public class Farmacie{ 
private TreeSet<Med> ts; 
private BufferedReader br; 
private PrintWriter pw; 
private String l; 
private Med med; 
private static Farmacie instanta; 
private Farmacie(){ 
    File f=new File("X:\\med.txt"); 
    ts=new TreeSet<Med>(); 
    String[] s, s1; 
    String ae; 
    if (f.exists()){ 
    try{ 
    br=new BufferedReader(new FileReader(f)); 
    while ((l=br.readLine())!=null){ 
    s=l.split("_"); 
    s1=s[0].split(" "); 
    med=new Med(s1[0], s1[1], s[1], s[2], s[3], s[4]); 
    ts.add(med); 
    } 
    }catch(IOException ioe){ioe.printStackTrace();} 
    } else System.out.println("Fisierul nu exista"); 
} 

public String getMed(){ 
    String rez=""; 
    Iterator<Med> it=ts.iterator(); 
    while (it.hasNext()) rez+=it.next()+"\n"; 
    return rez; 
} 

public void adaugaMed(String nume, String comp, String ind, String cind, String mod, String pret){ 
    med=new Med(nume, comp, ind, cind, mod, pret); 
    if (ts.contains(med)) { 
    JOptionPane.showMessageDialog(null, "Fisa electronica exista deja in agenda telefonica", "Alert", 
JOptionPane.ERROR_MESSAGE); 
    return; 
    } 
    ts.add(med); 
    JOptionPane.showMessageDialog(null, "A fost adaugata o noua fisa electronica", "Information", 
JOptionPane.INFORMATION_MESSAGE); 
} 

public String cautaMed(String nume){ 
    for(Med f: ts) if(nume.equals(f.getNume())) return f.toString(); 
    return "nu exista nicio fisa cu numele "+ nume; 
} 

public void stergeMed(String nume){ 
    Iterator<Med> it=ts.iterator(); 
    while (it.hasNext()) { 
    if(nume.equalsIgnoreCase(it.next().getNume())) { 
    it.remove(); 
    JOptionPane.showMessageDialog(null, "Fisa a fost stearsa din agenda telefonica", "Information", JOptionPane.INFORMATION_MESSAGE); 
    return; 
    } 
    } 
    JOptionPane.showMessageDialog(null, "Fisa nu se gaseste in agenda telefonica", "Alert", JOptionPane.ERROR_MESSAGE); 
} 
public void salveaza(){ 
    try{ 
    pw=new PrintWriter(new FileWriter("fise.txt")); 
    for(Med f: ts) pw.println(f); 
    pw.close(); 
    }catch(IOException e){e.printStackTrace();} 
} 
public static Farmacie getInstanta(){ if (instanta==null) instanta=new Farmacie(); 
    return instanta; 
} 
} 

import java.awt.*;import java.awt.event.*; 
public class MedNouFrame extends Frame{ 
private GestorEvenimenteFNF ec; 
private TextField lt, at, tt, aet, mt, pt; 
private Button ok, cancel; 
public MedNouFrame(){ 
    super ("Adauga un med nou"); 
    Panel p=new Panel(); 
    p.setLayout(new GridLayout(6,2,10,10)); 
    p.setBackground(Color.lightGray); 

    p.add(new Label("Nume")); 
    lt=new TextField(10); 
    p.add(lt); 

    p.add(new Label("Comp")); 
    at=new TextField(10); 
    p.add(at); 

    p.add(new Label("ind")); 
    tt=new TextField(10); 
    p.add(tt); 

    p.add(new Label("cind")); 
    aet=new TextField(10); 
    p.add(aet); 

    p.add(new Label("mod")); 
    mt=new TextField(10); 
    p.add(mt); 

    p.add(new Label("pret")); 
    pt=new TextField(10); 
    p.add(pt); 

    add(p); 
    ec=new GestorEvenimenteFNF(); 
    p=new Panel(); 
    ok=new Button("OK"); 
    ok.addActionListener(ec); 
    p.add(ok); 
    cancel=new Button("Cancel"); 
    cancel.addActionListener(ec); 
    p.add(cancel); 
    add(p, BorderLayout.SOUTH);  
    setLocation(300,300); 
} 
    private class GestorEvenimenteFNF implements ActionListener{ 
    private Farmacie tnb; 
    GestorEvenimenteFNF(){tnb=Farmacie.getInstanta();} 
    public void actionPerformed(ActionEvent e){ 
    if(e.getSource()==ok) { 
     tnb.adaugaMed(lt.getText(),at.getText(),tt.getText(), aet.getText(), mt.getText(), pt.getText()); 
     MedNouFrame.this.dispose(); 
    } 
    else MedNouFrame.this.dispose(); 
    } }} 

import java.awt.*;import java.awt.event.*; 
public class CautaMedFrame extends Frame{ 
private GestorEvenimenteCFF ec; 
private TextField t; 
private TextArea ta; 
public CautaMedFrame(){ 
    super("Cauta med"); 
    Panel p=new Panel(); 
    p.add(new Label("Introduceti nume")); 
    t=new TextField(10); 
    ec=new GestorEvenimenteCFF(); 
    t.addActionListener(ec); 
    p.add(t); 
    add(p, BorderLayout.NORTH);  
    ta=new TextArea(20,50); 
    add(ta); 
    setSize(300,200); 
    setLocation(300,300); 
    addWindowListener(new WindowAdapter(){ 
    public void windowClosing(WindowEvent we){ 
    CautaMedFrame.this.dispose(); 
    }}); 
} 
    private class GestorEvenimenteCFF implements ActionListener{ 
    private Farmacie at; 
    GestorEvenimenteCFF(){at=Farmacie.getInstanta();} 
    public void actionPerformed(ActionEvent e){ 
    if (e.getSource()==t){ 
    ta.setText(at.cautaMed(t.getText())); 
    t.setText(""); 
    }  
    } }} 

import java.awt.*;import java.awt.event.*; 
public class StergeMedFrame extends Frame{ 
private GestorEvenimenteFF ec; 
private TextField t; 
private Button ok, c; 
public StergeMedFrame(){ 
    super ("Sterge o fisa"); 
    Panel p=new Panel(); 
    p.setBackground(Color.lightGray); 
    p.add(new Label("Introduceti nume si prenume")); 
    t=new TextField(10); 
    p.add(t); 
    add(p);  
    p=new Panel(); 
    ec=new GestorEvenimenteFF(); 
    ok=new Button("OK"); 
    ok.addActionListener(ec); 
    p.add(ok); 
    c=new Button("Cancel"); 
    c.addActionListener(ec); 
    p.add(c); 
    add(p, BorderLayout.SOUTH); 
    setSize(300,100); 
    setLocation(300,300); 
} 
private class GestorEvenimenteFF implements ActionListener{ 
    private Farmacie at; 
    GestorEvenimenteFF(){ at=Farmacie.getInstanta(); } 
    public void actionPerformed(ActionEvent e){ 
    if (e.getSource()==ok) { 
    at.stergeMed(t.getText()); 
    t.setText(""); 
    } 
    else StergeMedFrame.this.dispose(); 
    } }} 

import java.awt.*; 
import java.awt.event.*; 
public class MedFrame extends Frame{ 
private TextArea ta; 
private Farmacie at; 
public MedFrame(){ 
    super ("Agenda telefonica"); 
    add(new Label("Toate fisele"), BorderLayout.NORTH); 
    ta=new TextArea(20,200); 
    at=Farmacie.getInstanta(); 
    ta.setText(at.getMed()); 
    ta.setEnabled(false); 
    add(ta); 
    setSize(500,300); 
    setLocation(300,300); 
    addWindowListener(new WindowAdapter(){ 
    public void windowClosing(WindowEvent we){ 
    MedFrame.this.dispose(); 
    }}); 
    } } 

import java.awt.*;import java.awt.event.*; 
public class FereastraPrincipala extends Frame{ 
private GestorEvenimenteFP ec; 
private Button[] b; 
public FereastraPrincipala(){ 
    super ("Bine ati venit!"); 
    Panel p=new Panel(); 
    p.setLayout(new GridLayout(3,2,10,10)); 
    p.setBackground(Color.lightGray); 
    String[] s=new String[]{"Creaza fisa noua", "Cauta fisa", "Sterge fisa", "Arata continut agenda telefonica", 
"Salveaza toate fisele"}; 
    ec=new GestorEvenimenteFP(); 
    b=new Button[5]; 
    for (int i=0; i<b.length; i++){ 
    b[i]=new Button(s[i]); 
    b[i].addActionListener(ec); 
    p.add(b[i]); 
} 
add(p);  
} 
private class GestorEvenimenteFP implements ActionListener{ 
private Frame f; 
public void actionPerformed(ActionEvent e){ 
    if(e.getSource()==b[0]){ 
    f=new MedNouFrame(); 
    f.pack(); 
    f.setVisible(true); 
    } 
    else if(e.getSource()==b[1]){ f=new CautaMedFrame(); f.setVisible(true); } 
    else if(e.getSource()==b[2]){ f=new StergeMedFrame(); f.setVisible(true); } 
    else if(e.getSource()==b[3]){ f=new MedFrame(); f.setVisible(true); } 
    else { Farmacie at=Farmacie.getInstanta(); at.salveaza(); } 
} 
} 
public static void main(String[] args){ 
Frame w=new FereastraPrincipala(); 
w.setSize(375, 150); 
w.setLocation(300,300);  
w.setVisible(true);  
w.addWindowListener(new WindowAdapter(){ 
    public void windowClosing(WindowEvent e){System.exit(0);} 
});  
} 
} 
+0

你可以分享你'med.txt'文件的內容? – Mureinik

+0

你確定你輸入文件的每一行都有正確的格式嗎? 'med = new Med(s1 [0],s1 [1],s [1],s [2],s [3],s [4]);'如果一行不在至少6塊由whitepsaces分隔。 – StephaneM

+0

這是太多的代碼。你甚至從未指出發生異常的行 –

回答

0

我對Farmacie的構造函數做了一些修改。我對行中第一個元素的分割進行了一些檢查。如果沒有空間,那麼它會在那裏失敗。否則你的代碼就可以。

private Farmacie() { 
    File f = new File("G:\\med.txt"); 
    ts = new TreeSet<Med>(); 
    String[] s, s1; 
    String ae; 
    if (f.exists()) { 
    try { 
     br = new BufferedReader(new FileReader(f)); 
     while ((l = br.readLine()) != null) { 
      s = l.split("_"); 

      s1 = s[0].split(" "); 
      String first = s1[0]; 
      String second = null; 
      if (s1.length > 1) 
       second = s1[1]; 

      med = new Med(first, second, s[1], s[2], s[3], s[4]); 
      ts.add(med); 
     } 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 
} else 
    System.out.println("Fisierul nu exista"); 

}

相關問題