aspose file tools*
The moose likes Java in General and the fly likes howto: return class of Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "howto: return class of Hashtable" Watch "howto: return class of Hashtable" New topic
Author

howto: return class of Hashtable

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi.
Just when I think I understand it at last... I've the following compile time error:

cannot resolve symbol
symbol : constructor paramTableFrame (java.net.URK, java.lang.String, java.lang.String)
location: paramTableFrame rtnFrame = new paramTableFrame(thisURL, Instr1, Instr2)

Following is the code snippet from the "control-applet", it controls a lot of things by using values from returned classes

Following is the code snippet from one of many returned classes, this one is external to controlApplet.java i.e. it is not a helper class or method in controlApplet.java
It actually works well when tested.


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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Constructors have no return value. If you give a "constructor" a return value, then it just becomes a plain ordinary method which just happens to have the same name as the class it's in (this ought to be illegal, in my opinion, but it's not.) So paramTableFrame() in your paramTableFrame class is a method, not a constructor, so you can't call it with "new".
Now, you don't use the Hashtable return value; you use this method as if it were a constructor. So just remove the return and the return type, and turn it into a constructor, and you'll be fine.
One more thing: there's an exceedingly strong convention in Java that class names start with a capital letter; making them start with a small letter, as method names do, makes the resulting code surprisingly hard to read.


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

Joined: Jul 29, 2002
Posts: 277
Hi.
First THX again for your time and explanation (I needed that).
I am now using the constructor rather than calling a method by commenting out "return paramTable" and the return type, like this

It compiles well.
However, now I have a runtime error on my Java console:

Testing enumeration on paramTable
java.lang.NullPointerException
at cbsMenu$3.actionPerformed(cbsMenu.java:507)
...........etc...

I understand from this runtime error that my controlApplet.java is not reading the Hashtable in ParamTableFrame class.
I feel I'm close but quo vadis?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, the stack trace says that line 507 or file cbsMenu.java is the cause. Perhaps you could look at that line. Maybe even tell us which line it is. Chances are, there's a . somewhere on the line. Whatever is to the left of the line, that's null. If there's more than one . on the line you have a minor problem; you need to find out which one is the one throwing the NullPointerException. Typically this can be accomplished by adding a few simple System.out.println() statements before the line causing the error. Or you can split the erroring line into several lines instead, each with one . only. Once you know which reference is null, you need to decide why it's null. Is null a normal occurrance here, or is it an indication of an error elsewhere? The answer to that will dictate the proper course of action here.


"I'm not back." - Bill Harding, Twister
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi, thanks for helping out.
I've indeed inserted System.println, like this

The Enumeration line is indeed where I trip over.
Also, I've modified the code slightly since last post:
from: Enumeration enum = paramTable.keys();
to: Enumeration enum = paramTableFrame.paramTable.keys();
Since paramTable comes back from ParamTableClass, I tested the Hashtable content by enumerating it in ParamTableClass.java, like this

From the Java console output, I can see where I might have gone wrong with my code:
(1) the first time controlApplet calls the constructor ParamTableFrame, there is indeed nothing to enumerate (because the user hasn't clicked on any JButton yet in ParamTableFrame for the action listener) ;
(2) in appletControl, it seems I must pause there to wait for ParamTableFrame to come back with a populated Hashtable?

I am think along that line, hope I'm not off course ??
TIA :-(
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi.
NullPointerException is thrown as soon as I try to reference ParamTableFrame.paramTable from within controlApplet.java
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi, I hope I've still got your attention.
There are actually two parts to this problem
(1) return class of Hashtable ( this has been successful );
(2) ActionListener refreshes Hashtable and then return Hashtable;
Here is the code which works for (1);

Now here is the ParamTableFrame.java which passes the Hashtable back to controlApplet.
It works so far

The Java console output is encouraging:

(111) Testing enumeration in cbsMenu
(222) Testing enumeration in cbsMenu
(333) Testing enumeration in cbsMenu
...(B) from ParamTableFrame: to controlApplet
...(A) from ParamTableFrame: to controlApplet
(444) Testing enumeration in cbsMenu
paramTableFrame tmpTableaction: review
paramTableFrame tmpTablevar_discPrice: 9407.172131147541
paramTableFrame tmpTablevar_qty: 1

Obviously I can get the Hashtable from paramTableFrame.java back to controlApplet.java
But when the user clicks the JButton "next" in paramTableFrame.java, and thereby create another entry for the Hashtable, how do I get controlApplet.java to acknowledge that?
It'd be good if I can get controlApplet.java to acknowledge the new entry in paramTable whenever the user clicks on the JButton in paramTableFrame.java
But HOW ?
TIA :-)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[B][/B]
All right, this is the source of the NullPointerException. Something to the left of a '.' is null. That means either paramTableFrame is null, or paramTable is null. Looking at your code, apparently paramTableFrame is a class name? It would really be helpful if you used standard Java naming conventions here. A class name should always begin with a capital letter, then only capitalize new words. So this should be ParamTableFrame. That will help make your code readable to others.
Now, if paramTableFrame really is a class name, what is paramTableFrame.paramTable? A static field of the class? Did you declare paramTable as static somewhere you haven't shown above? Probably. Otherwise this code wouldn't compile, I think. (Ummm, it does compile, right?) Now, I'm not at all sure you really want paramTable to be static. It would make more sense to me if each ParamTableFrame had its own paramTable, using a non-static paramTable variable.
Ah, this looks like a problem:

This is a constructor, creating a new instance of the paramTableFrame class. But the problem is, you don't save a reference to the instance anywhere. So how can you refer to the instance later, or do anything useful with it? You probably want to do something like this:

This assumes that paramTable is not a static variable. You need in instance to access the data of an instance (non-static) variable. So you create an instance of ParamTableFrame, save the reference using the variable ptf, then use that reference to access the instance field rtf.paramTable.
The same sort of problem may occur for your code here:

You're creating an instance, but you're not saving a reference to the instance, so I doubt you'll be able to do anything useful with the instance.
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi. Thanks for that.
I think we can safely say that the code works for case (1) return class of Hashtable;
My problem at the moment is (2) insert values inside anonymous class (action listener) into the Hashtable "paramTable" *and* display that in the java console.

Following is the java console output, which suggests (1) works.

(333) ShowParamTable in cbsMenu
...(C) from ParamTableFrame: to controlApplet
...(B) from ParamTableFrame: to controlApplet
...(A) from ParamTableFrame: to controlApplet
(444) ShowParamTable in cbsMenu
achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Sorry about the display, didn't know it would come out like that.
Thought it'd using <pre> </pre> tags...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: howto: return class of Hashtable