因为 你的 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);
}
}
拿到外面就好了
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);
}
}
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);
}
}
正确程序如下:
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);
}
}
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)
{
}
}
}
多谢高人