Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static final constant goes null

Tags:

android

junit

I have a class that extends from ServiceTestCase to test my Android Service implementation.

public class MainCoreServiceTest extends ServiceTestCase<CoreService> {

    protected static final String DB_PATH = "/data/data/"
            + CoreService.class.getPackage().getName() + "/databases/"
            + DatabaseManager.DB_NAME;

    // More code
}

The problem I am facing is that if I run only one test method, everything is fine, but if I launch the whole class (which contains several test methods) then on second test method I get an ExceptionInInitializerError and I found out this is because of DB_PATH being null. This is the method where this happens:

private void wipeOutDB() {

    // Erase DB file
    File dbFile = new File(DB_PATH);
    if (dbFile.exists()) {
        assertTrue(dbFile.delete());
    }

    // Erase journal file
    dbFile = new File(DB_PATH + "-journal");
    if (dbFile.exists()) {
        assertTrue(dbFile.delete());
    }
}

new File(DB_PATH) obviously fails when DB_PATH is null

I don't modify this constant anywhere (anyway I can't modify it because it's final), so I don't understand this behavior.

If I move this constant to another class/interface, it works fine.

Can anyone please explain this behavior? Thanks in advance!

like image 965
m0skit0 Avatar asked Nov 04 '22 10:11

m0skit0


1 Answers

The Exception you get means, that DB_PATH couldn't be initialized - thus it is null later on. I guess one of the parts you use to init DB_PATH is null, probably somewhere in this one: CoreService.class.getPackage().getName().

Also see: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ExceptionInInitializerError.html

like image 105
Ridcully Avatar answered Nov 09 '22 16:11

Ridcully