NullPointerException / problem with listeners?
So I have my program running, the panel shows up etc, however when I click any of the radio buttons I get the following error and I haven't figured out why:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at WeatherForm07$RBListener.actionPerformed(WeatherForm07.java:364)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:291)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
"
Any help/suggestions much appreciated! =D
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
publicclass WeatherForm07extends JPanel
{
/** Width of JFrame */
privateint WIDTH = 400;
/** Height of JFrame */
privateint HEIGHT = 375;
/** Main Window to hold form */
private JFrame frame;
/** The form within the frame */
private JPanel panel;
/** Main title on the form */
private JLabel lblTitle;
/** Label to hold the name of the programmer */
private JLabel lblProgrammer;
/** Label at top of airport names */
private JLabel lblAirport;
/** Label at top of nine altitudes */
private JLabel lblAltitude;
/** Label to hold the Wind Direction */
private JLabel lblDir;
/** Label to hold Wind Speed */
private JLabel lblSp;
/** Label to hold the Wind Temperature */
private JLabel lblTemp;
/** Static label at left of Wind Direction */
private JLabel lblPDir;
/** Static label at left of Wind Speed */
private JLabel lblPSp;
/** Static label at left of Wind Temperature */
private JLabel lblPTemp;
/** The Exit Button */
private JButton btnExit;
//Reference 701
/** Radio buttons for the Airport or Weather Station ID's */
private JRadioButton [] Apt =new JRadioButton[21];
/** Radio buttons for the nine altitudes */
private JRadioButton [] Alt =new JRadioButton[9];
//Reference 702
/** Object containing the code for decoding the Nat Weather Svc FD report */
private NWSFB06[] wea;
//Reference 703
private String[] strApt =new String[21];
/** Temporary variable to hold Airport Name */
private String strAirport;
/** Temporary variable to hold a number indicating
which Airport radio button has been selected */
privateint intAirport;
/** Array of Altitude Labels */
private String[] strAlt =
{"3","6","9","12","18","24","30","34","39"};
/** Temporary variable to hold Altitude Labels */
private String strAltitude;
/** Temporary variable to hold a number indicating
which altitude radio button has been selected */
privateint intAltitude;
private JTextArea TA;
/** Weather Constructor */
public WeatherForm07()
{
intAirport = 0;//initialize Airport
intAltitude = 3;//initialize Altitude
strAltitude ="3";//initialize Altitude
//Reference 704
frame =new JFrame("Asgn A19007");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/** Setting the Layout Manager to null enables you to
precisely position controls using x, y coordinates */
panel =new JPanel(null);
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
panel.setBackground(Color.lightGray);
lblTitle =new JLabel("Aviation Center");
/** This is an example of how to set the font */
lblTitle.setFont(new Font("Helvetica", Font.BOLD,24));
/** The setSize() method sets the width and height of the
object. 410 is the width, 20 is the height */
lblTitle.setSize(410,20);
/** The setLocation() method is where you set the x,y
location of the object. In this case, the
title is set 15 pixels from the left margin
and 25 pixels from the top margin */
lblTitle.setLocation(15,25);
/** Setting the color of the font */
lblTitle.setForeground(Color.blue);
/** Now that you are done setting the properties,
add it to the panel. */
panel.add(lblTitle);
lblProgrammer =new JLabel("programmed by ...");
lblProgrammer.setSize(200,20);
lblProgrammer.setLocation(100,50);// 100 from Left, 50 from Top
panel.add(lblProgrammer);
/** Radio Buttons - Airports */
lblAirport =new JLabel("Airport");
lblAirport.setSize(200,20);
lblAirport.setLocation(15,100);
panel.add(lblAirport);
//Reference 705
/** Here you will put code that will:
/** Open the file and read the first record */
int counter = 0;
try
{
Scanner fileScan =new Scanner(new File("FBIN.txt"));
/** The following has the effect of skipping the upper records */
fileScan.nextLine();// (Extracted From...)
fileScan.nextLine();// FDUW02 KWBC 231410
fileScan.nextLine();// DATA BASED ON 231200Z
fileScan.nextLine();// VALID 231800Z
fileScan.nextLine();// Blank
fileScan.nextLine();// FT 30006000
while (fileScan.hasNext())
{
/** Get each record and pass to NWSFD */
String strStaWea = fileScan.nextLine();
if ((strStaWea.length()) < 70)
{
strStaWea +=" ";
}
NWSFB06 fd =new NWSFB06(strStaWea);
//System.out.println(strStaWea.length());
wea =new NWSFB06[21];
wea[counter] = fd;// OR new NWSFB06(strStaWea);
//System.out.println("begin problem?");
strApt[counter] = fd.getStationID();
counter++;
}
}
catch (FileNotFoundException problem)
{
System.out.println("File not found.");
}
//Reference 707
/** Add your radio buttons for Airports here
To do this declare an array of JRadioButtons above
and then set the title, size and location */
RBListener aptListener =new RBListener();
ButtonGroup groupApt =new ButtonGroup();
int x = 15, y = 100;
int count = 0;
for (int rowCount = 0; rowCount < 3; rowCount++)//loop to count the row
{
y += 25;//top
x = 15;//left
for (int colCount = 0; colCount < 7; colCount++)// loop to count the column
{
Apt[count] =new JRadioButton(strApt[count],true);// create a JRadioButton object
Apt[count].addActionListener (aptListener);// create the actionLinstener
Apt[count].setSize(60,20);//wide, high - set the size
Apt[count].setLocation(x,y);
groupApt.add (Apt[count]);// add the object to the radio button group
panel.add(Apt[count]);
System.out.println(count);
count++;
x += 60;
}
}
/** Label for the Altitude */
lblAltitude =new JLabel("Altitude in Thousands");
lblAltitude.setSize(200,20);
lblAltitude.setLocation(15,175);
panel.add(lblAltitude);
/** Add you radio buttons for Altitude here */
RBListener altListener =new RBListener();
ButtonGroup groupAlt =new ButtonGroup();
for (count = 0; count < 9; count++)
{
Alt[count] =new JRadioButton(strAlt[count],true);
groupAlt.add (Alt[count]);
Alt[count].addActionListener (altListener);
Alt[count].setSize(40,20);// wide, high
x = 200;// pixels from top
y = (count * 40) + 15;// pixels from left
Alt[count].setLocation(y,x);// left,top
panel.add(Alt[count]);
}
/** Labels for the Answers */
lblPDir =new JLabel("Direction: ");
lblPDir.setSize(100,20);
lblPDir.setLocation(15,225);
panel.add(lblPDir);
lblDir =new JLabel("--");
lblDir.setSize(100,10);
lblDir.setLocation(115,225);
panel.add(lblDir);
lblPSp =new JLabel("Speed: ");
lblPSp.setSize(100,20);
lblPSp.setLocation(15,250);
panel.add(lblPSp);
lblSp =new JLabel ("--");
lblSp.setSize(100,10);
lblSp.setLocation(115,250);
panel.add(lblSp);
lblPTemp =new JLabel("Temp C: ");
lblPTemp.setSize(100,20);
lblPTemp.setLocation(15,275);
panel.add(lblPTemp);
lblTemp =new JLabel("--");
lblTemp.setSize(100,20);
lblTemp.setLocation(115,275);
panel.add(lblTemp);
/** Buttons */
btnExit =new JButton("Exit");
btnExit.addActionListener (new ButtonListener());
btnExit.setSize(350,20);
btnExit.setLocation(15,300);
panel.add(btnExit);
frame.getContentPane().add(panel);
}//end of DemoForm07
publicvoid display()
{
frame.pack();
frame.setVisible(true);
}
/** Code for what to do when the user presses EXIT */
privateclass ButtonListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent event)
{
if (event.getSource() == btnExit)
{
System.exit(0);//return to operating system
}
}
}//end of ButtonListener
/** Code for what to do when the user clicks on
one of the radio buttons */
privateclass RBListenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent event)
{
Object source = event.getSource();
for (int count = 0; count < 21; count++)
{
if (source == Apt[count])
{
intAirport = count;
}
}
for (int count = 0; count < 9; count++)
{
if (source == Alt[count])
{
strAltitude = strAlt[count];
}
}
/** Use your NWSFD object to determine the wind
direction, speed and temperature. */
lblDir.setText(wea[intAirport].getWindDir(strAltitude));
lblSp.setText(wea[intAirport].getWindSpeed(strAltitude));
lblTemp.setText(wea[intAirport].getWindTemp(strAltitude));
lblAirport.setText("Airport=" +strApt[intAirport]);
lblAltitude.setText("Altitude=" +strAltitude +"000");
}//end of action performed
}//end of RBListener
}
[19496 byte] By [
eeka] at [2007-11-27 3:29:47]

