here is my one class (controller) and the other (GUI)
what is happening is inside the SearchListener inner class for the button, I am calling a method from the controller class (i have already tested the parameter sent and that part works fine) but when i click on a code in the gui and then hit the button "search", I am getting this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI$SearchListener.actionPerformed(GUI.java:137) ----> this is the line where I call the "get info" method from the controller class...
CONTROLLER CLASS
import java.util.*;
import java.util.Collections;
import java.io.*;
import java.net.URLConnection;
import java.net.URL;
/**
* Class controls operation of program
* credit given to:
* http://www.java-tips.org/java-se-tips/java.io/how-to-read-file-in-java.html
* & http://www.wellho.net/mouth/1239_End-of-File-on-a-Java-BufferedReader.html
* & http://snippets.dzone.com/posts/show/3553
* & http://tutorials.jenkov.com/java-io/inputstreamreader.html
* & http://www.kodejava.org/examples/272.html
* @author some changes made by me: Carine Callo
* @field BufferedReader breader connects with file reader and allows program to read line by line
* @field String icaoCode holds result from one line from the file
* @field int count holds count of icao objects
*/
public class Controller
{
private ArrayList<String> codes = new ArrayList();
private ArrayList<Icao> icaoArray;
private BufferedReader breader;
private String icaoCode = "";
private int count = 0; //count for number of code objects
private Controller controllerRef; //reference back to controller
private GUI g;
/**
* CONSTRUCTOR
* instantiates readers for reading file content
*/
public Controller()
{
try {
breader = new BufferedReader(new FileReader("input.txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
createArray();
readFile();
}
/**
* reads each line from file and queries server
*/
public void readFile()
{
int i = 0; //index for array
try{
while(breader.ready()) //if it is not ready to read means end of file reached
{
icaoCode = breader.readLine(); //reads one line from file
query(icaoCode); //sends code read from file to query method
}
g = new GUI(codes.toArray(new String[codes.size()]), controllerRef);
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* queries server and stores result in a string
* @param String c resulting String from reading one ICAO code from file
* @field URLConnection urlc object that allows connection to website for query
*/
public void query(String c) throws Exception
{
String result = ""; //holds result from reading web server
//used to read the result from the webpage
BufferedReader bufferedResult = new BufferedReader(
new InputStreamReader(
new URL("http://api.geonames.org/weatherIcaoJSON?ICAO=" + c + "&formatted=true&username=carinlynchin").openStream()));
while(bufferedResult.ready())
{
result += bufferedResult.readLine().trim();
}
createIcaoObj(result);
}
/**
* creates an ICAO object
*/
public void createIcaoObj(String i)
{
if(!i.contains("value"))
{
Icao icaoObj = new Icao(i);
codes.add(icaoObj.getIcaoCode());
addToArray(icaoObj);
}
else
System.out.println(icaoCode + " has no observation");
}
/**
* puts object into array of comparable objects
*/
public void addToArray(Icao iObj)
{
//adds objects to array
icaoArray.add(iObj);
}
/**
* creates new array for icao objects
*/
public void createArray()
{
//creates new array of Icao objects
icaoArray = new ArrayList();
}
/**
* gets code selected and calls ICAO object methods to place into gui
*/
public void getInfo(String c)
{System.out.println("inside getinfo method");
// for(int i=0; i<icaoArray.size(); i++)
// {
// if(icaoArray.get(i).getIcaoCode().equals(c))
// {
// g.clouds.setText(icaoArray.get(i).getClouds());
// }
// }*/
}
}
AND MY GUI CLASS....
import java.awt.*;
import javax.swing.*;
import java.util.*;
import javax.swing.border.*;
import javax.swing.JList;
import java.awt.event.*;
public class GUI
{
private JFrame frame;
protected JLabel clouds, weather, observation, windDirection, windSpeed,
icao, seaLevelPressure, elevation, countryCode, lng, lat,
temperature, dew, humidity, stationName, dateTime;
private JButton search, close;
private JPanel panel1, panel2, panel3, panel4;
private JList choices;
private String codes;
private String userChoice;
protected Controller ref;
/**
* CONSTRUCTOR
*/
public GUI(String[] c, Controller r)
{
ref = r;
//LABEL INSTANTIATIONS & setting borders for panels
clouds = new JLabel();
clouds.setPreferredSize(new Dimension(80,50));
clouds.setBorder(new TitledBorder("Clouds"));
weather = new JLabel();
weather.setBorder(new TitledBorder("Weather Conditions"));
observation = new JLabel();
observation.setBorder(new TitledBorder("Observation"));
windDirection = new JLabel();
windDirection.setBorder(new TitledBorder("Wind Direction"));
windSpeed = new JLabel();
windSpeed.setBorder(new TitledBorder("Wind Speed"));
icao = new JLabel();
icao.setBorder(new TitledBorder("Icao Code"));
seaLevelPressure = new JLabel();
seaLevelPressure.setBorder(new TitledBorder("Sea Level Pressure"));
elevation = new JLabel();
elevation.setBorder(new TitledBorder("Elevation"));
countryCode = new JLabel();
countryCode.setBorder(new TitledBorder("Country Code"));
lng = new JLabel();
lng.setBorder(new TitledBorder("Longitude"));
lat = new JLabel();
lat.setBorder(new TitledBorder("Latitude"));
temperature = new JLabel();
temperature.setBorder(new TitledBorder("Temperature"));
dew = new JLabel();
dew.setBorder(new TitledBorder("Dew Point"));
humidity = new JLabel();
humidity.setBorder(new TitledBorder("Humidity"));
stationName = new JLabel();
stationName.setBorder(new TitledBorder("StationName"));
dateTime = new JLabel();
dateTime.setBorder(new TitledBorder("Date & Time"));
//OTHER INSTANTIATIONS
frame = new JFrame("Weather");
search = new JButton("Search");
search.setMaximumSize(new Dimension(80,50));
search.addActionListener(new SearchListener());
close = new JButton("Close");
close.setMaximumSize(new Dimension(80,50));
close.addActionListener(new CloseListener());
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
choices = new JList(c);
choices.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
choices.setLayoutOrientation(JList.VERTICAL);
choices.setFixedCellHeight(20);
choices.setFixedCellWidth(50);
choices.setVisibleRowCount(10);
JScrollPane listScroller = new JScrollPane(choices);
listScroller.setPreferredSize(new Dimension(100,200));
//add text fields to panel1
panel1.setLayout(new GridLayout(8,2,10,10));
panel1.add(clouds);
panel1.add(weather);
panel1.add(observation);
panel1.add(windDirection);
panel1.add(windSpeed);
panel1.add(icao);
panel1.add(seaLevelPressure);
panel1.add(elevation);
panel1.add(countryCode);
panel1.add(lat);
panel1.add(lng);
panel1.add(temperature);
panel1.add(dew);
panel1.add(humidity);
panel1.add(stationName);
panel1.add(dateTime);
//add to panel2
panel2.add(search);
panel2.add(close);
//add to panel3
panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
panel3.add(panel1);
panel3.add(Box.createRigidArea(new Dimension(50,50)));
panel3.add(panel2);
//add to panel4
panel4.add(listScroller);
//set up frame
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setSize(500,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel3);
frame.add(panel4);
frame.pack();
}
/*
* INNER CLASS FOR BUTTON ACTION LISTENER for search button
*/
private class SearchListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//get selected item from jlist
//pull information from icao object corresponding to it
userChoice = (String)choices.getSelectedValue();
ref.getInfo(userChoice);
}
}
/*
* INNER CLASS FOR BUTTON ACTION LISTENER for close button
*/
private class CloseListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}