473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Storing and Using user input in GUI

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
3 10783
jkmyoung
2,057 Expert 2GB
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
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
2,057 Expert 2GB
(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

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

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
6
by: Rob | last post by:
Hi, Can anyone point me in the direction of how to store information after the ? in the url in form fields. i.e. http://url/test.asp?User=Rob in Form Field UserName Many thanks,
0
by: Larry Neylon | last post by:
Hi there, I'm currently trying to implement a website that will store and retrieve Polish, so I need to be able to handle Polish characters using classic ASP with MySql5. Does anybody have an...
6
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
2
by: Mythran | last post by:
We followed an example found on MSDN to create an encrypted FormsAuthenticationTicket and storing the ticket in a cookie. Is this the "correct" way to store the authentication ticket? We are...
6
by: J055 | last post by:
Hi I have the following code. I upload an XML file using the FileUpload object, store the stream in a session so the user gets the chance to confirm some options then pass the stream from the...
10
by: drago | last post by:
Hello guys again! Back with invoice problems. Just want to know how to store data in the main database in different tables and fields using a form. I have a datasheet view of a form where i have to...
2
by: bagerbeiger | last post by:
I am relatively new to programming (3-5 months) and have to write a program that prompts users for input (number of items ordered at a restaurant), performs some monetary calculations, and then...
8
by: yomama | last post by:
Hello, I am writing a program in which I take a string (specifically, first and last name with a space in between) from the user and put it into a data member of a class node. I have tried various...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.