java - Could not initialize class com.example.testapp.SomeHelper -


public class somehelper {     ...     private int static x;     static {         map<string, string> amap = new hashmap<>();         //populate map here     }     public static void setx(int value){        x = value;     } } 

when somehelper.setx called, "could not initialize class somehelper" exception. i'm not sure how fix this. faced issue? tried catching exception , re throwing runtime exception static block, doesn't help.

i think mean noclassdeffounderror this:

noclassdeffounderror: not initialize class somehelper 

as javase-7 states:

noclassdeffounderror thrown if java virtual machine or classloader instance tries load in definition of class (as part of normal method call or part of creating new instance using new expression) , no definition of class found.

the searched-for class definition existed when executing class compiled, definition can no longer found.

sometimes noclassdeffounderror occurs if static bits of class i.e. initialization takes place during defining of class, fails.

so first change

private int static x;  

to,

private static int x; 

declare setx() static, or create instance of somehelper invoke setx().

to invoke method class name, method should static.

try this:

public static void setx(int value){    x = value; }  somehelper.setx(someinteger); 

or this:

somehelper somehelper = new somehelper (); // default constructor  somehelper .setx(someinteger); 

note that, don't have provide constructors class, must careful when doing this. compiler automatically provides no-argument, default constructor class without constructors.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -