I'm fairly new to java, but I'm sure I've done this before and now it's not working right. What am I forgetting here?

LeftPanel class

Inventory inventory;
JTextField txtMoney = new JFormattedTextField(inventory.getInvWallet());

Inventory class

public class Inventory {

	public int invId = 0;
	public int invPrice = 0;
	public String invName = "";
	public double invWallet=100;
	public Inventory(int invId, int invPrice, String invName, double invWallet){
		this.invId = invId;
		this.invPrice = invPrice;
		this.invName = invName;
		this.invWallet = invWallet;
	public double getInvWallet(){
		return invWallet;
	public void setInvWallet(double invWallet){
		this.invWallet = invWallet;

The error I get

Exception in thread "main" java.lang.NullPointerException
	at LeftPanel.<init>(LeftPanel.java:14)
	at Sim.main(Sim.java:17)

Thanks muchly to those who answer

7 Years
Discussion Span
Last Post by Slyvr

You need to initialize the object with the keyword new, like

Object o = new Object();

Or in your case you need to initialize the Inventory object. All declared and not explicitly initialized objects are initialized to null, hence the NullPointerException

Edited by Slimmy: Explained a bit more


You have declared the object for the Inventory class. But you have not allocated the memory for it. So you have to write that statement explicitly.

Inventory inventory;
inventory = new Inventory();
JTextField txtMoney = new JFormattedTextField(inventory.getInvWallet());

Hope this will work fine for you. Try it out and please do let me know.


Thank you both, that seemed to be the problem. I usually put that but I forgot on this one :P
I have another issue now, though.

Part of my program needs to read through the list of employees I have in the employee class (similar to the inventory class) and check whether they've been hired already or not, but I'm not entirely sure how to do that. I can do


but how do I get the next Id and the next, next, next, etc.?

I'm thinking I'll have to reorganize the entire program and store the list in arrays and a .txt or something. Is this a bad idea, or are there reasons why I shouldn't do this method instead?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.