post the class NWSFB06 plz
import java.util.*;
import java.io.*;
/**
NWSFB06.java - Receives Station Weather string extracting and formatting
data before returning it. Implements the NWS interface.
*/
public class NWSFB06 implements NWS
{
String strStationWeather, strAlt;
public NWSFB06 (String strStaWea)
{
strStationWeather = strStaWea + " ";
}
/** Returns the weather in one somewhat condensed but user friendly line
@param strAlt A 2 character string representing the altitude in thousands
@return A line with the weather for a location and altitude*/
public String getPrintLine (String strAlt)
{
return "The weather for " + getStationID() + " at " + strAlt + "000 feet is "
+ getAltWea(strAlt) + "";
}
/** Returns index of weather information for an altitude.
@param strAlt A 2 character string representing the altitude in thousands
@return An integer representing the starting point in stStationWeather for
the data associated with the altitude entered in the strAlt parameter*/
public String getStationID ()
{
String result;
result = strStationWeather.substring(0,3);
return result;
}
/** Returns weather information for an altitude.
@param strAlt A 2 character string representing the altitude in thousands
@return A 7 character string that contains weather data for a specific altitude*/
public String getAltWea(String strAlt)
{
String strOut = "";
int intPosition;
intPosition = getPos(strAlt);
/** Extracts the seven character string from Station Weather
Adds a space on the end so that we can easily work with the string */
strOut = strStationWeather.substring(intPosition,intPosition + 7) + " ";
/** We are looking for the end of the Altitude Weather and then
we pad the end with more spaces */
strOut = strOut.substring(0,strOut.indexOf(" ",0)) + "";
/** Now chop the thing down to EXACTLY seven spaces */
strOut = strOut.substring(0,7);
return strOut;
}
/** Returns index of weather information for an altitude.
@param strAlt A 2 character string representing the altitude in thousands
@return An integer representing the starting point in stStationWeather for
the data associated with the altitude entered in the strAlt parameter*/
public int getPos (String strAlt)
{
int intPos = Integer.parseInt(strAlt);
switch (intPos)
{
case 3:
intPos = 4;
break;
case 6:
intPos = 9;
break;
case 9:
intPos = 17;
break;
case 12:
intPos = 25;
break;
case 18:
intPos = 33;
break;
case 24:
intPos = 41;
break;
case 30:
intPos = 49;
break;
case 34:
intPos = 56;
break;
case 39:
intPos = 63;
break;
default:
break;
}
return intPos;
}
/** At a given altitude, returns the Wind Direction.
@param strAlt A 2 character string representing the altitude in thousands
@return A 3 character string indicating the direction of the wind in degrees*/
public String getWindDir(String strAlt)
{
String strWindDir;
int intPosition, intCompare;
intPosition = getPos(strAlt);
strWindDir = strStationWeather.substring(intPosition,intPosition + 2);
/** Checks for blanks and returns N/A if that is the case. If not, determines if the
direction is within normal parameters. If it is a zero is simply appended to the string.
Otherwise the direction is formatted first. Also checks to see if the Wind Speed is "calm"
and returns N/A for the direction in that case*/
if (strWindDir.equals(" "))
strWindDir = "N/A";
else
{
intCompare = Integer.parseInt(strWindDir);
if (intCompare >= 1 && intCompare <= 36)
{
strWindDir = intCompare + "0";
}
else if (intCompare >= 51 && intCompare <= 86)
{
intCompare -= 50;
strWindDir = intCompare + "0";
}
else if (intCompare == 99)
strWindDir = "N/A";
}
return strWindDir;
}
/** At a given altitude, returns the Wind Speed, in knots, as a two or three digit string.
@param strAlt A 2 character string representing the altitude in thousands
@return A 2-3 character string indicating the Wind Speed in knots*/
public String getWindSpeed(String strAlt)
{
String strWindSpeed, strCompare;
int intPosition, intCompare, intConversion;
intPosition = getPos(strAlt);
strWindSpeed = strStationWeather.substring(intPosition + 2,intPosition + 4);
strCompare = strStationWeather.substring(intPosition, intPosition + 2);
/** Checks for blanks and returns N/A if that is the case. Otherwise checks the
Wind Direction string to determine if the speed is over 99 knots or is calm formatting
the data if either exception occurs. If not it returns the knots as originally found
in the strStationWeather substring.*/
if (strWindSpeed.equals(" "))
strWindSpeed = "N/A";
else
{
intCompare = Integer.parseInt(strCompare);
intConversion = Integer.parseInt(strWindSpeed);
if (intCompare >= 1 && intCompare <= 36)
{
}
else if (intCompare >= 51 && intCompare <= 86)
{
intConversion += 100;
strWindSpeed = intConversion + "";
}
else if (intCompare == 99)
strWindSpeed = "calm";
}
return strWindSpeed;
}
/**Returns Wind Temperature based on altitude.
@param strAlt A 2 character string representing the altitude in thousands
@return A 3 character indicating Wind Temperature in degrees Celsius*/
public String getWindTemp(String strAlt)
{
String strWindTemp;
int intPosition, intAlt;
intPosition = getPos(strAlt);
strWindTemp = strStationWeather.substring(intPosition + 4,intPosition + 7);
/**Ensures that the temperatures returned for above 24000 feet are
automatically negative or if the altitude is 3000 "N/A" is returned
at there is no temperature information for this altitude.*/
intAlt = Integer.parseInt(strAlt);
if (intAlt > 24)
strWindTemp = "-" + strWindTemp;
else
if (intAlt < 6)
strWindTemp = "N/A";
return strWindTemp;
}
/** Returns the fully formatted weather for a location and altitude
@param strAlt A Two character string representing the altitude in thousands
@return A paragraph with the fully formatted weather data*/
public String getWeather(String strAlt)
{
String strAltitude, strStationID, strWeather;
strAltitude = " " + strAlt + "000 ";
strStationID = getStationID();
strWeather = getAltWea(strAlt);
return getPrintLine(strAlt)
+"\r\n which translates into:\r\n"
+ "Wind Direction of\t" + getWindDir(strAlt) + " degrees\r\n"
+ "Wind Speed of\t" + getWindSpeed(strAlt) + " knots\r\n"
+ "Wind Temperature of\t" + getWindTemp(strAlt) + " degrees Celsius\r\n";
}
/*
public void status(String strVar)
{
}
*/
}
eeka at 2007-7-12 8:32:47 >

