File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Totally baffled with  ArrayList throwing NullPOinterException ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Totally baffled with  ArrayList throwing NullPOinterException !" Watch "Totally baffled with  ArrayList throwing NullPOinterException !" New topic
Author

Totally baffled with ArrayList throwing NullPOinterException !

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi.
I've been on this problem for a week, can anyone see the error please?
I'm trying to implement a TXRecord class containing an ArrayList to hold transient record data. TXRecord can be invoked by any number of applets in same browser to add a record.
Java console throws this error msg at runtime:

...
system datetime stamp: Feb 27, 2004 12:21:22 PM
(1) ConfirmationFrame: blank account name : blank unit cost
java.lang.NullPointerException at ConfirmationFrame$prdDtlActionListener.actionPerformed(ConfirmationFrame.java:328)

TXRecord.java...

This is the startup applet which loads TXRecord for the first time

This is one of the applets which will invoke TXRecord
[code]
public class ConfirmationFrame extends JFrame
{ TXRecord txrecord = null;
prdDtlActionListener listener;
// need to expand the parameter list...
public ConfirmationFrame(URL codeBase, String baseInstr) throws IOException
{ // ...unrelated java code...
// ==============================
/** Listens to the buttons and the combo box. */
class prdDtlActionListener implements ActionListener
{
public void actionPerformed(ActionEvent aevt)
{
if (aevt.getSource() == portfolioList) // action is on the combobox
{ portfolioName = (String)portfolioList.getSelectedItem();
} else if (aevt.getSource() == back) // back to product detail
{
System.out.println("back button");
} else if (aevt.getSource() == confirm)
{ String var_acctName = "blank account name";
String var_unitCost = "blank unit cost";
System.out.println("(1) ConfirmationFrame: "+ var_acctName + " : " + var_unitCost );
txrecord.insertTXRecord("xxx", "yyy"); //<---- ERROR HERE? flush to TXRecord
} else if (aevt.getSource() == cancel)
{ System.out.println("Cancelled");
f.dispose();
}
}
} // end of class prdDtlActionListener
// ==============================
public void callTXRecord()
{
txrecord = TXRecord.selfRef;
}
// ==============================
[code]


humanum errare est.
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi.
Further tests show the following offending line of code, but why is it tripping a NullPointerException ?

Can someone help please ? :-)
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Achana,
You need to follow these errors logically. The exception is a null pointer
but does not really tell you which variable is null. Looking at your code there are not many contenders so before you use the pointers in your error area, put in some code like:


I guess this was not your error

but that this was your error:

so you could use;

so that your code will complain if the pointer is wrong.
I have found that exceptions in code apparently compiled on the fly by the jit jre throw exceptions back to their calling code, so maybe the error is in this code.

but that is not likely unless var_acctName or var_unitCost is a null.
Let's know how you get on. You may already have found your problem?
Ed


The nice thing about Standards is that there are so many to choose from!
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi Ed.
Tried out your suggestion

with the following Java console output

system datetime stamp: Feb 27, 2004 11:53:09 PM
txrecord is null!
java.lang.NullPointerException at ConfirmationFrame$prdDtlActionListener.actionPerformed(ConfirmationFrame.java:330)

So I am back to square one, which is I cannot write and store transient records to the TXRecord class.
It's midnight. Thanks for your time and input. :-)
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
Your code is most odd! It's usually considered bad form to reveal your internal variables directly in your interface, and a static "selfRef" variable means that your class will only really work as a singleton. You'll get all kinds of weird behaviour if you create multiple instances of the class.
But anyway, assuming you do want this TXRecord to be a singleton, if you change the line of code, as follows, it may help:
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
I don't know if I missed anything, but I didn't see you initiliaze public static TXRecord selfRef anywhere in the code; I see you first set to null, then set it to self??? Can you tell us what your client looks like when using this class?
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi David,
I understand where you are coming from about exposing internal variables, but what other techniques do I have to write a class which holds transoent records from other applets ? Whether hashmap or array list, I still need to flush from other applets to TXRecord for temporary storage before flushing to database.
Thanks for your time and input. :-)
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hiya Adrian,
I've included the stub code for one of the clients but didn't format it properly, so here it is again in a more detailed form...

Based on previous replies, esp. the last one from David, I would like to use another technique if there was one. Are there any other ways to use an applet to store transient data from other applets before they are flushed to the database ?
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 196
The only time txrecord gets a value is when the method TXRecord is called.
public void TXRecord()
{
txrecord = TXRecord.selfRef;
}
But where is this method called?
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
OK. Look at this piece of code

You can see that the statement
txrecord is null!
is being printed out. Meaning that txrecord is actually null. And then you call a method on the null object.
[ March 02, 2004: Message edited by: Murali Nanchala ]

"It is almost always answered in the API" - Murali Nanchala
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi.
Thanks for your time and input, it is much appreciated.
I've tried many different techniques and code variations.
The requirement stays the same: how do I implement a class using a Hashtable to hold transient records from other applets until it is ready to flush them to the database?
I've been asked why I want to do that, here is the answer: the users on the Net will select their items from different JFrames (launched from JMenu's), their selection will be held in a Hashtable. At the end, the user confirms the selection and the content of the Hashtable will be flushed to the database. It is not possible to flush to database every time a user selects an item before confirming it.
I believe after three weeks at it and getting no practical answers from any Java forums (Javaranch, Sun, IBM or jGuru) that it is impossible to create a class to hold record data from client applets.
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
That means some of these will be rewritten in C++, what a blow. At least I am assured that it is not difficult in C++ (my C++ is possibly worse than my Java so I can't tell).
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 196
Calling an instance method on a reference variable that can have the value null is a no go area. Correct me if I'm wrong but when txrecord has the value null, you still call getTXRecord(). Why?

[ March 03, 2004: Message edited by: Arnold Reuser ]
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
I believe after three weeks at it and getting no practical answers from any Java forums (Javaranch, Sun, IBM or jGuru) that it is impossible to create a class to hold record data from client applets.

Personally I think the reason you haven't had a useful response is because neither your code nor your question make much sense!
It's bizarre because in some areas you seem to have advanced knowledge of Java (e.g. implementing ActionListeners), and in other areas (e.g. creating objects and classes) you appear to be relatively inexperienced.
I'm guessing (from your use of Hungarian notation) that you already know Visual Basic, and have jumped in at the deep end with Java, picked up some existing code, read it and then started working out how to change it. You're obviously an intelligent guy, but you need to have a solid grounding in Java and object-oriented programming before you can start tackling this kind of problem. Please don't take offence at this. It's intended to be constructive. Go through some of the Java tutorials. It's a pain, but it will save you time in the long run.
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hiya David.
No offence taken and thanks for taken time to review my issue.
Yes, indeed I do learn from Sun tutorials, books (lots of them) and experiment a with bits of codes I pick up. When all else fails, I post questions on the forum.
THX and keep up the good work helping others :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Totally baffled with ArrayList throwing NullPOinterException !