GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Jun-2011, 07:51
macko macko is offline
New Member
 
Join Date: Jun 2011
Posts: 4
macko is on a distinguished road

NullPointerException problem


Here is my error:
JAVA Code:
	at java.awt.Container.add(Unknown Source)
	at frmClient.panelSetup(frmClient.java:117)
	at frmClient.<init>(frmClient.java:65)
	at frmClient.main(frmClient.java:48)


Here is my code:
JAVA Code:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class frmClient implements ActionListener{
	
	
		public static JFrame[] arrFrame = new JFrame[1];

		// Define JPanels for layout
		public static JPanel[] panel = new JPanel[16];
		
		// Define labels
		public static JLabel lblStatus;
		
		// Define JTextFields
		public static JFormattedTextField[] txtBox = new JFormattedTextField[75];

		// Define JComboBox's
		public static JComboBox comBoxA;
		
		// Define integers
		public static int[] arrNumber = new int[58];
		
		// Define doubles
		public static double[] arrDouble = new double[17];

		// Define JButtons
		public static JButton btnSave, btnClear, btnClose, btnCalculate;
		
		// Define boolean
		public static boolean ctrlLock;
	
	// Define custom RGB colors for table(s)
	static Color colTableA = new Color(194, 250, 203);
	static Color colTableB = new Color(242, 235, 150);
	
	// Main method
	public static void main(String[] args) {
		new frmClient(); 
	}
	
	public frmClient(){		
		
		// Create the main JFrame that will hold all the panels & Add data Frame
		arrFrame[0] = new JFrame("Company Name");
		arrFrame[0].setVisible(true);
		arrFrame[0].setResizable(false);
		arrFrame[0].setBounds(450, 300, 800, 570);
		
		// Create panel to hold all of the panels
		panel[0] = new JPanel();
		panel[0].setBackground(Color.WHITE);
		panel[0].setForeground(Color.GREEN);

		// Extracted methods
		panelSetup();
		
	}

	private void panelSetup() {
		// Create panelLogo for the company logo
		panel[1] = new JPanel();
		panel[1].add(new JLabel("                                      Factory Emissions                                      "));
		
		// Create panel1 to hold label(s) at top
		panel[2] = new JPanel();
		panel[2].setBackground(Color.WHITE);
		panel[2].add(new JLabel("Time              "));
		panel[2].add(new JLabel("Monday          "));
		panel[2].add(new JLabel("Tuesday           "));
		panel[2].add(new JLabel("Wednesday          "));
		panel[2].add(new JLabel("Thursday           "));
		panel[2].add(new JLabel("Friday                           "));
		panel[2].add(new JLabel("Weekly Average"));

		// Create panel2 to hold first line of JTextField(s)
		panel[3] = new JPanel();
		panel[3].setBackground(Color.WHITE);
		
		for(int i = 0; i > 74; i++){
			
			for(int a = 3; a > 14; a++){
				if(a == 11){
					// Create panel11 to hold the line seperator(s)
					panel[a] = new JPanel();
					panel[a].setBackground(Color.WHITE);
					panel[a].add(new JLabel("___________________________________________________" +
							"_______________________________________________________"));
				} // End IF
				else {
					
				panel[a] = new JPanel();
				panel[a].setBackground(Color.WHITE);
				txtBox[i] = new JFormattedTextField();
				txtBox[i].setHorizontalAlignment(JTextField.RIGHT);
				txtBox[i].setColumns(8);
				panel[a].add(txtBox[i]);
				} // End ELSE
		}
			
		}
		
		// Add all the panels to the main panel
		arrFrame[0].add(panel[0]);
		panel[0].add(panel[1]);
		panel[0].add(panel[2]);
		panel[0].add(panel[3]);
		panel[0].add(panel[4]);
		panel[0].add(panel[5]);
		panel[0].add(panel[6]);
		panel[0].add(panel[7]);
		panel[0].add(panel[8]);
		panel[0].add(panel[9]);
		panel[0].add(panel[10]);
		panel[0].add(panel[11]);
		panel[0].add(panel[12]);
		panel[0].add(panel[13]);
		panel[0].add(panel[14]);
		panel[0].add(panel[15]);
					
		// Create a new anwser array
		String[] arrComboBox = new String [2];
		arrComboBox[0] = "Disabled";
		arrComboBox[1] = "Enabled";
		
		// Create comboboxA and add arrComboBox data into selector
		comBoxA = new JComboBox(arrComboBox);
		comBoxA.setBackground(Color.white);
		comBoxA.setForeground(Color.MAGENTA);
		comBoxA.addActionListener(this);
		
		// Set boolean state
		ctrlLock = false;
		
		// Create panel14 for the buttons & Button settings
		/*panel14 = new JPanel();
		panel14.setBackground(Color.WHITE);
		panel14.add(btnClear = new JButton("Clear this weeks data"));
		panel14.add(btnCalculate = new JButton("Calculate"));
		panel14.add(new JLabel("                      "));
		panel14.add(new JLabel("Lock Controls: "));
		panel14.add(comBoxA);
		panel14.add(new JLabel("                      "));
		panel14.add(btnSave = new JButton("Save Data"));
		panel14.add(btnClose = new JButton("Close"));
		*/
		
		// Add Time & Button ActionListener(s)
		//btnClear.addActionListener(this);
		//btnSave.addActionListener(this);
		//btnClose.addActionListener(this);
		//btnCalculate.addActionListener(this);
		
	}


	

public void actionPerformed(ActionEvent e) {
	
	// Execute the code if btnCalculate has focus and lock controls is off
	if(btnCalculate.hasFocus()){
		if(ctrlLock == false){

		}
		else if(ctrlLock == true){
			JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
		}
	}
	
	// Execute the code if btnClose has focus
	if(btnClose.hasFocus()) { 
		if(ctrlLock == false){
			System.exit(0);
			}
		else if (ctrlLock == true){
			JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
		}
		}
	
	if(comBoxA.getSelectedIndex()== 0){
		
		// Set boolean state
		ctrlLock = false;
	}
	else if(comBoxA.getSelectedIndex()== 1){
		ctrlLock = true;
	}
	
	// Execute the code if btnSave is clicked
	if(e.getSource() == btnSave){
		if(ctrlLock == false){
			
		}
		else if(ctrlLock == true){
			JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
		}
	}
	
	// Execute the code if btnClear is clicked
	if(e.getSource() == btnClear){
		if(ctrlLock == false){
			
		
		
		}
		else if(ctrlLock == true){
			JOptionPane.showMessageDialog(arrFrame[0], "Control lock is currently enabled");
		}
	}	
}





	
	
	//public void week5PM(){
		
		//intCalculate = intMonI + intTueI + intWedI + intThuI + intFriI;
		//dblWkI = intCalculate / 5;
		//txtWAI.setText(Double.toString(dblWkI));
		
	//}
	
	//public void calcAverage(){
		
		//dblCal = dblWkA + dblWkB + dblWkC + dblWkD + dblWkE + dblWkF + dblWkG
		//+ dblWkH + dblWkI;
		
		//dblWA = dblCal;
		//txtWAJ.setText(Double.toString(dblWA));
		
		//dblWB = dblCal / 9;
		//txtWAK.setText(Double.toString(dblWB));
		
	//}
	
}
	


