因为 你的 n1 和n2 在try中定义的 
拿到外面就好了 
public class hash
{
Integer n1,n2;
public static void main(String args[])
{
try{
             Hashtable h1=new Hashtable();
             h1.put("one",new Integer(1));
             h1.put("two",new Integer(2));

              n1=(Integer)h1.get("one");
              n2=(Integer)h1.get("two");           }
        catch(NullPointerException e)
          {

        } System.out.print(n1);
System.out.print(n2);
}
}

解决方案 »

  1.   

    刚才的有点小错误import java.util.*;
    public class hash{    public static void main(String args[])
        {
    Integer n1 = new Integer(0);
    Integer n2 = new Integer(0); try{
        Hashtable h1=new Hashtable();
        h1.put("one",new Integer(1));
        h1.put("two",new Integer(2));     n1=(Integer)h1.get("one");
        n2=(Integer)h1.get("two"); }
    catch(NullPointerException e)
    { } System.out.print(n1);
    System.out.print(n2);
        }
    }
      

  2.   

    你在try中定义的变量相当于try结构体中的局部变量,所以结束try后,它会自动销毁的,所以你要把变量定义放到外面。
    正确程序如下:
    import java.util.*;
    public class hash
    {

    public static void main(String args[])
    {
    Integer n1 = null;
    Integer n2 = null;
    try{
                 Hashtable h1=new Hashtable();
                 h1.put("one",new Integer(1));
                 h1.put("two",new Integer(2));

                  n1=(Integer)h1.get("one");
                  n2=(Integer)h1.get("two");           }
            catch(NullPointerException e)
              {

            } System.out.print(n1);
    System.out.print(n2);
    }
    }
      

  3.   

    行了!但是需要把    System.out.print(n1);
     System.out.print(n2);
    方到try中去!
    即:
    import java.util.*;
    public class hash
    {


    public static void main(String args[])
    {
    Integer n1;
    Integer n2;
    try{
                 Hashtable h1=new Hashtable();
                 h1.put("one",new Integer(1));
                 h1.put("two",new Integer(2));

                 n1=(Integer)h1.get("one");
                 n2=(Integer)h1.get("two");
                 System.out.print(n1);
            System.out.print(n2);           }
            catch(NullPointerException e)
              {

            }
    }
    }
    多谢高人