I think your problem is here :
while (fileScan.hasNext()) {
/** Get each record and pass to NWSFD */
String strStaWea = fileScan.nextLine();
if ((strStaWea.length()) < 70) {
strStaWea += " ";
}
NWSFB06 fd = new NWSFB06(strStaWea);
// System.out.println(strStaWea.length());
wea = new NWSFB06[21];///////////////////////// EVERY TIME, YOU CREATE THE SAME NWSFB06 TABLE
wea[counter] = fd;// OR new NWSFB06(strStaWea);
// System.out.println("begin problem?");
strApt[counter] = fd.getStationID();
counter++;
}
is this what you want :
wea = new NWSFB06[21];
while (fileScan.hasNext()) {
/** Get each record and pass to NWSFD */
String strStaWea = fileScan.nextLine();
if ((strStaWea.length()) < 70) {
strStaWea += " ";
}
NWSFB06 fd = new NWSFB06(strStaWea);
// System.out.println(strStaWea.length());
wea[counter] = fd;// OR new NWSFB06(strStaWea);
// System.out.println("begin problem?");
strApt[counter] = fd.getStationID();
counter++;
}
But if you do this, are you sure not to exceed 21 NWSFB06 ?
a suggestions:
import java.util.*;
...
List myList = new ArrayList();
while (fileScan.hasNext()) {
/** Get each record and pass to NWSFD */
String strStaWea = fileScan.nextLine();
if ((strStaWea.length()) < 70) {
strStaWea += " ";
}
NWSFB06 fd = new NWSFB06(strStaWea);
// System.out.println(strStaWea.length());
myList.add(fd);///////////CHANGE HERE
// System.out.println("begin problem?");
strApt[counter] = fd.getStationID();
counter++;
}
wea = (NWSFB06[])myList.toArray();
Message was edited by:
java_2006
plz post a the FBIN.txt content
Ahh yes, you are right, when I put thewea = new NWSFB06[21];
before the loop the program will now display results properly instead of crashing. It would be better to have an arraylist but I'm not sure if I want to go through the heache of putting it in since I will make more errors to correct....I'm not sure how to reference a indexed item in an arraylist, what does that look like?
here is a sample FBIN.txt:
(Extracted from FBUS31 KWNO 060203)
FD1US1
DATA BASED ON 060000Z
VALID 060600ZFOR USE 0200-0900Z. TEMPS NEG ABV 24000
FT 300060009000120001800024000 30000 34000 39000
BIH30080305+07 1012-01 1213-15 1523-26 152244 152952 152961
BLH 2206 1511+16 1914+10 1816+03 1821-14 1733-28 194741 205950 216460
FAT 0815 0916+15 0707+07 1608-01 1617-15 1333-27 113243 122953 142459
FOT 0207 1907+13 1616+06 1826-02 2019-14 2207-25 331341 341651 321663
ONT 9900 9900+14 9900+08 1809+01 1625-17 1826-30 163244 183350 202955
RBL 9900 1406+15 1511+08 1808-01 2020-16 9900-25 201742 211452 270663
SAC 0605 1205+16 1311+08 1709-01 1615-15 1515-27 111542 141652 200961
SAN 9900 9900+14 1811+08 1717+01 1822-17 1930-29 203943 214049 224156
SBA 3105 9900+13 1107+07 1408+00 1513-17 1517-31 112344 121850 161055
SFO 3005 1106+14 1114+07 1314-01 1418-15 1111-26 080942 111051 990060
SIY1208+14 1813+07 2016-01 2218-15 2110-25 221342 331351 340863
WJF0413+14 0706+07 2008+00 1730-16 1524-28 123244 143051 172557
AST 1521 1919+10 1924+04 2026-04 2131-15 2329-24 242841 243152 253365
IMB 2410+05 2711-02 2123-14 2113-26 290742 301052 251065
LKV 1905+07 2311-01 2214-15 2208-26 232242 222552 251064
OTH 1207 1821+12 2027+05 2034-03 2117-14 2209-24 241041 270852 272064
PDX 1225 1814+11 2122+04 2122-04 2123-14 2317-25 252142 252252 262865
RDM1310+12 2011+05 2418-03 2017-15 1911-26 990042 310751 260764
GEG1312+05 1906+00 2611-04 2929-16 2937-27 304442 294553 294866
SEA 1508 1811+09 2017+02 2120-04 2325-16 2629-25 262842 253752 254465
YKM 0710 1406+06 2210+03 2415-03 2421-15 2623-25 283042 282652 273265
eeka at 2007-7-12 8:32:47 >

