Need Java help - FMVperformance.com : The site for all your Ford Mazda and Volvo needs
 
LinkBack Thread Tools
post #1 of 5 (permalink) Old 07-18-2007, 11:48 AM Thread Starter
Senior Member
 
Join Date: Aug 2005
Location: Ellicott City, MD
Posts: 9,451
Need Java help

I am 110% stumped. I am trying to create a fun applet as a space battle simulator (honor harrington rip off if anyone has read the books). Anyway, this is what it comes to when I am bored at work. Anyway, my issue is that I am trying to read my choice selections when I hit a button (or even if the choice selection changes, I care not which way I achieve it). Anyway, my issue is no matter what I try I can't get a read off of the choice drop down (using choice.getSelectedItem()) within the actionPerformed or itemStateChanged methods.

Here is my code, anyone have any ideas??? By the way, the commented out section is what I had been using, but it wasn't working at all. Thanks.

Here is a sample code that I am trying to get to work (and apply it to my larger applet).

import java.lang.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;



public class sample extends Applet implements ActionListener, ItemListener
{

public Choice cSamp;
private Button sampButton;
private TextField tf1;

public void init()
{
Choice cSamp = new Choice();
cSamp.add("None"
cSamp.add("one"
cSamp.add("two"
cSamp.addItemListener(this);
add(cSamp);
sampButton = new Button("Change"
sampButton.addActionListener(this);
add(sampButton);
tf1 = new TextField(30);
add(tf1);
}


public void itemStateChanged (ItemEvent e)
{

tf1.setText(cSamp.getSelectedItem());
}

public void actionPerformed (ActionEvent e)
{
tf1.setText(cSamp.getSelectedItem());
}

}
azazel1024 is offline  
Sponsored Links
Advertisement
 
post #2 of 5 (permalink) Old 07-18-2007, 02:07 PM Thread Starter
Senior Member
 
Join Date: Aug 2005
Location: Ellicott City, MD
Posts: 9,451
Re: Need Java help

Anybody?

If it helps when I throw exception handling in there it is throwing a NullPointerException

*sigh*

WHY!

Grrr.

Anyway, thanks to anyone who can give this a swing.
-Matt
azazel1024 is offline  
post #3 of 5 (permalink) Old 07-18-2007, 02:11 PM
F1 Driver
 
Join Date: Apr 2004
Location: New Haven, Connecticut
Posts: 8,677
Send a message via AIM to Hexedit Send a message via MSN to Hexedit
Re: Need Java help

the java master has arrived.

What line is throwing the null pointer exception?

Just to clarify, a null pointer exception occurs when you try to reference a variable that does not point to an active object. Now I don't know the lifecycle of an applet off the top of my head and from the code you have shown me you are initializing all your variables so thats why I want to know what line is throwing the exception.

Titanium Grey HB 5 spd, ABS, Moonroof, Leather seats, Exel RS-GT 17x7.0 Hyperblack 225/45/17 Azenis RT-615, Racing Beat Front/Rear Sway + Endlinks, Mazdaspeed MSP3 Coilovers, 88 AWR Mount, Apexcone 6000k HID, I deturboed my car and removed my built engine. I'm now stock powertrain + an Injen.
Hexedit is offline  
post #4 of 5 (permalink) Old 07-18-2007, 02:20 PM Thread Starter
Senior Member
 
Join Date: Aug 2005
Location: Ellicott City, MD
Posts: 9,451
Re: Need Java help

crap.

I figured it out after staring at it awhile longer.

I had palced a try/catch in the ItemStateChanged method.

The reason why it is not working outside of the init method is because I had used

Choice cSamp= new Choice();

It makes cSamp local only to the init method, so of course it is not accesible.

I changed it to cSamp = new Choice(); and it works dandily now.

*sigh*, if I had only not spent 3hrs on this crap.

Sometimes I tell you it is the little things.
-Matt
azazel1024 is offline  
post #5 of 5 (permalink) Old 07-18-2007, 07:56 PM
F1 Driver
 
Join Date: Apr 2004
Location: New Haven, Connecticut
Posts: 8,677
Send a message via AIM to Hexedit Send a message via MSN to Hexedit
Re: Need Java help

hahaha i didn't even see that, stupid me....

Titanium Grey HB 5 spd, ABS, Moonroof, Leather seats, Exel RS-GT 17x7.0 Hyperblack 225/45/17 Azenis RT-615, Racing Beat Front/Rear Sway + Endlinks, Mazdaspeed MSP3 Coilovers, 88 AWR Mount, Apexcone 6000k HID, I deturboed my car and removed my built engine. I'm now stock powertrain + an Injen.
Hexedit is offline  
Sponsored Links
Advertisement
 
Reply

  FMVperformance.com : The site for all your Ford Mazda and Volvo needs > Misc > Lounge

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the FMVperformance.com : The site for all your Ford Mazda and Volvo needs forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in










Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page



Posting Rules  
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome