-1

In this program, I need my program to put a class's status into a JTextArea that is uneditable, but I keep getting a NullPointerException. I have a feeling that this has something to do with the fact that the object is deserialized every time it starts. If I remove this and replace it with an actual String, it works fine. What do I do? I'll post both classes below.

BankGUI class:

package GUIs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

public class BankGUI {

    BankAccount account;

    private void deserializeAccount() {
        try {
            ObjectInputStream objectStream2 = new ObjectInputStream(
                    new FileInputStream("bankAccounts.txt"));
            account = (BankAccount) objectStream2.readObject();
            objectStream2.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        BankGUI gui = new BankGUI();
        gui.deserializeAccount();
        gui.displayGUI();
    }

    // all global components for JFrame
    JTextArea statusArea;
    JCheckBox isLockedCheckBox;
    JList depositAmount;
    JList withdrawAmount;
    JButton depositButton;
    JButton withdrawButton;
    JButton saveAccountButton;

    private void displayGUI() {
        JFrame frame = new JFrame("Virtual Bank v3.3");

        Integer[] intList = { 1, 2, 5, 10, 20, 50 };

        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
        rightPanel.setBackground(Color.GREEN);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.GREEN);
        centerPanel.setLayout(new GridBagLayout());

        frame.add(BorderLayout.CENTER, centerPanel);
        frame.add(BorderLayout.EAST, rightPanel);

        // add some JLabel's
        JLabel depositAmountLabel = new JLabel("Deposit Amount:");
        JLabel withdrawAmountLabel = new JLabel("Withdraw Amount:");
        JLabel isLockedLabel = new JLabel("Lock account(True/False)");

        // finish components(center panel)
        statusArea = new JTextArea(25, 25);
        statusArea.setEditable(false);
        statusArea.setText(account.status);

        centerPanel.add(statusArea);

        // add this to panel
        isLockedCheckBox = new JCheckBox();
        // add this to panel

        // scrollers and Jlists
        // ***********************************************************************
        depositAmount = new JList(intList);
        JScrollPane scroller1 = new JScrollPane(depositAmount);
        scroller1
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller1
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        depositAmount.setVisibleRowCount(1);
        depositAmount.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        withdrawAmount = new JList(intList);
        JScrollPane scroller2 = new JScrollPane(depositAmount);
        scroller2
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller2
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        depositAmount.setVisibleRowCount(1);
        depositAmount.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // ***********************************************************************

        depositButton = new JButton("Deposit Amount.");
        withdrawButton = new JButton("Withdraw Amount");
        saveAccountButton = new JButton("Save your Account");

        rightPanel.add(depositAmount);
        rightPanel.add(depositButton);

        frame.setSize(425, 650);
        frame.setVisible(true);
    }

    private void serializeAccount() {
        try {
            ObjectOutputStream objectStream1 = new ObjectOutputStream(
                    new FileOutputStream("bankAccounts.txt"));
            objectStream1.writeObject(account);
            objectStream1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

BankAccount class:

package GUIs;

import java.io.Serializable;

public class BankAccount implements Serializable {

    private static final long serialVersionUID = -5341449653011848470L;

    int balance = 0;
    int userWallet = 0;
    String owner = "Foo Bar";

    String status = "Account Owner: " + owner + "\nAccount balance: $"
            + balance + "\nOwner Wallet Balance: $" + userWallet;
    boolean isLocked = false;

    public int withdraw(int amount) {
        balance -= amount;
        userWallet += amount;
        return userWallet;
    }

    public int deposit(int amount) {
        balance += amount;
        userWallet -= amount;
        return balance;
    }

    public int depositCashIntoWallet(int amount) {
        userWallet += amount;
        return userWallet;
    }

}

Here's the stack trace:

Exception in thread "main" java.lang.NullPointerException
    at GUIs.BankGUI.displayGUI(BankGUI.java:85)
    at GUIs.BankGUI.main(BankGUI.java:49)
1
  • 3
    At which line NPE is throwing in displayGUI? Have you checked if account is null after deserialization using account==null ?
    – Vishal K
    Commented Apr 30, 2013 at 22:39

2 Answers 2

6

The stack trace almost tells you exactly what the problem is. You have a NullPointerException at line 85 in your BankGUI class. If my IDE is correct line 85 is this line

statusArea.setText(account.status);

Only 2 things can be null here. statusArea and account. Since you initialize statusArea right before you use it, it must be account.

You need to initialize account before you use it.

BankAccount account = new BankAccount();

I can see you try to initialize account in a try catch block here:

try {
        ObjectInputStream objectStream2 = new ObjectInputStream(
                new FileInputStream("bankAccounts.txt"));
        account = (BankAccount) objectStream2.readObject();
        objectStream2.close();
} catch (Exception e) {
        e.printStackTrace();
}

If an exception is thrown account is not initialized. Make sure this does not throw an exception or make sure you check for null before you use account.

8
  • or use the account inside the try block, or check for null before you use it.
    – Steve B.
    Commented Apr 30, 2013 at 22:47
  • @SteveB. Added check for null to the answer.
    – MAV
    Commented Apr 30, 2013 at 22:49
  • @HashSlingingHacker Try to figure out why it is null. What happens when you try to deserialize? (Look at the stacktrace you print out, use breakpoints, etc) Why does this happen? (eg. It could be a FileNotFoundException if bankAccounts.txt does not exist). The correct solution depend on what happens.
    – MAV
    Commented Apr 30, 2013 at 23:45
  • @MAV I've read the stack trace, and I understand it. I'm quite new to serialization, new to Java in general, and I just want to know why it is becoming null. Is it dieing or something while it is being stored? Help me out here... I'm lost.
    – hasherr
    Commented Apr 30, 2013 at 23:50
  • @HashSlingingHacker it is impossible for me to say anything concrete without more information. If you understand the stacktrace from the deserialization (not the nullpointer) look up the methods you use to see why it is thrown.
    – MAV
    Commented Apr 30, 2013 at 23:58
2

One possibility is that the file used to serialize BankAccount, bankAccounts.txt, may be corrupt. This could have happened due to the class itself being modified. In that case you could temporarily replace

account = (BankAccount) objectStream2.readObject();

with

account = new BankAccount();

in deserializeAccount. This will allow the application to load without throwing an NPE.

Add an ActionListener to call serializeAccount and re-save the file. The deserializeAccount method can now be reverted and the application should be able to load the bankAccounts.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.