i know its something simple although ive been having this problem ever since i started using for loops. The program starts when compiling and displays up to the 2nd panel. it seems to stop before the JFormattedTextFields come in. I have been looking and i do have a feeling its to do with my for loop. although if somebody could tell me how to solve this before i tackle the problem myself id still be thankful . Also i do know there are similar threads already listed. I had looked at one i tryed a few solutions but had no luck.

The error lines:
Code:
Line 117: panel[0].add(panel[4]);
Code:
Line 65: // Extracted methods panelSetup();

Code:
Line 48: new frmClient();

Thank you in advance. Also i apologize for the beginner question as i am half a sleep been workin to much this week
Last edited by LuciWiz : 17-Jun-2011 at 09:33. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 17-Jun-2011, 08:06
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: NullPointerException problem


I didn't try to compile it or run the program, but I did see this:
JAVA Code:
for(int a = 3; a > 14; a++){
When 'a' starts as 3, it will not be greater than 14 so it will exit the loop immediately.
  #3  
Old 17-Jun-2011, 08:14
macko macko is offline
New Member
 
Join Date: Jun 2011
Posts: 4
macko is on a distinguished road

Re: NullPointerException problem


hmm so the middle for statement should be a < 14 which is meaning execute while a is less then 14 am i correct
  #4  
Old 17-Jun-2011, 08:28
macko macko is offline
New Member
 
Join Date: Jun 2011
Posts: 4
macko is on a distinguished road

Re: NullPointerException problem


problem solved. thanks simple had to change the > 14 to < 15 it seems i was forgetting a panel which is why i had the null error
  #5  
Old 17-Jun-2011, 09:15
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: NullPointerException problem


Typically, when looping over an array, you do it like this:
JAVA Code:
for(int i = 0; i < ArrayLength; i++)
  #6  
Old 17-Jun-2011, 17:14
macko macko is offline
New Member
 
Join Date: Jun 2011
Posts: 4
macko is on a distinguished road

Re: NullPointerException problem


ye im new with the for loop. Guess i had it the wrong way around
  #7  
Old 08-Sep-2017, 23:59
priya465 priya465 is offline
Account Disabled
 
Join Date: Aug 2017
Posts: 16
priya465 is an unknown quantity at this point

Re: NullPointerException problem


You can do that locally by navigating in a command prompt to the directory and running java Example.

BufferedReader has the readLine you want like so:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String answer = reader.readLine();
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ge-Force FX 5600 XT 128 DDR Ultra/760 Problem Crazyfist Computer Hardware Forum 3 07-Jun-2004 19:15
A Problem with Accessibility on MAC IE Melvin Lin Web Design Forum 1 19-May-2004 05:30
GeForce FX 5600XT Problem too!!! 2funky Computer Hardware Forum 1 11-May-2004 05:25
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:12.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.