Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: error adding textboxes to panel

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default error adding textboxes to panel

    yet another null pointer exception, altho i cannot see why it will not allow me to add the textbox to the panel. code:

    // Put the data in the panels
    		for(int a = 2; a < 16; a++){
    			if(a == 12){
    				// Create panel11 to hold the line seperator(s)
     
    				panel[a] = new JPanel();
    				panel[a].setForeground(Color.BLUE);
    				panel[a].setBackground(Color.WHITE);
    				panel[a].add(new JLabel("___________________________________________________" +
    						"_______________________________________________________"));
    			} // End IF
     
    			else if(a == 2){
    				panel[a] = new JPanel();
    				panel[a].setBackground(Color.WHITE);
    				panel[a].setForeground(Color.BLUE);
     
    				for(b = 0; b <= 6; b++){
     
    					panel[a].add(arrLabel[b]);
    					arrLabel[b].setForeground(Color.PINK);
    				}
    			}
     
    			else {
     
    				panel[a] = new JPanel();
    				panel[a].setBackground(Color.WHITE);
    				panel[a].setForeground(Color.BLUE);
     
    		for(int i = 0; i < 76; i++){
    			if (i % 7 == 6) { a++; }
     
    			txtBox[i] = new JFormattedTextField();
    			txtBox[i].setHorizontalAlignment(JFormattedTextField.RIGHT);
    			txtBox[i].setColumns(9);
    			txtBox[i].setBackground(Color.PINK);
    			txtBox[i].setForeground(Color.RED);
    			if(i == 0){txtBox[0].setBackground(colTableB); txtBox[0].setEditable(false);}
    			if(i == 6){txtBox[6].setBackground(colTableA); txtBox[6].setEditable(false);}
     
    			THIS IS WHAT IS GIVING THE PROBLEM:
                                      panel[a].add(txtBox[i]);
     
    		}
    			} // End ELSE	
    		}

    Compiler Error:
    Exception in thread "main" java.lang.NullPointerException
    	at frmClient.panelSetup(frmClient.java:127)
    	at frmClient.<init>(frmClient.java:65)
    	at frmClient.main(frmClient.java:46)

    So the error is leading to the line that i have made bold. without the if statement in the loop it seems to work.
    the program is simple. 75 textboxes 7 in each panel. that is all that needs knowing for the mean time. So simply wondering what is the compiler saying about this line. Why can i not add the textbox to the panel with the if statement, Unless its because im declaring the new panel above the if statement and it doesnt know which panel im trying to add to as it panel[i] would have not been created yet. if im correct let me know although im going to try the solution and see if it solves the problem. if so ill post saying solved


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: error adding textboxes to panel

    ok yes im sorry for the trouble of reading this. i was correct. i guess i should have tried it earlier. I have fixed the problem by simply removing the code to declare a new panel from inside the for loop. to a new method which i had declared all 16 panels. and then ran the method before the for loop was executed. Simple fix.

Similar Threads

  1. Adding a Panel from a different class
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 25th, 2011, 08:28 AM
  2. Panel Problems?
    By Dellick17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2010, 02:31 PM
  3. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 07:07 PM
  4. Adding panels to a central panel.
    By Johannes in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2010, 06:31 PM
  5. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 05:00 PM