Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Errors with array and JTextArea and ComboBox

 
Trish Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 4442
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
 
Trish Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally got it to work! Thanks for the help.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4442
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic