import javax.swing.*;

import java.awt.*;


public   class keyboard 
 {
	
	
	public static void main(String args[])
	{
     String s[] = {"esc","F1","F2","F3","F4","F5",
    		        "F6","F7","F8","F9","F10","F11","F12",
    		         "psc","slk","pau"};           
        
        
     JButton j[] =  new JButton[s.length];   
     
     for(int i=0;i<s.length;i++)
         j[i].setText(s[i]);

     
     JFrame f = new JFrame();
	    
       for(int i=0;i<s.length;i++)
            f.add(j[i],JFrame.LEFT_ALIGNMENT);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.setVisible(true);
       
	}
	
	
}

I need Instant Help!. Code is giving me error of Null-Pointer Assignment, Please Any help me appreciated.

You may want to change your code like this:

import javax.swing.*;

import java.awt.*;


public   class keyboard {
		
  public static void main(String args[]) {
    String s[] = {"esc","F1","F2","F3","F4","F5",
                    "F6","F7","F8","F9","F10","F11","F12",
                    "psc","slk","pau"
    };
      
      
    JButton j[] =  new JButton[s.length];   
    
    for(int i=0; i < s.length; i++) {
      //initialise buttons
      j[i] = new JButton(s[i]);
      //j[i].setText(s[i]);
    }
   
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
          
    for(int i = 0; i < s.length; i++) {
      f.add(j[i]);
    }
  
    f.pack();
      
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
	
}

you got NullPointerException in your code because you haven't initialize button in your array.

JButton j[] =  new JButton[s.length];   

     //all button in above array is still null     
     for(int i=0;i<s.length;i++)
         j[i].setText(s[i]);

hope will help ..

JButton j[] =  new JButton[s.length];   
     
     for(int i=0;i<s.length;i++)
         j[i].setText(s[i]);

Line 1 creates the ARRAY of JButtons. It does not create any new JButton objects, so in line 4, you are setting text for a JButton that doesn't exist and you get a null pointer exception. Create the individual JButtons (see line 5 below).

JButton j[] =  new JButton[s.length];   
     
     for(int i=0;i<s.length;i++)
     {
         j[i] = new JButton ();
         j[i].setText(s[i]);
     }

[edit]
Someone beat me to it. :)
[/edit]

commented: Thank You +2

Thank You Very Much it solved my problem....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.