Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using getObject() method #25

Open
demo-Ashif opened this issue Jan 9, 2017 · 3 comments
Open

using getObject() method #25

demo-Ashif opened this issue Jan 9, 2017 · 3 comments

Comments

@demo-Ashif
Copy link

how to use ..

public Object getObject(String key, Class<?> classOfT){

    String json = getString(key);
    Object value = new Gson().fromJson(json, classOfT);
    if (value == null)
        throw new NullPointerException();
    return value;
}

this method.

@MatUFV
Copy link

MatUFV commented Jan 17, 2017

Hello, i had the same issue, after a couple of tries i found the solution.
For example i have a class called UserReponse, so i called this method like this

tinyDB.getObject("user",UserResponse.class);

As i could understand, this method will return the object and this parameter will cast the object it returns to the object type u pass to it.

I hope I have helped.

@NeoHimu
Copy link

NeoHimu commented Jul 27, 2018

It's not what you are saying. 2nd argument is nothing but default argument. Please pass an object of the same class with some default value. It'll work.
With this approach: tinyDB.getObject("user",UserResponse.class); it'll work only when you have set earlier some value for key "user" else it'll give NullPointerException.

@mudi256
Copy link

mudi256 commented Jul 13, 2020

Simply update the method from

`public T getObject(String key, Class classOfT){

    String json = getString(key);
    Object value = new Gson().fromJson(json, classOfT);
    if (value == null)
        throw new NullPointerException();
    return (T)value;
}`

to
`public T getObject(String key, Class classOfT){

    String json = getString(key);
    Object value = new Gson().fromJson(json, classOfT);
    if (value == null)
        return null;
    return (T)value;
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants