Even after I set a for loop in my constructor I receive a null pointer exception any ideas why?

here is a bit of the code

private String[] ingredientName,units = new String [40];
private double[] ingredientAmount = new double [40];

public Recipe() // Recipe Constructor example
	{
			for(int setVals =0; setVals<40;setVals++)
		{
			ingredientName[setVals] = new String("");
			units[setVals] = new String("");
			ingredientAmount[setVals] = new Double(0.0);
		}
}

You have not initialized all of your arrays.

Also, you don't need to use String constructor directly like this new String("") . Just use the literal ""

You have not initialized all of your arrays.

Also, you don't need to use String constructor directly like this new String("") . Just use the literal ""

Really? But inst the for loop doing that?

'ingredientName' is still null when you get to that loop.

'ingredientName' is still null when you get to that loop.

is it because its a whitespace?
Because I'm writing to a file and if its null or a whitespace I want the writer to skip over it.

whitespace? I'm saying you have a variable that you declared but never initialized at all. You've written private String[] ingredientName , but you never sized it. The "," you've used does not make all of your variables share that = new String[40]; (which is one more reason why it's just not a good idea to declare multiple variables on a single line)


You're attempting to access elements of something that is completely null.

Member Avatar for hfx642

You can think of it this way...
Lines #1 & #2 says... I'm GOING to create these variables [40 of them].
Lines #8, #9, & #10, you start using them, without actually CREATING them.
At least... That's the way that I think of it.

You can think of it this way...
Lines #1 & #2 says... I'm GOING to create these variables [40 of them].
Lines #8, #9, & #10, you start using them, without actually CREATING them.
At least... That's the way that I think of it.

So if its creating them in line 1 and 2 that means I don't need to initialize them again?

Member Avatar for hfx642

No.
Line #1 and #2 does NOT create (initialize) them.
It just tells Java that you are GOING to create them.
(ie. Give me some "room" in memory, so I CAN create them.)
You still have to create (initialize) them BEFORE you use them.

No.
Line #1 and #2 does NOT create (initialize) them.
It just tells Java that you are GOING to create them.
(ie. Give me some "room" in memory, so I CAN create them.)
You still have to create (initialize) them BEFORE you use them.

But that is why I'm using

ingredientName[setVals] = "";
			units[setVals] = "";
			ingredientAmount[setVals] = 0.0;

and I still receive this error.. Sorry for all of the questions.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Recipe.<init>(Recipe.java:22)
at Panel$Save.actionPerformed(Panel.java:252)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

There are two pieces to consider with an array: the array object itself and the contents of the array.

Let's look at the one that is just fine:
You declared a variable of type String[] called 'units'

String[] units

and you create it to hold 40 elements

units = new String[40]

and then you initialize the elements themselves to empty strings

units[i] = ""

That is fine and good. One of those steps is missing however for your ingredientName array. That is what I described above in my previous post.

Just for clarity - because you may be misunderstanding what happens with the comma here:

private String[] ingredientName,units = new String [40];

is equivalent to

private String[] ingredientName;
private String[] units = new String [40];
Member Avatar for hfx642

We can't help you if you don't give us the correct information.
The error listing says that you've got a problem on line #22.
However, the only code that I've seen, only has 12 lines!

We can't help you if you don't give us the correct information.
The error listing says that you've got a problem on line #22.
However, the only code that I've seen, only has 12 lines!

Well I got help with the program what you said was right I did not initialize the ingredientName variable.
Sorry for not providing the code but since it's a project with many disorganize lines I thought it might confuse you guys.. and that would be embarrassing.

Member Avatar for hfx642

No problem.
We are glad to help, but we can only go by what you give us.
Please mark this thread as "solved".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.