sys*_*bug 0 java map nullpointerexception
不确定有什么问题......它应该工作还是可能缺少什么?以下是代码:
public class TestOracleMap implements java.io.Serializable{
static TreeMap<String, Integer> map;
static TreeMap<String, Integer> localMap = new TreeMap<String, Integer>();
public static void StoreMapInDB(TreeMap<String, Integer> map) throws
IOException, FileNotFoundException{
try {
PreparedStatement insertMap = null;
//String insertString = "INSERT INTO TESTMAP(ID, MPFIELD) VALUES (1, ?)";
Connection con=null;
con.setAutoCommit(false);
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
"jdbc:oracle:thin:@oXXX",
"XXX",
"XXX");
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
ObjectOutputStream out = new ObjectOutputStream(bos);
out = new ObjectOutputStream(bos) ;
out.writeObject(map);
out.close();
byte[] buf = bos.toByteArray();
PreparedStatement prepareStatement = con.prepareStatement("insert into
TESTMAP(ID,MAPFIELD)values(?,?)");
prepareStatement.setLong(1, 1);
prepareStatement.setBinaryStream(2, new ByteArrayInputStream(buf), buf.length);
// insertMap.executeUpdate();
con.commit();
} catch(Exception e){e.printStackTrace();}
}
public static void main(String[] args)
{
try{
DateTime today = new DateTime();
int x = 1;
map.put("Hello!", x);
StoreMapInDB(map);
}catch(IOException ioe){
System.err.print(ioe);
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误在main方法的行中是:
map.put("Hello!", x);
Run Code Online (Sandbox Code Playgroud)
它给:
Exception in thread "main" java.lang.NullPointerException
at core.smd.classes.TestOracleMap.main(TestOracleMap.java:61)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)
好像你永远不会实例化map.你在这里声明
static TreeMap<String, Integer> map;
Run Code Online (Sandbox Code Playgroud)
但是当你在这里使用它仍然null给你NullPointerException.
map.put("Hello!", x);
Run Code Online (Sandbox Code Playgroud)
如果你以前这样做过
map = new TreeMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
它应该运行正常.
| 归档时间: |
|
| 查看次数: |
1298 次 |
| 最近记录: |