不知道什麼是錯的......它應該工作,或者可能錯過了某些東西?以下是代碼:添加到地圖時的Java異常
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);
}
}
}
錯誤是在main方法的線路是:
map.put("Hello!", x);
它給:
Exception in thread "main" java.lang.NullPointerException
at core.smd.classes.TestOracleMap.main(TestOracleMap.java:61)
Java Result: 1
你是否初始化「地圖」?對我看起來是空的。 – duffymo
你有沒有初始化'地圖'?我只看到localMap的初始化...... – Volker