aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Errors with array and JTextArea and ComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Errors with array and JTextArea and ComboBox" Watch "Errors with array and JTextArea and ComboBox" New topic
Author

Errors with array and JTextArea and ComboBox

Trish Smith
Greenhorn

Joined: Apr 15, 2012
Posts: 12
Hi all! I'm taking my first Java class (online) and have hit a huge wall trying to use swing for GUI elements. I would be extremely grateful is someone could look at my code and point me in the right direction for getting things working. This is my first post so I apologize if it is not formatted correctly.

I am attempting to set up 2 arrays -- one to store an ID number that will display in a ComboBox and one to store votes assoicated with each ID number (I would love to do this with a single array if possible) -- and two text areas to display results, plus two buttons and a combobox.

I've created private classes for the button and combobox listeners. I would like to increment the value stored in each array element when a vote is cast (vote button is pushed) and display a text chart of results when the end vote button pushed. Unfortunately, I am getting a null exception error whenever an array statement is encountered in the listeners.



Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I would love to do this with a single array if possible

create a separate class Vote (if that's what you want to call it)
it will have 2 fields
String id;
int voteCount = 0;
override toString() to return the id

create them giving each an id, add them to your array,
or add them to the comboBox (the id will show)

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

not sure, but this looks wrong. you declare the arrays at the top
public String[] singerID;
then in your constructor you say this
String[] singerID = new String[MAX];
it should be just
singerID = new String[MAX];

SCJP
Visit my download page
Trish Smith
Greenhorn

Joined: Apr 15, 2012
Posts: 12
Michael Dunn wrote:> I would love to do this with a single array if possible

create a separate class Vote (if that's what you want to call it)
it will have 2 fields
String id;
int voteCount = 0;
override toString() to return the id

create them giving each an id, add them to your array,
or add them to the comboBox (the id will show)



I started out with a separate class for vote, but then couldn't figure out how to increment a value. Feeling VERY STUPID at this point. I have worked with arrays quite a bit, but not in Java and not with classes. I don't quite have my head wrapped around making objects yet. Here is the code for the Vote class:



I even started making a class for the vote array, but think that is going down the wrong path and making things too complicated.

When the vote button is clicked I want to increment a count for an array element, such as: array[i] = vote++. I tried passing the array as a parameter to the button listener (grasping at straws) but that didn't work. I don't understand how to link the events in the private listener to a value at a specific array element...
Trish Smith
Greenhorn

Joined: Apr 15, 2012
Posts: 12
Randall Twede wrote:not sure, but this looks wrong. you declare the arrays at the top
public String[] singerID;
then in your constructor you say this
String[] singerID = new String[MAX];
it should be just
singerID = new String[MAX];


Duh. I'm so flustered at this point. Here is the correction:

Trish Smith
Greenhorn

Joined: Apr 15, 2012
Posts: 12
I appreciate the help! I have two different Java books open and am hoping an "aha" moment will happen. This was my stab at a VoteArray class:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not really sure what you're trying to achieve with arrays,
but here's a simple version of one or two things you're trying

run it, select a candidateID from the comboBox, click the 'Vote' button.
repeat numerous times

Trish Smith
Greenhorn

Joined: Apr 15, 2012
Posts: 12
Thanks Michael. This is an assignment which is why we have to do it with arrays. There are other parameters and restrictions as well, so we can't use features like SwingUtilities, each class has to be in a separate file, etc...
Trish Smith
Greenhorn

Joined: Apr 15, 2012
Posts: 12
I finally got it to work! Thanks for the help.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

Trish Smith wrote:we can't use features like SwingUtilities

Don't be silly. How else are you going to launch the GUI on the EDT?

Are you allowed to use EventQueue?
Trish Smith
Greenhorn

Joined: Apr 15, 2012
Posts: 12
Darryl Burke wrote:
Trish Smith wrote:we can't use features like SwingUtilities

Don't be silly. How else are you going to launch the GUI on the EDT?

Are you allowed to use EventQueue?


No. This is an intro level course and the instructor is very clear about what we can and cannot use. Each assignment is focused on a particular concept and may not necessarily lead to a "real world" programming solution.

To get things finally working, I modified the Vote class (thanks for the feedback received in this forum):


Modified the toString method in the Histogram class (a required element):


Then created the array objects (still using two because I ran out of time to get the single array solution working) in the VoteControls class:


Then modified the listeners:


Now that it works I wish I had time to go back and clean it up. It's not an elegant solution and there are stray bits of code that could be removed, but at least it does work and it was submitted by the deadline, with 30 minutes to spare. ;-)
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i noticed this also when i went to college the last time. they made me use arrays and vectors when we had ArrayList and others
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Errors with array and JTextArea and ComboBox