2013-05-15 64 views
0

編輯Java的ArrayList的空指針異常

我已經清理字符串平等.equals方法,改變了ContactsCollection初始化:

public static ArrayList<Contact> contactList = new ArrayList<Contact>(); 

我也改變了執行的操作方法希望「顯示聯繫人」能顯示多個聯繫人。

if (contactInput.equals("Display contacts")) 
    { 
     ContactsCollection.read(); 
     for (int i = 0; i < contactList.size(); i++) 
     { 
      contact = (Contact)contactList.get(i); 
      for (int j =0; j < contactList.size(); j++) 
      { 
       textArea.append(contact.getName() + "," + contact.getNumber() + "\n"); 
      } 
     } 
    } 

最終將編寫.dat文件,但不包含任何通過GUI添加的數據。

編輯完

我寫一個模擬手機圖形用戶界面,作爲一個非常基本的聯繫人管理。還有其他幾個類沒有處理按預期工作的ArrayList。

當試圖將聯繫人添加到該文件I在ContactsCollection類的線13接收一個零指示字例外:

for (int i = 0; i < contactList.size(); i++) 

和線93的觸點(GUI)類:

contactList.add(contact); 

我有一種感覺,我編寫聯繫人和ContactsCollection類時做了錯誤。我希望程序運行如下:用戶點擊添加聯繫人並輸入成爲對象聯繫人的信息,並添加到contactList ArrayList中並寫入(序列化)到文件「contactList.dat」。當用戶點擊顯示聯繫人時,文件被讀入並且每個聯繫人被顯示在GUI中。

我認爲我設置ArrayList的方式有幾個問題,但我認爲我非常接近讓程序按我的希望運行。任何幫助是極大的讚賞!

聯繫人類:

import java.util.*; 
import java.io.*; 

public class Contact implements Serializable 
{ 
public static final long serialVersionUID = 42L; 
public String name, number; 

Contact() 
{ 
    name = "No name"; 
    number = "No number"; 
} 

Contact (String theName, String theNumber) 
{ 
    this.name = theName; 
    this.number = theNumber; 
} 

public void setName(String aName) 
{ 
    this.name = aName; 
} 

public void setNumber(String aNumber) 
{ 
    this.number =aNumber; 
} 

public String getName() 
{ 
    return name; 
} 

public String getNumber() 
{ 
    return number; 
} 

public String toString() 
{ 
    return name + ": " + number; 
} 

public boolean equals(Contact other) 
{ 
    if (name.equals(other.getName()) && number.equals(other.getNumber())) 
    { 
     return(true); 
    } 
    else 
    { 
     return(false); 
    } 
} 
} 

聯繫人Collection類

import java.io.*; 
import java.util.*; 

class ContactsCollection 
{ 
public static ArrayList<Contact> contactList; 

public static void write() 
{ 
    try 
    { 
     ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("contactList.dat")); 
     for (int i = 0; i < contactList.size(); i++) 
     { 
      out.writeObject(contactList.get(i)); 
     } 
     out.close(); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

public static void read() 
{ 
contactList = new ArrayList<Contact>(); 
try 
{ 
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("contactList.dat")); 
    Contact temp; 
    while (in.available()!=0) 
    { 
     temp = (Contact)in.readObject(); 
     contactList.add(temp); 
    } 
    in.close(); 
} 
catch(FileNotFoundException e) 
{ 
    e.printStackTrace(); 
} 
catch(IOException e) 
{ 
    e.printStackTrace(); 
} 
catch(ClassNotFoundException e) 
{ 
    e.printStackTrace(); 
}  

} 
} 

聯繫人(GUI)類

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

class Contacts extends JFrame implements ActionListener, WindowListener 
{ 
public static final int WIDTH = 400; 
    public static final int HEIGHT = 600; 
    public static final int SMALL_WIDTH = 200; 
    public static final int SMALL_HEIGHT = 100; 

private static final Dimension stdBtn = new Dimension(150, 50);  

