aspose file tools*
The moose likes Beginning Java and the fly likes NullPointerException when using String[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException when using String[]" Watch "NullPointerException when using String[]" New topic
Author

NullPointerException when using String[]

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi.
I make reference to my earlier thread in this forum:
http://www.coderanch.com/t/395645/java/java/howto-return-multiple-values-parent
As stated earlier, I am trying to pass parameters between applets.
For testing, I've created a class for holding the data that needed to be passed back. It is an array.

From the following startup applet, I instantiate the above applet

The startupUpApplet invokes another applet, which evtly flushes the data to the TXDetailData as well as update the index for the next lot of data (in other words, it keeps multiple records):

ANd finally I added a little CleanUp routine which displays the updated TXDetailData to test the whole thing

When I tested it this morning without index it worked fine.
After I've added the index, and recompiled Java console reports the runtime error "NullPointerException".
Can anyone help out please?
TIA :-)
[ February 24, 2004: Message edited by: Jim Yingst ]

humanum errare est.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Nowhere in this code do you initialize the two array variables var_acctName and var_unitCost; they're both null. Hence, when you try to use them, you'll get a NullPointerException. You need to actually create an array object using "new" -- i.e.,
String[] var_acctName = new String[100];
I used "100" as the size because it wasn't clear to me what you'd want to size to be. If you don't know either, you should consider using ArrayLists instead of arrays, since ArrayLists can grow as needed.


[Jess in Action][AskingGoodQuestions]
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Arrgh, you hit the nail on the head.
Let's say a client buys pizza, pasta, salat, what ever over the Net, where each dish has its own JFrame, and I've to save the client's selection to a singleton pending flushing to the database on commit, then there is no way for me to know how big the order will be, hence I wouldn't know how big the array or hashtable should be.
That's my dilemma.
Having said that, I agree array[100] is probably on the extravagant side.
Thanks for taking time to look at my issue :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: NullPointerException when using String[]