By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,992 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,992 IT Pros & Developers. It's quick & easy.

Storing and Using user input in GUI

P: 2
Hey all,
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
  1. import java.util.Scanner;
  2. import java.io.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. public class Building3 
  7. {
  8.  
  9.  
  10.     static JFrame frame;
  11.     static Container pane;
  12.     static JLabel totalRes;
  13.     static JLabel total;
  14.     static int sumOccupants;
  15.     static JLabel average;
  16.     static JLabel avgApt;
  17.     static double avgPer;
  18.     static JLabel above;
  19.     static JLabel avgAbove;
  20.     static int noAbove;
  21.     static JLabel below;
  22.     static JLabel avgBelow;
  23.     static int noBelow;
  24.     static JPanel panel;
  25.     static JTextField apartment;
  26.     static int aptNo;
  27.     static JTextField residents;
  28.     static int[] occupants;
  29.     static JButton store;
  30.     static JButton quit;
  31.     static TotalHandler operation;
  32.  
  33.     static class TotalHandler implements ActionListener
  34.     {
  35.         public void actionPerformed(ActionEvent event)
  36.         {
  37.             String whichButton;
  38.             whichButton = event.getActionCommand();
  39.             if (whichButton.equals("Store"))
  40.             {
  41.                 aptNo = Integer.valueOf(apartment.getText()).intValue();
  42.                 occupants[aptNo] = Integer.valueOf(residents.getText()).intValue();
  43.             }
  44.  
  45.             else if (whichButton.equals("Quit"))
  46.             {
  47.                 for (int aptNo = 0; aptNo < occupants.length; aptNo++)
  48.                     sumOccupants = sumOccupants + occupants[aptNo];
  49.                 avgPer = sumOccupants / aptNo;
  50.                 if (occupants[aptNo] > avgPer)
  51.                     noAbove++;
  52.                 if (occupants[aptNo] < avgPer)
  53.                     noBelow++;
  54.             }
  55.             total.setText("" + sumOccupants);
  56.             avgApt.setText("" + avgPer);
  57.             avgAbove.setText("" + noAbove);
  58.             avgBelow.setText("" + noBelow);
  59.             apartment.setText("");
  60.             residents.setText("");
  61.         }
  62.     }    
  63.     public static void main(String[] args)
  64.     {
  65.         final int BUILDING_SIZE = 10;
  66.         final int MAX_OCCUPANTS = 20;
  67.  
  68.         operation = new TotalHandler();
  69.         int[] occupants = new int[BUILDING_SIZE];
  70.  
  71.         occupants[aptNo] = 0;
  72.         sumOccupants = 0;
  73.         avgPer = 0.0;
  74.         noAbove = 0;
  75.         noBelow = 0;
  76.  
  77.         totalRes = new JLabel("Total Residents:");
  78.         total = new JLabel("0", JLabel.RIGHT);
  79.         average = new JLabel("Avg number of residents per apt:");
  80.         avgApt = new JLabel("0.0", JLabel.RIGHT);
  81.         above = new JLabel("Above average occupancy number:");
  82.         avgAbove = new JLabel("0", JLabel.RIGHT);
  83.         below = new JLabel("Below average occupancy number:");
  84.         avgBelow = new JLabel("0", JLabel.RIGHT);
  85.  
  86.         apartment = new JTextField("Enter apartment number");
  87.         String str = apartment.getText();
  88.         residents = new JTextField("Enter number of residents");
  89.         String str1 = residents.getText();
  90.  
  91.         store = new JButton("Store");
  92.         quit = new JButton("Quit");
  93.  
  94.         store.addActionListener(operation);
  95.         quit.addActionListener(operation);
  96.  
  97.         frame = new JFrame();
  98.         frame.setSize(500, 200);
  99.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  100.         pane = frame.getContentPane();
  101.         pane.setLayout(new GridLayout(6, 2));
  102.  
  103.         pane.add(totalRes);
  104.         pane.add(total);
  105.         pane.add(average);
  106.         pane.add(avgApt);
  107.         pane.add(above);
  108.         pane.add(avgAbove);
  109.         pane.add(below);
  110.         pane.add(avgBelow);
  111.         pane.add(apartment);
  112.         pane.add(residents);
  113.         pane.add(store);
  114.         pane.add(quit);
  115.         frame.setVisible(true);
  116.     }
  117. }
  118.  
When the store button is pressed the apartment number should go in the array and the residents number should be stored. Then the textFields should be empty for new input. When quit is pressed it should calculate the averages and other calculations. however, when I press a button it results in error.
Apr 8 '10 #1
Share this Question
Share on Google+
3 Replies


jkmyoung
Expert 100+
P: 2,057
in Main you've got
occupants[aptNo] = 0;
but you haven't set aptNo yet.


In your event handler you use
aptNo = Integer.valueOf(apartment.getText()).intValue();
without handling the case where the string isn't an integer, and you use