    JPanel centerPanel, northPanel, southPanel; 
ImageIcon icon; 
JLabel picture; 
JButton addContact, displayContacts; 
JScrollPane scroll; 
JTextArea textArea; 
Clock clock; 
Background background; 
Contact contact; 
ArrayList<Contact> contactList; 


public Contacts() 
{ 
    super("Contacts"); 
    this.setLayout(new BorderLayout()); 
    this.setSize(WIDTH, HEIGHT); 
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
    addWindowListener(this); 
    this.setLocationRelativeTo(null); 

    centerPanel = new JPanel(); 
    northPanel = new JPanel(); 
    southPanel = new JPanel(); 

    centerPanel.setBackground(Color.BLACK); 
    southPanel.setBackground(Color.BLACK);  

    clock = new Clock(); 
    northPanel.add(clock); 

    icon = new ImageIcon("ContactsBackground.jpg"); 
    picture = new JLabel(icon); 
    centerPanel.add(picture); 

    textArea = new JTextArea("", 10, 30); 
    textArea.setEditable(false); 
    JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);  
    centerPanel.add(scroll);  

    JButton displayContacts = new JButton("Display contacts"); 
    displayContacts.addActionListener(this); 
    southPanel.add(displayContacts); 

    JButton addContact = new JButton("Add contact"); 
    addContact.addActionListener(this); 
    southPanel.add(addContact); 

    this.add(northPanel, BorderLayout.NORTH); 
    this.add(centerPanel, BorderLayout.CENTER); 
    this.add(southPanel, BorderLayout.SOUTH);  

    setResizable(false);   
} 

public void actionPerformed(ActionEvent e) 
{ 
    contactList = new ArrayList<Contact>(); 
    JButton source = (JButton)e.getSource(); 
    String contactInput = source.getText(); 

    if (contactInput == "Display contacts") 
    { 
     ContactsCollection.read(); 
     for (int i = 0; i < contactList.size(); i++) 
     { 
      contact = (Contact)contactList.get(i); 
      textArea.setText(contact.getName() + "," + contact.getNumber() + "\n"); 
     } 
    } 
    if (contactInput == "Add contact") 
    { 
     String name = JOptionPane.showInputDialog(null, "Enter Name"); 
     String number = JOptionPane.showInputDialog(null, "Enter Number"); 
     contact = new Contact(name, number); 
     contactList.add(contact); 
     ContactsCollection.write(); 
    } 
} 

public void windowOpened(WindowEvent e) 
{} 

public void windowClosing(WindowEvent e) 
{ 
    this.setVisible(false); 
    background = new Background(); 
    background.setVisible(true);   
} 

public void windowClosed(WindowEvent e) 
{} 

public void windowIconified(WindowEvent e) 
{} 

public void windowDeiconified(WindowEvent e) 
{} 

public void windowActivated(WindowEvent e) 
{} 

public void windowDeactivated(WindowEvent e) 
{}  
} 
+3

這是太多的代碼。 13號線在哪裏? – Ankit

回答

3

變化

public static ArrayList<Contact> contactList; 

public static ArrayList<Contact> contactList = new ArrayList<>(); 

contactList版本是null因爲你永遠不初始化,並在write()方法你嘗試調用size()就可以了。


另外,還有一些在你的GUI類中的一些嚴重缺陷(在actionPerformed()方法),閱讀這個問題解決這些問題:How do I compare strings in Java?


還記得textArea.setText(...)將設置完整的文本textArea,因爲這是在你的代碼循環中,textArea將只包含該循環的最後一次迭代的輸出。在你的情況下,它將只是最後一次聯繫。

+0

非常感謝! – user2380220

+0

我已經更新了頂部的代碼以顯示我所做的更改。此時它會寫入.dat文件,但只有Ԁ出現在文件中。 – user2380220

+0

@ user2380220:您仍然只能看到textarea中的最後一個聯繫人。使用'contactList'作爲類成員,而不是使用它作爲實例屬性來序列化它。 – jlordo