import java.lang.*;
import java.awt.*;
public class j013 extends Frame
{
Label L1;
  TextField T1;
Button B1;
Button B2;
 
 public j013()
{
this.setLayout(null);
L1=new Label("输入你的代号:");
L1.setBounds(40,50,80,25);
this.add(T1);
B1=new Button("Button1");
B1.setBounds(25,90,90,25);

this.add(B2);
this.setBackground(Color.yellow);
this.setBounds(0,0,250,220);
this.setTitle("this is Frame");
this.setVisible(true);
}
public static void main(String para[])
{
new j013();
}
}这是我摘抄的一段代码,编译没有问题,但运行时出现了
java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:621)
at java.awt.Container.add(Container.java:307)
at j013.<init>(j013.java:15)
at j013.main(j013.java:27)
Exception in thread "main" 
不知是怎么回事,请高手指点.package je3.basics;
public class Factorial2
{
long obj1=100;
Factorial2 obj2=new Factorial2();

public static long Factorial(long x)
{
if(x<0)
throw new IllegalArgumentException("x must be >=0");
if(x<=1) return 1;
else return x*factorial(x-1);

}
 System.out.println(obj2.Factorial(obj2.obj1)); 
}这是我自己设计的一个阶乘代码,总是有一个错误编译不过去,不知为什么?请高手指点一二.
Factorial2.java:15: <identifier> expected
 System.out.println(obj2.Factorial(obj2.obj1)); 
                           ^
1 error
这是错误显示.

解决方案 »

  1.   

    第一个是个空指针异常。在j013.java:15行第二个obj2.obj1是什么?
      

  2.   

    楼主可看下下面的代码
    <table bodle=1>
    <tr><td>class Factorial2 

    long obj1=4;  public static long Factorial(long x) 

    if(x <0) 
    throw new IllegalArgumentException("x must be > =0"); 

    if(x>=1)
    return x*Factorial(x-1); 
    else return 1;


    public static void main(String[] args)
    {
    Factorial2 obj2=new Factorial2();
    System.out.println(Factorial2.Factorial(obj2.obj1));  
    }

     
    </td></tr>
    </table>
      

  3.   

    this.add(T1);加上T1 之前你没  
    分配空间
    T1=new TextField();