Forums Register Login

listeners2

+Pie Number of slices to send: Send
Have been trying to code a program and i am having problems with the listeners. I have a combobox, a bunch of textfields and a 'ok button'. User picks the combo box, and writes in some text into the textfields and hits the 'ok button'. For now i want to just get all the info in varialbes so i can write more code later to write the results to disk. However i cant get the listeners to work right. here are some of the problem points..
blah blah
ButtonListener BL=new ButtonListener();

blah blah`

buttoncreate=new JButton("Create .trn file");
buttoncreate.addActionListener(BL);
mainPanel.add(buttoncreate);
this.add(mainPanel);
buttonquit=new JButton("Quit");
mainPanel.add(buttonquit);

blah blah

public class ButtonListener implements ActionListener{

public void actionPerformed (ActionEvent e){


if(e.getSource()==buttoncreate){
String start=(String)carType.getSelectedItem();
}
else{

buttoncreate.setText("bad");
}

it will compile but when i enter a value or no value in textfields i just get an error on the Sring start line


thanks roba
+Pie Number of slices to send: Send
> i just get an error on the Sring start line

if it's getting to that line, the listener is working.
what is the error message?
+Pie Number of slices to send: Send
if either i enter info in the box or just leave it emty (JTextField) the output pane would spit out info and it would not work. It complained about that line.
roba
+Pie Number of slices to send: Send
sorry here is the error
Exception in thread"AWT-EventQueue-0"java.lang.NullPointerException at robbox1$ButtonListener.actionPerformed(robbox.java:118


String start=(String)carType.getSelectedItem(); //line 118

sorry this time i am tring to get info from my combo box. But I have not got any listener to work yet. Thanks for your time.
roba
+Pie Number of slices to send: Send
What it could be doing is trying to get the selected item when there is no item selected. Maybe try to getSelectedIndex and check that the value is >=0 before you do the getSelectedItem line.

Try that and see.

Cheers,
Rachel
+Pie Number of slices to send: Send
The NullPointerException would indicate that carType is null, not that there is trouble in getSelectedItem(). Is it null?
+Pie Number of slices to send: Send
I tried to make declare the variable right off the bat now to see what happens.
private JComboBox carType="Cabboose";

but it says 'imcompatible type' so i can not do it that way.
Do you want to have a quick look at my code? It is only about 100 lines and the gui parts work fine, just the listener problem. Thanks
roba
JComboBox carType= new JComboBox();
carType.addItem("Caboose");

blah blah

public class ButtonListener implements ActionListener{

public void actionPerformed (ActionEvent e){


if(e.getSource()==buttoncreate){

String start=(String)carType.getSelectedItem();
+Pie Number of slices to send: Send
if only 100 or so lines, post the code
+Pie Number of slices to send: Send
would you possibly have your code structured like this:



if you do, including the 'type' (JComboBox) in the constructor
makes that 'carType' local to the constructor. The other 'carType'
is then null when accessed from elswhere in your code
+Pie Number of slices to send: Send
Here is the whole thing...



import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner.*;

public class robbox1 extends JFrame{

public static void main(String[] args){
new robbox1();
}
private JButton buttoncreate, buttonquit;
//public JComboBox carType;
private JLabel instuctions, carNumStart, carNumEnd, emailtext, twentyline, yardNumlab, tracklabel;
private JTextField carID, carIDend, origion, emailbox, twentylinebox, yardNumfield, trackfield;
public String start, end, email, origional, yardNum, trackNum, carTypeSelect;


public robbox1(){
this.setLocation(120,120);
this.setSize (450,420);
this.setTitle("Slim Car Builder 2.0");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


ButtonListener bl=new ButtonListener(); // Is this right???
JPanel mainPanel=new JPanel();

JLabel instructions=new JLabel("Please select the Car Type to construct:");
mainPanel.add(instructions);

JComboBox carType= new JComboBox();
carType.addItem("Caboose"); //3
carType.addItem("Passenger Car"); //4
carType.addItem("Well Car"); //5
carType.addItem("Spine Car"); //6
carType.addItem("AutoRack"); //7
carType.addItem("Woodchip Car"); //8
carType.addItem("Box Car"); //9
carType.addItem("Refer"); //10
carType.addItem("Coal Car"); //11
carType.addItem("Tank Car"); //12
carType.addItem("Grain Hopper"); //13
carType.addItem("Open Top Hopper"); //14
carType.addItem("Gondola"); //15
carType.addItem("Ore Jennie"); //16
carType.addItem("Flat Car"); //17
carType.addItem("Centerbeam Car"); //18
carType.addItem("Intermodal Flat"); //19
carType.addItem("Stock Car"); //20
carType.addItem("Steel Coil Car"); //21
carType.addItem("Bulkhead Flat"); //22
carType.addItem("Heavy Flat Car"); //23
carType.addItem("Covered Hopper"); //24


mainPanel.add(carType);

JLabel carNumStart=new JLabel("Enter Starting Car Number in the box:");
mainPanel.add(carNumStart);

JTextField carID=new JTextField(12);
mainPanel.add(carID);

JLabel carNumEnd=new JLabel("Enter Ending Car Number (200 car Max):");
mainPanel.add(carNumEnd);
JTextField carIDend=new JTextField("GATX 123457", 12);
mainPanel.add(carIDend);

JLabel origionator=new JLabel(" Enter Origionator");
mainPanel.add(origionator);
JTextField origion=new JTextField("Slim", 10);
mainPanel.add(origion);

JLabel emailtext=new JLabel("Enter email address or instuctions:");
mainPanel.add(emailtext);
JTextField emailbox=new JTextField("redbeardtheroadguy@hotmail.com", 25);
mainPanel.add(emailbox);

JLabel twentyline=new JLabel("Enter extra instuctions shown with car on covering with mouse");
mainPanel.add(twentyline);
JTextField twentylinebox=new JTextField("bill to Zuni, NM when empty", 25);
mainPanel.add(twentylinebox);

JLabel yardNumlab=new JLabel(" Enter yard number for cut of cars to arrive at:");
mainPanel.add(yardNumlab);
JTextField yardNumfield=new JTextField("1100 ", 5);
mainPanel.add(yardNumfield);

JLabel tracklabel=new JLabel(" Enter yard exit number for cut to arrive on: (track #):");
mainPanel.add(tracklabel);
JTextField trackfield=new JTextField("1", 2);
mainPanel.add(trackfield);


buttoncreate=new JButton("Create .trn file");
buttoncreate.addActionListener(bl);
mainPanel.add(buttoncreate);
this.add(mainPanel);
buttonquit=new JButton("Quit");
mainPanel.add(buttonquit);


this.add(mainPanel);
this.setVisible(true);
}

public class ButtonListener implements ActionListener{

public void actionPerformed (ActionEvent e){


if(e.getSource()==buttoncreate){
String start=carID.getText(); //This is the line it complains about
if(name.length()==0);
buttoncreate.setText("no entry");
}


}
/else{


buttoncreate.setText("bad");
}

// basically i want to put all of the JTextFields and JComboBox into
// separate String variables that I can use to make another piece of the program (diskwriting)
// I think Micheal Dunn is right but I am unsure how to change it to make it work!
// Thanks for you time!!
// roba
+Pie Number of slices to send: Send
> String start=carID.getText(); //This is the line it complains about

class members:
JTextField carID, carIDend,etc

in your constructor:
JTextField carID=new JTextField(12);

reasons given in earlier post.

to fix, change
JTextField carID=new JTextField(12);
to
carID=new JTextField(12);
+Pie Number of slices to send: Send
Thanks that is what my problem was. Thanks.
roba
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1389 times.
Similar Threads
What is the best way to create GUI components- in-line, methods, inner classes or separate classes
Blank Frame or Panel
Box.createGlue() creates nothing
Dynamic Swing
Two separate classes (not inner classes) and ActionListener question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:50:22.