occupants[aptNo]
without checking that aptNo is a valid apartment number.
Apr 8 '10 #2

P: 2
Ok, so I've simplified my application and removed all of the "static" in it. When I run the application it no longer results in errors but I still haven't really moved anywhere.

I think I'm having serious issues with the JButtons. Right now, i'm failing to see why this isn't working. When I hit either the "store" or "quit" buttons nothing happens and I don't know what to do.

I feel as if when I'm trying to figure it out I'm over-complicating things and am missing the simple answer. If somebody would be able to review my new code and point out where I'm goofing up it would be great.

Thanks
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. class Building5 implements ActionListener
  5. {
  6.  
  7.  
  8.     JFrame frame = new JFrame();
  9.     Container pane = frame.getContentPane();
  10.     JLabel totalRes = new JLabel("Total Residents:");
  11.     JLabel total = new JLabel("0", JLabel.RIGHT);
  12.     JLabel average = new JLabel("Avg number of residents per apt:");
  13.     JLabel avgApt = new JLabel("0.0", JLabel.RIGHT);
  14.     JLabel above = new JLabel("Above average occupancy number:");
  15.     JLabel avgAbove = new JLabel("0", JLabel.RIGHT);
  16.     JLabel below = new JLabel("Below average occupancy number:");
  17.     JLabel avgBelow = new JLabel("0", JLabel.RIGHT);
  18.  
  19.     JTextField apartment = new JTextField("Enter apartment number");
  20.  
  21.     JTextField residents = new JTextField("Enter number of residents");
  22.  
  23.  
  24.     JButton store = new JButton("Store");
  25.     JButton quit = new JButton("Quit");
  26.  
  27.     public Building5()
  28.     {
  29.  
  30.         frame.setSize(500, 200);
  31.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.         pane = frame.getContentPane();
  33.         pane.setLayout(new GridLayout(6, 2));
  34.  
  35.         pane.add(totalRes);
  36.         pane.add(total);
  37.         pane.add(average);
  38.         pane.add(avgApt);
  39.         pane.add(above);
  40.         pane.add(avgAbove);
  41.         pane.add(below);
  42.         pane.add(avgBelow);
  43.         pane.add(apartment);
  44.         pane.add(residents);
  45.         pane.add(store);
  46.         pane.add(quit);
  47.         frame.setVisible(true);
  48.     }
  49.     public void actionPerformed(ActionEvent event)
  50.     {
  51.         final int BUILDING_SIZE = 10;
  52.         final int MAX_OCCUPANTS = 20;
  53.  
  54.  
  55.  
  56.         int[] occupants = new int[BUILDING_SIZE];
  57.  
  58.         int aptNo = 0;
  59.         occupants[aptNo] = 0;
  60.         int sumOccupants = 0;
  61.         double avgPer = 0.0;
  62.         int noAbove = 0;
  63.         int noBelow = 0;
  64.         String whichButton;
  65.         whichButton = event.getActionCommand();
  66.  
  67.         if (whichButton.equals("Store"))
  68.         {
  69.             String str = apartment.getText();
  70.             int aptValue = Integer.parseInt(str);
  71.             String str1 = residents.getText();
  72.             int resValue = Integer.parseInt(str1);
  73.  
  74.             aptNo = aptValue;
  75.             occupants[aptNo] = resValue;
  76.             apartment.setText("");
  77.             residents.setText("");
  78.         }
  79.  
  80.         else if (whichButton.equals("Quit") || MAX_OCCUPANTS > 20)
  81.         {
  82.             for (aptNo = 0; aptNo < occupants.length; aptNo++)
  83.                 sumOccupants = sumOccupants + occupants[aptNo];
  84.             avgPer = sumOccupants / aptNo;
  85.             if (occupants[aptNo] > avgPer)
  86.                 noAbove++;
  87.             if (occupants[aptNo] < avgPer)
  88.                 noBelow++;
  89.  
  90.         total.setText("" + sumOccupants);
  91.         avgApt.setText("" + avgPer);
  92.         avgAbove.setText("" + noAbove);
  93.         avgBelow.setText("" + noBelow);
  94.         apartment.setText("");
  95.         residents.setText("");
  96.         }
  97.         frame.setVisible(true);
  98.     }
  99.     public static void main(String[] args)
  100.     {
  101.         Building5 building = new Building5();
  102.     }
  103. }
  104.  
Apr 10 '10 #3

jkmyoung
Expert 100+
P: 2,057
(Repeat of above comment plus:)
In terms of style, you should be declaring your elements as class variables (you're mostly doing this), and setting them up in the constructor.


You're resetting the occupants everytime you click a button:
int[] occupants = new int[BUILDING_SIZE];
int[] occupants should be declared in the class variables section, and set up in the constructor.
Apr 12 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.