FileOutputStream 随机写入 null。

15 浏览
0 Comments

FileOutputStream 随机写入 null。

Closed. 这个问题需要 调试细节。现在不接受答案。


编辑问题,包括 期望的行为、具体问题或错误,以及重现问题所需的最短代码。这将有助于其他人回答这个问题。

改进此问题

我有一个程序,可以 读取写入 文件。当我重启几次程序后,整个文件就变成了空白,程序现在无法启动。

编辑:

我已经在帖子中添加了主要类。

代码:

Game 类:

package gui;
import java.awt.AWTException;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Game2 extends javax.swing.JFrame implements Runnable {
// Took out the menu items
/**
 * Creates new form Game
 */
public Game2() {
    initComponents();
}
// Class declarations
static final ShopExtras SE = new ShopExtras();
// Number values
static int clicks = 0 + read(0);
static int coins = 0 + read(1);
static int upgrade = 0 + read(2);
static int clicksToday = 0;
// Booleans
static boolean clickLoop = true;
public static void write() {
    Properties prop = new Properties();
    OutputStream writer = null;
    // Creates array copy of the values
    int[] data = new int[4];
    data[0] = clicks + 0;
    data[1] = upgrade + 0;
    data[2] = coins + 0;
    try {
        // Creates the file
        writer = new FileOutputStream("config.properties");
        // Adds the properties to the file
        prop.setProperty("clicks", "" + data[0]);
        prop.setProperty("upgrade", "" + data[1]);
        prop.setProperty("coins", "" + data[2]);
        prop.setProperty("click amount", "" + data[3]);
        prop.store(writer, null);
    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public static int read(int i) {
    Properties prop = new Properties();
    InputStream reader = null;
    // Arrays for loading data
    int[] intData = new int[5];
    String[] data = new String[5];
    try {
        // Finds the file
        reader = new FileInputStream("config.properties");
        // Gets the file
        prop.load(reader);
        data[0] = prop.getProperty("clicks");
        data[1] = prop.getProperty("coins");
        data[2] = prop.getProperty("upgrade");
        data[4] = prop.getProperty("clicks today");
        // Prints out requested value
        System.out.println(prop.stringPropertyNames());
        // Gets requested value
        intData[i] = Integer.parseInt(data[i]) + 0;
    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    // Returns requested value
    return intData[i] + 0;
}
public static void loopedTasks() {
    Thread t1 = new Thread() {
        @Override
        public void run() {
            while (true) {
                // First task
                label.setText("You have clicked: " + clicks + " times!");
                clickButton.setText("Click me! +" + 1);
                coinLabel.setText("You have: " + coins + " coins.");
                // Secondary Task
                write();
            }
        }
    };
    t1.start();
}
/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
//                           
private void initComponents() {
    jMenu1 = new javax.swing.JMenu();
    jMenu4 = new javax.swing.JMenu();
    jMenu3 = new javax.swing.JMenu();
    jMenu6 = new javax.swing.JMenu();
    jPanel2 = new javax.swing.JPanel();
    resetButton = new javax.swing.JButton();
    clickButton = new javax.swing.JButton();
    label = new javax.swing.JLabel();
    coinLabel = new javax.swing.JLabel("You have: "+coins+" coins.");
    menuBar = new javax.swing.JMenuBar();
    shopMenu = new javax.swing.JMenu();
    clickerUpgrade = new javax.swing.JMenu();
    jMenu1.setText("jMenu1");
    jMenu4.setText("jMenu4");
    jMenu3.setText("jMenu3");
    jMenu6.setText("jMenu6");
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
    resetButton.setText("Reset");
    resetButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            resetButtonActionPerformed(evt);
        }
    });
    clickButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            clickButtonActionPerformed(evt);
        }
    });
    label.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    label.setText("You have clicked: "+clicks+" times!");
    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(resetButton)
            .addGap(0, 0, Short.MAX_VALUE))
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(coinLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                    .addGap(85, 85, 85)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(clickButton, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(label))
                    .addGap(35, 35, 35)))
            .addContainerGap(90, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
            .addComponent(coinLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(label)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
            .addComponent(clickButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(90, 90, 90)
            .addComponent(resetButton))
    );
    shopMenu.setText("Shop");
    clickerUpgrade.setText("Clicker Upgrade");
    shopMenu.add(clickerUpgrade);
    xPointsMenu.setText("Multiplied Points");
    shopMenu.add(xPointsMenu);
    menuBar.add(shopMenu);
    jMenu2.setText("jMenu2");
    menuBar.add(jMenu2);
    coinMenu.setText("Coins");
    menuBar.add(coinMenu);
    setJMenuBar(menuBar);
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    pack();
}//                         
// Click and Reset
private void clickButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    clicks = clicks + 1;
    System.out.println(1);
}                                           
private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    clicks = 0;
    upgrade = 0;
}                                           
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Game2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Game2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Game2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Game2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Game().setVisible(true);
            loopedTasks();
        }
    });
}
// Variables declaration - do not modify                     
public static javax.swing.JButton clickButton;
private javax.swing.JMenu clickerUpgrade;
private static javax.swing.JLabel coinLabel;
private javax.swing.JMenu coinMenu;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu6;
private javax.swing.JPanel jPanel2;
public static javax.swing.JLabel label;
private javax.swing.JMenuBar menuBar;
private javax.swing.JButton resetButton;
private javax.swing.JMenu shopMenu;
private javax.swing.JMenu xPointsMenu;
// End of variables declaration                   
@Override
public void run() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

