I've hit a snag on a beginner Java problem involving building occupancy. I'm supposed to get user input from a GUI. My main problem is that I'm totally lost on how to take the information from the user and implement it in my GUI. I have the display set up the way I want but can't use the input. I don't quite know what to do. Whenever I hit one of the buttons I get "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" at pretty much everywhere. Would any of you be able to take a look at my code and find out what I'm doing wrong and give me insight on how to fix it.
Thanks
Expand|Select|Wrap|Line Numbers
- import java.util.Scanner;
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class Building3
- {
- static JFrame frame;
- static Container pane;
- static JLabel totalRes;
- static JLabel total;
- static int sumOccupants;
- static JLabel average;
- static JLabel avgApt;
- static double avgPer;
- static JLabel above;
- static JLabel avgAbove;
- static int noAbove;
- static JLabel below;
- static JLabel avgBelow;
- static int noBelow;
- static JPanel panel;
- static JTextField apartment;
- static int aptNo;
- static JTextField residents;
- static int[] occupants;
- static JButton store;
- static JButton quit;
- static TotalHandler operation;
- static class TotalHandler implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- String whichButton;
- whichButton = event.getActionCommand();
- if (whichButton.equals("Store"))
- {
- aptNo = Integer.valueOf(apartment.getText()).intValue();
- occupants[aptNo] = Integer.valueOf(residents.getText()).intValue();
- }
- else if (whichButton.equals("Quit"))
- {
- for (int aptNo = 0; aptNo < occupants.length; aptNo++)
- sumOccupants = sumOccupants + occupants[aptNo];
- avgPer = sumOccupants / aptNo;
- if (occupants[aptNo] > avgPer)
- noAbove++;
- if (occupants[aptNo] < avgPer)
- noBelow++;
- }
- total.setText("" + sumOccupants);
- avgApt.setText("" + avgPer);
- avgAbove.setText("" + noAbove);
- avgBelow.setText("" + noBelow);
- apartment.setText("");
- residents.setText("");
- }
- }
- public static void main(String[] args)
- {
- final int BUILDING_SIZE = 10;
- final int MAX_OCCUPANTS = 20;
- operation = new TotalHandler();
- int[] occupants = new int[BUILDING_SIZE];
- occupants[aptNo] = 0;
- sumOccupants = 0;
- avgPer = 0.0;
- noAbove = 0;
- noBelow = 0;
- totalRes = new JLabel("Total Residents:");
- total = new JLabel("0", JLabel.RIGHT);
- average = new JLabel("Avg number of residents per apt:");
- avgApt = new JLabel("0.0", JLabel.RIGHT);
- above = new JLabel("Above average occupancy number:");
- avgAbove = new JLabel("0", JLabel.RIGHT);
- below = new JLabel("Below average occupancy number:");
- avgBelow = new JLabel("0", JLabel.RIGHT);
- apartment = new JTextField("Enter apartment number");
- String str = apartment.getText();
- residents = new JTextField("Enter number of residents");
- String str1 = residents.getText();
- store = new JButton("Store");
- quit = new JButton("Quit");
- store.addActionListener(operation);
- quit.addActionListener(operation);
- frame = new JFrame();
- frame.setSize(500, 200);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- pane = frame.getContentPane();
- pane.setLayout(new GridLayout(6, 2));
- pane.add(totalRes);
- pane.add(total);
- pane.add(average);
- pane.add(avgApt);
- pane.add(above);
- pane.add(avgAbove);
- pane.add(below);
- pane.add(avgBelow);
- pane.add(apartment);
- pane.add(residents);
- pane.add(store);
- pane.add(quit);
- frame.setVisible(true);
- }
- }