[英]Java exception when adding to map
Not sure what is wrong...it should work or maybe am missing something? 不知道出什么问题了...它应该可以工作,或者可能丢失了某些东西? the following is the code:
以下是代码:
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);
}
}
}
the error is in the line in the main method that is: 错误在main方法的一行中,即:
map.put("Hello!", x);
it gives: 它给:
Exception in thread "main" java.lang.NullPointerException
at core.smd.classes.TestOracleMap.main(TestOracleMap.java:61)
Java Result: 1
Seems like you never instantiate map
. 好像您从不实例化
map
。 You declare it here 你在这里声明
static TreeMap<String, Integer> map;
but when you use it here it is still null
giving you the NullPointerException
. 但是在这里使用它时,它仍然为
null
为您提供NullPointerException
。
map.put("Hello!", x);
If you do this before 如果您之前这样做
map = new TreeMap<String, Integer>();
it should run fine. 它应该运行良好。
Where did you initialize "map"? 您在哪里初始化“地图”? Looks null to me.
在我看来是空的。
Change this line: 更改此行:
static TreeMap<String, Integer> map = new TreeMap<String, Integer>();
You declare this and set it to null, but I don't see where you use it. 您对此进行声明并将其设置为null,但是我看不到在哪里使用它。
PreparedStatement insertMap = null;
You've got more heartache ahead here: 您在这里还有更多的心痛:
Connection con=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
"jdbc:oracle:thin:@oXXX",
"XXX",
"XXX");
con.setAutoCommit(false);
Move the autoCommit down until after you get the connection from the driver manager. 向下移动autoCommit,直到从驱动程序管理器获得连接为止。
You're serializing the Map to INSERT it into the database? 您要序列化地图以将其插入数据库吗? That's not normalized.
这没有被标准化。 A normalized schema would have a row per entry.
规范化的架构每个条目将有一行。
The more I read your code, the less sense it makes. 我阅读您的代码越多,它的意义就越小。 Good luck.
祝好运。
You never initialize map
to anything, hence it is null
. 您永远不会初始化
map
到任何东西,因此它为null
。 You'll need to assign a valid TreeMap<String, Integer>
to it somewhere, like for instance on line 2 when you declare it, use: 您需要在某处为其分配有效的
TreeMap<String, Integer>
,例如在声明它的第2行,例如,使用:
static TreeMap<String, Integer> map = new TreeMap<String, Integer>();
变量映射永远不会初始化,仅在构造函数中声明。
map = new TreeMap<String, Integer>();
您只声明地图,而不初始化它
static TreeMap<String, Integer> map=new HashMap<String,Integer>();
static TreeMap<String, Integer> map = new TreeMap<String, Integer>(); // <-- in your code, you're not constructing it
static TreeMap<String, Integer> localMap = new TreeMap<String, Integer>();
You r initializing a variable Connection con = null; 您可以初始化变量Connection con = null; then ur setting con.setAutoCommit(false).. Without creating object for that how can u set auto commit for that?
然后我们设置con.setAutoCommit(false)..如果不为此创建对象,如何设置自动提交呢?
您确实在声明时初始化了map对象,就像您在声明时初始化了localMap对象一样,只是检查了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.