0

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:

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
2
  • Whee did you initialize "map"? Looks null to me. Commented Dec 7, 2011 at 10:10
  • Did you init 'map' somewhere? I see only initialization of localMap... Commented Dec 7, 2011 at 10:11

8 Answers 8

9

Seems like you never instantiate map. You declare it here

static TreeMap<String, Integer> map;

but when you use it here it is still null giving you the NullPointerException.

map.put("Hello!", x);

If you do this before

map = new TreeMap<String, Integer>();

it should run fine.

Sign up to request clarification or add additional context in comments.

2 Comments

cool that doesn't return the null anymore...now the line where it says connection con = null; then I do con.setAutoCommit(false); the thing here is it is returning null but clearly because I set it null in the line before. if I dont, it says it is not initialized. anyway out?
duffymo gives you the solution for the con problem :)
3

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.

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.

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.

3 Comments

I know the code is messy but the map after testing the table solution instead seemed better for this purpose. I am attempting this for the first time so yeh it might not look good. Thanks though :)
maybe its being serialised as a caching mechanism or as a way to maintain state. See Fowler's Enterprise Patterns book about different reasons for doing that.
Fair enough. Good point, Ilan. The OP just needs to realize that it's not normalized. The implication is that s/he won't be able to search on individual values in the Map. Storing and retrieving are "all or none" operations.
2

You never initialize map to anything, hence it is 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:

static TreeMap<String, Integer> map = new TreeMap<String, Integer>();

Comments

1

the variable map is never initialized, only declared in the constructor.

map = new TreeMap<String, Integer>();

Comments

1

you only declare map,but not initialize it

static TreeMap<String, Integer> map=new HashMap<String,Integer>();

Comments

1
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>();

Comments

1

You r initializing a variable Connection con = null; then u r setting con.setAutoCommit(false).. Without creating object for that how can u set auto commit for that?

Comments

0

you did initialize map object as you initialized localMap object at declaration just check that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.