Exception in thread "main" java.lang.NullPointerException
at awt.GridLayoutTest.init(GridLayoutTest.java:40)
at awt.GridLayoutTest.<init>(GridLayoutTest.java:28)
at awt.GridLayoutTest.main(GridLayoutTest.java:70)
程序如下:
package awt;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
public class GridLayoutTest {
Frame frame = null;
Panel pm = null;
Panel pc = null;
Panel ps = null;
String[] s = {"7","8","9","/",
"sprt","4","5","6",
"*","%","1","2",
"3","-","1/x","0",
"+/-",".","+","="};
String[] m = {"MC","MR","MS","M+"};
String[] c = {"Backspace","CE","C"};
Button[] bs = new Button[s.length];
Button[] bm = new Button[m.length];
Button[] bc = new Button[c.length];
TextField tf = new TextField("0.",150);
public GridLayoutTest() {
init();
}
private void init() {
frame = new Frame("计算器 v1.0");
frame.setLayout(new GridLayout(4,5,3,3));
for(int i=0;i<bs.length;i++)
{
bs[i] = new Button(s[i]);
bs[i].setBackground(Color.LIGHT_GRAY);
bs[i].setForeground(Color.blue);
ps.add(bs[i]);
}
for(int j=0;j<bm.length;j++){
bm[j] = new Button(m[j]);
bm[j].setBackground(Color.LIGHT_GRAY);
bm[j].setForeground(Color.blue);
pm.add(bm[j]);
}
for(int k=0;k<bc.length;k++){
bc[k] = new Button(c[k]);
bc[k].setBackground(Color.LIGHT_GRAY);
bc[k].setForeground(Color.blue);
pc.add(bc[k]);
}
frame.add(tf,"North");
frame.add(pc,"Center");
frame.add(pm,"West");
frame.add(ps,"South");
frame.setSize(150,150);
frame.pack();
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new GridLayoutTest();
}
}
3 个解决方案
#1
ps = new Panel();
pm = new Panel();
pc = new Panel();
pm = new Panel();
pc = new Panel();
#2
噢.初始化.
#3
这些是数据类型不匹配导致的。
#1
ps = new Panel();
pm = new Panel();
pc = new Panel();
pm = new Panel();
pc = new Panel();
#2
噢.初始化.
#3
这些是数据类型不匹配导致的。