写入方法:

    Properties prop = new Properties();
    OutputStream writer = null;
    // Creates array
    int[] data = new int[1];
    data[0] = 10;
    try {
        // Creates the file
        writer = new FileOutputStream("config.properties");
        // Adds properties to the file
        prop.setProperty("integer", "" + data[0]);
        prop.store(writer, null);
        ...

读取方法:

    Properties prop = new Properties();
    InputStream reader = null;
    // Arrays for loading data
    String[] data = new String[1];
    try {
        // Finds the file
        reader = new FileInputStream("config.properties");
        // Gets the file
        prop.load(reader);
        // Gets the property
        data[0] = prop.getProperty("integer");
    ...

错误: java.lang.ExceptionInInitializerError

Caused by: java.lang.NumberFormatException: null

修复:

删除config文件,然后重新运行程序。

非常感谢任何帮助!

admin 更改状态以发布 2023年5月23日
0
0 Comments

在这一行中:prop.setProperty("integer", "" + data[0]); 你将属性的键设置为 "integer"

但是当你获取该属性时,你尝试使用另一个键"clicks"获取它。

因此,在读取方法中,data[0]的值为null。这就是此错误消息试图说服您的原因:

Error: java.lang.ExceptionInInitializerError 
Caused by: java.lang.NumberFormatException: null

为了解决这个问题,将键更改为:

data[0] = prop.getProperty("integer");

关于doc中的 getProperty()方法:

public String getProperty(String key)

在此属性列表中搜索指定键的属性。如果在此属性列表中没有找到键,则递归检查默认属性列表及其默认值。如果未找到属性,则该方法返回null

参数:
key - 要查找的属性键。

返回值:
具有指定键值的属性列表中的值。

你可以阅读文档以了解一些关于 Properties 的知识。

更新:

评论:抱歉,在我的帖子中忘记更改了。整个文件都变成了 null/blank。

首先,我必须说你的完整代码无法编译(我不得不做很多事情才能到达这一步)。

我想这是你所得到的步骤(重启了几次):

enter image description here

ExceptionInInitializerError的原因:

你的read()clicks都是static的,如果static的上下文出了问题,你就会遇到这种错误消息。阅读更多

让我们逐步了解发生了什么:

在这一行中:data[0] = clicks + 0;,你试图访问clicks变量。

而那个变量正在调用read(0)static int clicks = 0 + read(0);

然后,在read()中,你试图做这个:

intData[i] = Integer.parseInt(data[i]) + 0;//i is 0

然后,当试图将data[i]传递给整数时,它是null(这就是错误消息所说的)。

java.lang.NumberFormatException的原因:

无法将空值(空字符串"")转换为整数。阅读更多

你可以像下面这样处理这个异常:

try{
    intData[i] = Integer.parseInt(data[i]) + 0;
}catch(NumberFormatException e){
    System.out.println("its null");//anything you want
}

这将处理异常,但你的文件clicks将重置为0(在几次重启后文件为空)。

为什么是null

当你第一次运行程序时,你可以看到java.io.FileNotFoundException:,但你的程序可以正常运行,在你点击按钮几次后,config.properties文件就会被创建。

原因是,在调用read()之前,您还没有调用write()

作为解决方案,在尝试读取文件(config.properties)之前,请检查属性文件是否存在。

为什么在重启几次后出现此错误:

因为您在一个线程中的loopedTasks()方法内调用了write()方法。您还在主线程中调用了loopedTasks()方法。而且这两个方法和其他字段(包括clicks)都是static的。

补充说明:您已经经常使用static这个关键词。

请阅读此文章Java:何时使用静态方法?并观看此视频

有多种方法可以使此方法正常工作。但是,避免使用static(如果您不知道如何正确使用,可能会出现意外情况),并在main()内这样做:

Game2 g= new Game2();
g.setVisible(true);
g.loopedTasks();

最后,我认为您已经理解我的意思了。

0