Geoff Prewett

Greenhorn
+ Follow
since Dec 22, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Geoff Prewett

You need the "public" so that code outside the class (namely the JVM's class loader) can call the function. You need the "static" so that the function can be called without an object (since it is not clear how one would instantiate the class, as there might be multiple constructors). It needs to be called "main" because the JVM has to have something to search for, and "main" is the canonical name. And it has to have String[] as its argument so that the JVM can pass in the command line arguments.
14 years ago
Sebastian's answer looks like the problem, to me, too. Also, I noticed you're assigning things to the 'patry1' local variable in the Pantry constructor. Since you don't do anything with them, I assume that you probably meant them to persist, in which case you would want to assign them to a member variable. Something like:

(the foreach loop iterates over every item in the array. You probably do not want "for (int i = 0; i < 3; i++)" since if you ever change the length of the array your code will break. "for (int i = 0; i < items.length; ++i)" is better, but the foreach loop is more concise.)

The NullPointerException name is a bit confusing, because what most CS people call "pointers", Java usually calls "references". Basically if you try to do anything will a null reference besides checking to see if it's null, you'll get a NullPointerException. Look at the output window and you'll see a stack trace that points you to the exact line that causes the problem.
14 years ago