gpt4 book ai didi

java - 写入 RandomAccessFile 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 05:17:20 25 4
gpt4 key购买 nike

我可以在创建随机访问文件后直接写入它,但一旦我尝试从我的函数写入,它就会中断。

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();
}
}

抱歉,代码之墙很长,但这是两个有问题的函数。第一个尝试打开一个名为输出的 RandomAccessFile,然后调用 createRecord() 尝试将用户输入从 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)

最佳答案

您似乎遇到了阴影问题...

您已在 openFile 中创建了一个局部变量 output...

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

但是访问,似乎是createRecord中的实例变量...

output.seek(0);

实例变量很可能为null...

通常,我建议删除重新声明并使用实例字段,但在此情况下,我认为您应该删除实例字段并将 RandomAccessFile 的引用传递给 createRecord 方法

您也没有管理您的资源,请记住,如果您打开,则必须关闭它...

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了解更多详情

关于java - 写入 RandomAccessFile 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com