2014-11-08 166 views
0

我可以在創建後立即寫入隨機訪問文件,但只要我嘗試從我的函數寫入,就會中斷。寫入RandomAccessFile時出現NullPointerException

private void openFile() 
{ 
    JFileChooser fileChooser = new JFileChooser(); 

    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
    int result = fileChooser.showSaveDialog(this); 

    // user clicked Cancel button on dialog 
    if (result == JFileChooser.CANCEL_OPTION) 
    { 
     return; 
    } 

    File fileName = fileChooser.getSelectedFile(); 

    if (fileName == null || fileName.getName().equals("")) 
    { 
    JOptionPane.showMessageDialog(this, 
     "Invalid File Name", 
     "Invalid File Name", 
     JOptionPane.ERROR_MESSAGE); 
    } 
    else 
    { 
    // Open the file 
     try 
     { 
      RandomAccessFile output = new RandomAccessFile(fileName, "rw"); 
      createRecord(); 
     } 
     catch (IOException e) 
     { 
      JOptionPane.showMessageDialog(this, 
      "File does not exist", 
      "Invalid File Name", 
      JOptionPane.ERROR_MESSAGE); 
     }  
    } 
} 

private void createRecord() 
{ 
    String firstName = tfdFirst.getText(); 
    String lastName = tfdLast.getText(); 
    String emailAddress = tfdEmail.getText(); 
    String homeAddress = tfdAddress.getText(); 
    Long phoneNumber = new Long(tfdPhone.getText()); 
    char gender = tfdGender.getText().charAt(0); 

    System.out.println(firstName + lastName + emailAddress + homeAddress + phoneNumber + gender); 

    try 
    { 
     output.seek(0); 
     output.writeChar(gender); 
     writeString(output, lastName); 
     writeString(output, firstName); 
     writeString(output, emailAddress); 
     writeString(output, homeAddress); 
     output.writeLong(phoneNumber); 
    } 
    catch(IOException ex) 
    { 
     ex.printStackTrace(); 
    } 
} 

對不起,代碼的牆,但這些是兩個有問題的功能。第一個嘗試在調用createRecord()之前嘗試打開一個稱爲輸出的RandomAccessFile,它試圖將來自JFrame的用戶輸入寫入文件。如果我在createRecord()的位置放置了一個簡單的writeInt()函數,那麼它可以正常工作,但一旦調用createRecord()就好像輸出從不存在一樣。

我一直在這工作幾個小時無濟於事,所以如果有人能幫助找到解決方案,我將非常感激。

編輯:按要求堆棧跟蹤。 (警告,很長......)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at good_gals_customer_app.NewCustomerUI.createRecord(NewCustomerUI.java:298) 
at good_gals_customer_app.NewCustomerUI.openFile(NewCustomerUI.java:273) 
at good_gals_customer_app.NewCustomerUI.butCreateRecordActionPerformed(NewCustomerUI.java:186) 
at good_gals_customer_app.NewCustomerUI.access$100(NewCustomerUI.java:21) 
at good_gals_customer_app.NewCustomerUI$2.actionPerformed(NewCustomerUI.java:87) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
at java.awt.Component.processMouseEvent(Component.java:6527) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
at java.awt.Component.processEvent(Component.java:6292) 
at java.awt.Container.processEvent(Container.java:2234) 
at java.awt.Component.dispatchEventImpl(Component.java:4883) 
at java.awt.Container.dispatchEventImpl(Container.java:2292) 
at java.awt.Component.dispatchEvent(Component.java:4705) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) 
at java.awt.Container.dispatchEventImpl(Container.java:2278) 
at java.awt.Window.dispatchEventImpl(Window.java:2739) 
at java.awt.Component.dispatchEvent(Component.java:4705) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746) 
at java.awt.EventQueue.access$400(EventQueue.java:97) 
at java.awt.EventQueue$3.run(EventQueue.java:697) 
at java.awt.EventQueue$3.run(EventQueue.java:691) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) 
at java.awt.EventQueue$4.run(EventQueue.java:719) 
at java.awt.EventQueue$4.run(EventQueue.java:717) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 
+0

堆棧跟蹤請指出,導致NPE的線 – MadProgrammer 2014-11-08 10:11:15

+0

我甚至沒有看到這個代碼如何編譯; 'output'在'createRecord()'中沒有聲明' – fge 2014-11-08 10:12:25

+0

@fge出現這個問題,'output'很可能是一個實例字段 – MadProgrammer 2014-11-08 10:16:51

回答

2

你,這似乎是一個陰影問題...

你在openFile創建一個局部變量output ...

RandomAccessFile output = new RandomAccessFile(fileName, "rw"); 

但訪問,這似乎是在createRecord一個實例變量...

output.seek(0); 

很可能,實例變量是null ...

通常,我會建議刪除重新聲明並使用實例字段,但在此,我認爲您應該刪除實例字段並傳遞引用的RandomAccessFilecreateRecord方法

你也沒有管理您的資源,記住,如果你打開,你必須關閉它...

try (RandomAccessFile output = new RandomAccessFile(fileName, "rw")) 
{ 
    createRecord(output); 
} 
catch (IOException e) 
{ 
    JOptionPane.showMessageDialog(this, 
    "File does not exist", 
    "Invalid File Name", 
    JOptionPane.ERROR_MESSAGE); 
}  

看看The try-with-resources Statement更多細節

+0

謝謝!現在起作用了。另外,我打算在稍後添加關閉代碼,我想確保我可以先寫入。 – Clabbage 2014-11-08 10:25:31

+0

只要你知道,不關閉文件可能會導致文件不被刷新;) – MadProgrammer 2014-11-08 10:27:10

+0

好吧謝謝你提醒我:) – Clabbage 2014-11-08 10:28:10

相關問題