ArrayList is very simple to use. It's a kind of dynamic array.
This class has some usefull methods:
- get(int index) : which returns the object at the index
- size() : returns the size
- add(Object o): Appends the specified element to the end of this list.
See :
http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
By the way, you have the same problem with strApt[]
(strApt[counter] = fd.getStationID();)
Ahh, thank you very much, if I run into too much more trouble I may post it here haha ^_^
eeka at 2007-7-12 8:32:47 >

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
import java.io.*;
public class WeatherForm07 extends JPanel {
/** Width of JFrame */
private int WIDTH = 400;
/** Height of JFrame */
private int HEIGHT = 375;
/** Main Window to hold form */
private JFrame frame;
/** The form within the frame */
private JPanel panel;
/** Main title on the form */
private JLabel lblTitle;
/** Label to hold the name of the programmer */
private JLabel lblProgrammer;
/** Label at top of airport names */
private JLabel lblAirport;
/** Label at top of nine altitudes */
private JLabel lblAltitude;
/** Label to hold the Wind Direction */
private JLabel lblDir;
/** Label to hold Wind Speed */
private JLabel lblSp;
/** Label to hold the Wind Temperature */
private JLabel lblTemp;
/** Static label at left of Wind Direction */
private JLabel lblPDir;
/** Static label at left of Wind Speed */
private JLabel lblPSp;
/** Static label at left of Wind Temperature */
private JLabel lblPTemp;
/** The Exit Button */
private JButton btnExit;
// Reference 701
/** Radio buttons for the Airport or Weather Station ID's */
private JRadioButton[] Apt = new JRadioButton[21];
/** Radio buttons for the nine altitudes */
private JRadioButton[] Alt = new JRadioButton[9];
// Reference 702
/** Object containing the code for decoding the Nat Weather Svc FD report */
private NWSFB06[] wea;
// Reference 703
private String[] strApt;// = new String[21];
/** Temporary variable to hold Airport Name */
private String strAirport;
/**
* Temporary variable to hold a number indicating which Airport radio button has been selected
*/
private int intAirport;
/** Array of Altitude Labels */
private String[] strAlt = { "3", "6", "9", "12", "18", "24", "30", "34", "39" };
/** Temporary variable to hold Altitude Labels */
private String strAltitude;
/**
* Temporary variable to hold a number indicating which altitude radio button has been selected
*/
private int intAltitude;
private JTextArea TA;
/** Weather Constructor */
public WeatherForm07() {
intAirport = 0; // initialize Airport
intAltitude = 3; // initialize Altitude
strAltitude = "3"; // initialize Altitude
// Reference 704
frame = new JFrame("Asgn A19007");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/**
* Setting the Layout Manager to null enables you to precisely position controls using x, y coordinates
*/
panel = new JPanel(null);
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
panel.setBackground(Color.lightGray);
lblTitle = new JLabel("Aviation Center");
/** This is an example of how to set the font */
lblTitle.setFont(new Font("Helvetica", Font.BOLD, 24));
/**
* The setSize() method sets the width and height of the object. 410 is the width, 20 is the height
*/
lblTitle.setSize(410, 20);
/**
* The setLocation() method is where you set the x,y location of the object. In this case, the title is set 15
* pixels from the left margin and 25 pixels from the top margin
*/
lblTitle.setLocation(15, 25);
/** Setting the color of the font */
lblTitle.setForeground(Color.blue);
/**
* Now that you are done setting the properties, add it to the panel.
*/
panel.add(lblTitle);
lblProgrammer = new JLabel("programmed by ...");
lblProgrammer.setSize(200, 20);
lblProgrammer.setLocation(100, 50); // 100 from Left, 50 from Top
panel.add(lblProgrammer);
/** Radio Buttons - Airports */
lblAirport = new JLabel("Airport");
lblAirport.setSize(200, 20);
lblAirport.setLocation(15, 100);
panel.add(lblAirport);
// Reference 705
/**
* Here you will put code that will:
*
* /** Open the file and read the first record
*/
int counter = 0;
try {
Scanner fileScan = new Scanner(new File("FBIN.txt"));
/** The following has the effect of skipping the upper records */
fileScan.nextLine(); // (Extracted From...)
fileScan.nextLine(); // FDUW02 KWBC 231410
fileScan.nextLine(); // DATA BASED ON 231200Z
fileScan.nextLine(); // VALID 231800Z
fileScan.nextLine(); // Blank
fileScan.nextLine(); // FT 3000 6000
List weaList = new ArrayList();///////////CHANGE HERE
List strAptList = new ArrayList();///////////CHANGE HERE
while (fileScan.hasNext()) {
/** Get each record and pass to NWSFD */
String strStaWea = fileScan.nextLine();
if ((strStaWea.length()) < 70) {
strStaWea += " ";
}
NWSFB06 fd = new NWSFB06(strStaWea);
// System.out.println(strStaWea.length());
weaList.add(fd);///////////CHANGE HERE
/*wea = new NWSFB06[21];
wea[counter] = fd;// OR new NWSFB06(strStaWea);
*/
// System.out.println("begin problem?");
//strApt[counter] = fd.getStationID();
strAptList.add(fd.getStationID());///////////CHANGE HERE
counter++;
}
wea = new NWSFB06[weaList.size()];///////////CHANGE HERE
for (int i = 0; i < weaList.size(); i++) {///////////CHANGE HERE
wea[i] = (NWSFB06)weaList.get(i);///////////CHANGE HERE
}
strApt = new String[strAptList.size()];///////////CHANGE HERE
for (int i = 0; i < strAptList.size(); i++) {///////////CHANGE HERE
strApt[i] = (String)strAptList.get(i);///////////CHANGE HERE
}
}
catch (Exception e) {///////////CHANGE HERE
System.out.println("File not found.");
e.printStackTrace();
}
// Reference 707
/**
* Add your radio buttons for Airports here To do this declare an array of JRadioButtons above and then set the
* title, size and location
*/
RBListener aptListener = new RBListener();
ButtonGroup groupApt = new ButtonGroup();
int x = 15, y = 100;
int count = 0;
for (int rowCount = 0; rowCount < 3; rowCount++)// loop to count the row
{
y += 25;// top
x = 15;// left
for (int colCount = 0; colCount < 7; colCount++)// loop to count the column
{
Apt[count] = new JRadioButton(strApt[count], true); // create a JRadioButton object
Apt[count].addActionListener(aptListener);// create the actionLinstener
Apt[count].setSize(60, 20); // wide, high - set the size
Apt[count].setLocation(x, y);
groupApt.add(Apt[count]); // add the object to the radio button group
panel.add(Apt[count]);
System.out.println(count);
count++;
x += 60;
}
}
/** Label for the Altitude */
lblAltitude = new JLabel("Altitude in Thousands");
lblAltitude.setSize(200, 20);
lblAltitude.setLocation(15, 175);
panel.add(lblAltitude);
/** Add you radio buttons for Altitude here */
RBListener altListener = new RBListener();
ButtonGroup groupAlt = new ButtonGroup();
for (count = 0; count < 9; count++) {
Alt[count] = new JRadioButton(strAlt[count], true);
groupAlt.add(Alt[count]);
Alt[count].addActionListener(altListener);
Alt[count].setSize(40, 20); // wide, high
x = 200; // pixels from top
y = (count * 40) + 15; // pixels from left
Alt[count].setLocation(y, x); // left,top
panel.add(Alt[count]);
}
/** Labels for the Answers */
lblPDir = new JLabel("Direction: ");
lblPDir.setSize(100, 20);
lblPDir.setLocation(15, 225);
panel.add(lblPDir);
lblDir = new JLabel("--");
lblDir.setSize(100, 10);
lblDir.setLocation(115, 225);
panel.add(lblDir);
lblPSp = new JLabel("Speed: ");
lblPSp.setSize(100, 20);
lblPSp.setLocation(15, 250);
panel.add(lblPSp);
lblSp = new JLabel("--");
lblSp.setSize(100, 10);
lblSp.setLocation(115, 250);
panel.add(lblSp);
lblPTemp = new JLabel("Temp C: ");
lblPTemp.setSize(100, 20);
lblPTemp.setLocation(15, 275);
panel.add(lblPTemp);
lblTemp = new JLabel("--");
lblTemp.setSize(100, 20);
lblTemp.setLocation(115, 275);
panel.add(lblTemp);
/** Buttons */
btnExit = new JButton("Exit");
btnExit.addActionListener(new ButtonListener());
btnExit.setSize(350, 20);
btnExit.setLocation(15, 300);
panel.add(btnExit);
frame.getContentPane().add(panel);
} // end of DemoForm07
public void display() {
frame.pack();
frame.setVisible(true);
}
/** Code for what to do when the user presses EXIT */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == btnExit) {
System.exit(0); // return to operating system
}
}
} // end of ButtonListener
/**
* Code for what to do when the user clicks on one of the radio buttons
*/
private class RBListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
for (int count = 0; count < 21; count++) {
if (source == Apt[count]) {
intAirport = count;
}
}
for (int count = 0; count < 9; count++) {
if (source == Alt[count]) {
strAltitude = strAlt[count];
}
}
/**
* Use your NWSFD object to determine the wind direction, speed and temperature.
*/
lblDir.setText(wea[intAirport].getWindDir(strAltitude));
lblSp.setText(wea[intAirport].getWindSpeed(strAltitude));
lblTemp.setText(wea[intAirport].getWindTemp(strAltitude));
lblAirport.setText("Airport=" + strApt[intAirport]);
lblAltitude.setText("Altitude=" + strAltitude + "000");
} // end of action performed
} // end of RBListener
public static void main(String[] args) {
new WeatherForm07().display();
}
}
So when I saw your post I was attempting something similar, using the code you showed in the last post I got an unchecked error so I added "-Xlint" to the compiler settings but I am still receiving three warnings:
-jGRASP exec: C:\Program Files\Java\jdk1.6.0\bin\javac -g C:\SchoolPrograms\Week7\WeatherForm07.java -Xlint
WeatherForm07.java:208: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
weaList.add(fd);///////////CHANGE HERE
^
WeatherForm07.java:210: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
strAptList.add(fd.getStationID());///////////CHANGE HERE
^
WeatherForm07.java:43: warning: [serial] serializable class WeatherForm07 has no definition of serialVersionUID
public class WeatherForm07 extends JPanel
^
3 warnings
Here is what I have for code right now, also I made comments to explain what is going on, I'm still a little confused as to the function of the loop code that appears to be intializing the lists... let me know what you think if you get a chance. =)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
import java.io.*;
public class WeatherForm07 extends JPanel
{
/** Width of JFrame */
private int WIDTH = 400;
/** Height of JFrame */
private int HEIGHT = 375;
/** Main Window to hold form */
private JFrame frame;
/** The form within the frame */
private JPanel panel;
/** Main title on the form */
private JLabel lblTitle;
/** Label to hold the name of the programmer */
private JLabel lblProgrammer;
/** Label at top of airport names */
private JLabel lblAirport;
/** Label at top of nine altitudes */
private JLabel lblAltitude;
/** Label to hold the Wind Direction */
private JLabel lblDir;
/** Label to hold Wind Speed */
private JLabel lblSp;
/** Label to hold the Wind Temperature */
private JLabel lblTemp;
/** Static label at left of Wind Direction */
private JLabel lblPDir;
/** Static label at left of Wind Speed */
private JLabel lblPSp;
/** Static label at left of Wind Temperature */
private JLabel lblPTemp;
/** The Exit Button */
private JButton btnExit;
//Reference 701
/** Radio buttons for the Airport or Weather Station ID's */
private JRadioButton [] Apt = new JRadioButton[21];
/** Radio buttons for the nine altitudes */
private JRadioButton [] Alt = new JRadioButton[9];
//Reference 702
/** Object containing the code for decoding the Nat Weather Svc FD report */
private NWSFB06[] wea;
//Reference 703
private String[] strApt;
/** Temporary variable to hold Airport Name */
private String strAirport;
/** Temporary variable to hold a number indicating
which Airport radio button has been selected */
private int intAirport;
/** Array of Altitude Labels */
private String[] strAlt =
{"3","6","9","12","18","24","30","34","39"};
/** Temporary variable to hold Altitude Labels */
private String strAltitude;
/** Temporary variable to hold a number indicating
which altitude radio button has been selected */
private int intAltitude;
private JTextArea TA;
/** Weather Constructor */
public WeatherForm07()
{
intAirport = 0; //initialize Airport
intAltitude = 3; //initialize Altitude
strAltitude = "3"; //initialize Altitude
//Reference 704
frame = new JFrame("Asgn A19007");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/** Setting the Layout Manager to null enables you to
precisely position controls using x, y coordinates */
panel = new JPanel(null); //"null" setting allows for
//manual placement of components
panel.setPreferredSize( new Dimension(WIDTH, HEIGHT));
panel.setBackground(Color.lightGray);
lblTitle = new JLabel("Aviation Center");
/** This is an example of how to set the font */
lblTitle.setFont(new Font("Helvetica", Font.BOLD,24));
/** The setSize() method sets the width and height of the
object. 410 is the width, 20 is the height */
lblTitle.setSize(410,20);
/** The setLocation() method is where you set the x,y
location of the object. In this case, the
title is set 15 pixels from the left margin
and 25 pixels from the top margin */
lblTitle.setLocation(15,25);
/** Setting the color of the font */
lblTitle.setForeground(Color.blue);
/** Now that you are done setting the properties,
add it to the panel. */
panel.add(lblTitle);
lblProgrammer = new JLabel("programmed by ");
lblProgrammer.setSize(200,20);
lblProgrammer.setLocation(100,50); // 100 from Left, 50 from Top
panel.add(lblProgrammer);
/** Radio Buttons - Airports */
lblAirport = new JLabel("Airport");
lblAirport.setSize(200,20);
lblAirport.setLocation(15,100);
panel.add(lblAirport);
//Reference 705
/** Here you will put code that will:
/** Open the file and read the first record */
int counter = 0;
try
{
Scanner fileScan = new Scanner(new File("FBIN.txt"));
/** The following has the effect of skipping the upper records */
fileScan.nextLine(); // (Extracted From...)
fileScan.nextLine(); // FDUW02 KWBC 231410
fileScan.nextLine(); // DATA BASED ON 231200Z
fileScan.nextLine(); // VALID 231800Z
fileScan.nextLine(); // Blank
fileScan.nextLine(); // FT 30006000
List weaList = new ArrayList();//Initializes weaList ArrayList
List strAptList = new ArrayList();//Initializes strAptList ArrayList
while (fileScan.hasNext())
{
/** Get each record and pass to NWSFD */
String strStaWea = fileScan.nextLine();
if ((strStaWea.length()) < 70)
{
strStaWea += " ";
}
NWSFB06 fd = new NWSFB06(strStaWea);
weaList.add(fd);//Ads new NWSFB06 to the list
strAptList.add(fd.getStationID());//Ads station IDs to the list
counter++;
}
wea = new NWSFB06[weaList.size()];////////////////////////Initializes list and?
for (int weaCount = 0; weaCount < weaList.size(); weaCount++)
{
wea[weaCount] = (NWSFB06)weaList.get(weaCount);
}
strApt = new String[strAptList.size()];////////////////////////Initializes list and?
for (int aptCount = 0; aptCount < strAptList.size(); aptCount++
{
strApt[aptCount] = (String)strAptList.get(aptCount);
}
}
catch (Exception error)//Catches exception if neccesary and prints the StackTrace
{
System.out.println("File not found.");
error.printStackTrace();
}
//Reference 707
/** Add your radio buttons for Airports here
To do this declare an array of JRadioButtons above
and then set the title, size and location */
RBListener aptListener = new RBListener();
ButtonGroup groupApt = new ButtonGroup();
int x = 15, y = 100;
int count = 0;
for (int rowCount = 0; rowCount < 3; rowCount++)//loop to count the row
{
y += 25;//top
x = 15;//left
for (int colCount = 0; colCount < 7; colCount++)// loop to count the column
{
Apt[count] = new JRadioButton(strApt[count],true); // create a JRadioButton object
Apt[count].addActionListener (aptListener);// create the actionLinstener
Apt[count].setSize(60,20); //wide, high - set the size
Apt[count].setLocation(x,y);
groupApt.add (Apt[count]); // add the object to the radio button group
panel.add(Apt[count]);
count++;
x += 60;
}
}
/** Label for the Altitude */
lblAltitude = new JLabel("Altitude in Thousands");
lblAltitude.setSize(200,20);
lblAltitude.setLocation(15,175);
panel.add(lblAltitude);
/** Add you radio buttons for Altitude here */
RBListener altListener = new RBListener();
ButtonGroup groupAlt = new ButtonGroup();
for (count = 0; count < 9; count++)
{
Alt[count] = new JRadioButton(strAlt[count], true);
groupAlt.add (Alt[count]);
Alt[count].addActionListener (altListener);
Alt[count].setSize(40,20);// wide, high
x = 200; // pixels from top
y = (count * 40) + 15;// pixels from left
Alt[count].setLocation(y,x); // left,top
panel.add(Alt[count]);
}
/** Labels for the Answers */
lblPDir = new JLabel("Direction: ");
lblPDir.setSize(100,20);
lblPDir.setLocation(15,225);
panel.add(lblPDir);
lblDir = new JLabel("--");
lblDir.setSize(100,10);
lblDir.setLocation(115,225);
panel.add(lblDir);
lblPSp = new JLabel("Speed: ");
lblPSp.setSize(100,20);
lblPSp.setLocation(15,250);
panel.add(lblPSp);
lblSp = new JLabel ("--");
lblSp.setSize(100,10);
lblSp.setLocation(115,250);
panel.add(lblSp);
lblPTemp = new JLabel("Temp C: ");
lblPTemp.setSize(100,20);
lblPTemp.setLocation(15,275);
panel.add(lblPTemp);
lblTemp = new JLabel("--");
lblTemp.setSize(100,20);
lblTemp.setLocation(115,275);
panel.add(lblTemp);
/** Buttons */
btnExit = new JButton("Exit");
btnExit.addActionListener (new ButtonListener());
btnExit.setSize(350,20);
btnExit.setLocation(15,300);
panel.add(btnExit);
frame.getContentPane().add(panel);
} //end of WeatherForm07
public void display()
{
frame.pack();
frame.setVisible(true);
}
/** Code for what to do when the user presses EXIT */
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == btnExit)
{
System.exit(0); //return to operating system
}
}
} //end of ButtonListener
/** Code for what to do when the user clicks on
one of the radio buttons */
private class RBListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
for (int count = 0; count < 21; count++)
{
if (source == Apt[count])
{
intAirport = count;
}
}
for (int count = 0; count < 9; count++)
{
if (source == Alt[count])
{
strAltitude = strAlt[count];
}
}
/** Use your NWSFD object to determine the wind
direction, speed and temperature. */
lblDir.setText(wea[intAirport].getWindDir(strAltitude));
lblSp.setText(wea[intAirport].getWindSpeed(strAltitude));
lblTemp.setText(wea[intAirport].getWindTemp(strAltitude));
lblAirport.setText("Airport=" +strApt[intAirport]);
lblAltitude.setText("Altitude=" +strAltitude + "000");
}//end of action performed
}//end of RBListener
public static void main(String[] args)
{
new WeatherForm07().display();
}
}
eeka at 2007-7-12 8:32:47 >

