TinyDb class is here
im using it simply by default codes like : TinyDB tinyDB = new TinyDB(MyActivity.this); and tinyDB.putInt("hadi" , 10);
but im getting an error that i cant understand . it say tinyDB is null object reference . you can see the error below :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ahmadi.TinyDB.TinyDB.putInt(java.lang.String, int)' on a null object reference
You have to create Object of Your TinyDB class like :-
TinyDB tinyDB = new TinyDB();
You should properly initiate your TinyDB before using it!
For Example in Activity:
TinyDB tinyDB = new TinyDB(this);
tinyDB.putInt("hadi" , 10);
or in Fragment:
TinyDB tinyDB = new TinyDB(getContext());
tinyDB.putInt("hadi" , 10);
TinyDB is just a simple wrapper around SharedPreferences so it is important that you provide a valid Context to it to initiate SharedPreferences
You are getting error because you are passing context before Oncreate just pass the context after oncreate donot make it global variable.
Related
I made nonActivity class, and I use there other nonActivity Database class. When I call method from 1st class I have Error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
Here is a fragment of my code of both classes:
DataBase.java
public DataBase(Context context) {
super(context, DATABASE_NAME, null, TABLE_VERSION);
}
and ObliczPodatki.java
public class ObliczPodatki extends Application
.
.
.
myDb = new DataBase(this.getApplicationContext());
and the full error message:
Process: com.example.nadgraniav01, PID: 11274
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:116)
at com.example.nadgraniav01.ObliczPodatki.obliczPodatki(ObliczPodatki.java:43)
at com.example.nadgraniav01.DodajNadgranieActivity$2.onClick(DodajNadgranieActivity.java:99)
at android.view.View.performClick(View.java:6669)
at android.view.View.performClickInternal(View.java:6638)
at android.view.View.access$3100(View.java:789)
at android.view.View$PerformClick.run(View.java:26145)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
if I use this instead of this.getApplicationContext() I am getting following erroe
Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
Try this then:
#Override
public void onCreate() {
super.onCreate()
SQLiteHandler db = new SQLiteHandler(getApplicationContext());
....rest of db calls..
}
Use:
myDb = new DataBase(getApplicationContext());
Or,
myDb = new DataBase(Activityname.this);
getApplicationContext is not the context that you should use,
Use this instead
myDb = new DataBase(this);
You have to initialise myDb variable in onCreate() method of ObliczPodatki class.
myDb = new DataBase(getApplicationContext());
I have two instances of shared preferences in my activity class. One instance I'm using to retrieve data that is already stored in shared preferences. The other instances I'm trying to store new sets of data, into a different file, that I'm retrieving from api. Can we do that because null values are being stored in the shared preferences.
Here is a part of my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);){
..........
SharedPreferences sharedPreferences = getSharedPreferences("User",Context.MODE_PRIVATE);
email = sharedPreferences.getString("email",DEFAULT);
//some piece of code........
setdata();//in this method I'm initializing the variables
SharedPreferences sharedPreferences2 = getSharedPreferences("File",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences2.edit();
editor.putString("a",a);
editor.putString("b",b);
editor.putString("c",c);
editor.putString("d",d);
editor.putString("e",e);
editor.commit();
}
public void setData(){
a="hello";
b="world";
c="hello";
d="world";
e="hello";
}
All these are in my onCreate method of my activity class.
a,b,c,d,e are global variable which I'm initializing in some other method of the class. I'm making the method call in onCreate method before implementing the shared preferences. But null values are being stored.
You have answered you question... The code you have posted is in onCreate method of your activity and a,b,c,d,e are global variables.
So they have null values when the activity starts.
You need to execute the code to save the values after you have initialized the value of a,b,c,d,e which is after receiving data from the API.
I have an app that I control the Sphero with via the Sphero Android SDK and in my MainActivity, I have the user connect the Sphero and that Sphero is hooked to the variable mRobot. I want to create an Intent which I already have in my MainActivity:
Intent calibrationIntent = new Intent(MainActivity.this, CalibrationActivity.class);
calibrationIntent.putExtra("Robot", mRobot);
startActivity(calibrationIntent);
Here, I put the mRobot variable in the putExtra() in the CalibrationActivity.class, I tried to get the extra variables, but I couldn't:
Intent calibrationIntent = getIntent();
Bundle bundle = getIntent().getExtras();
Sphero mRobot = bundle.getString("Robot");
I get an error in the lineSphero mRobot = bundle.getString("Robot"); because I am trying to convert a Robot data type into a string.
How would I go about passing the mRobot variable through intents so I could modify it in the other activity? I tried to convert to a string in my MainActivity using mRobot.toString();, but I don't know how to convert it back into a robot in the calibrationActivity.
EDIT:
I tried using getParcelable() and getSerializable() in my MainActivity and in my CalibrationActivity, I cast the string into a Sphero via
Sphero mRobot = (Sphero) bundle.getParcelable("Robot");
or
Sphero mRobot = (Sphero) bundle.getSerializable("Robot");
But as soon as I press the button after the Sphero is connected, the app crashes, and the log is Caused by: java.lang.ClassCastException: orbotix.robot.base.Robot cannot be cast to orbotix.sphero.Sphero
Okay, I guess I didn't read your problem carefully enough.
I get an error in the line Sphero mRobot = bundle.getString("Robot"); because I am trying to convert a Robot data type into a string.
I didn't catch that you said it was a Robot data type, I just looked at your declaration of mRobot as a Sphero. Now look at your error message:
java.lang.ClassCastException: orbotix.robot.base.Robot cannot be cast to orbotix.sphero.Sphero
The problem is that mRobot is not a Sphero. It is a Robot. Specifically, it is a orbotix.robot.base.Robot.
Change to this:
Robot mRobot = (Robot) bundle.getParcelable("Robot");
assuming you have
import orbotix.robot.base.Robot
I'm not familiar with the type system of the Sphero SDK, so I don't know how Sphero and Robot are related in the type hierarchy, but the error message is telling you: You serialized a Robot and are trying to deserialize it into a Sphero.
Whether Sphero is serializable or not, you can always have a shared singleton to pass the data between activities. This won't be limited to any specific type of data. A simple implementation will be like this:
public class DataHolder {
private Sphero mRobot;
public Sphero getData() {return mRobot;}
public void setData(String mRobot) {this.mRobot = mRobot;}
private static final DataHolder holder = new DataHolder();
public static DataHolder getInstance() {return holder;}
}
Then in your caller method, do this:
Intent calibrationIntent = new Intent(MainActivity.this, CalibrationActivity.class);
DataHolder.getInstance().setData(mRobot);
startActivity(calibrationIntent);
Then in your CalibrationActivity you can always get the mRobot object by:
Sphero myRobot = DataHolder.getInstance().getData();
You are trying to cast an object to a String. Sphero is an Object. You have to pass it as a parcelable extra.
Sphero mRobot = (Sphero) bundle.getExtra("Robot");
So I have been going through the Android Developer training on the official site and there is a point where they want us to finally instantiate our database.
So they tell us to use this snippet of code:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
However, I'm getting an error for the getContext() method. It states that it cannot find a symbol for that method.
So I searched the source and that method in the View class just cannot be found. Is this a deprecated method? And if this isn't an option, is there any other way we can grab the context of a view?
Thank you!
The line of code you pass is:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(geContext());
It should work if you substitute for any of these code lines :
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
Or
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getApplicationContext());
Or
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this);
The android developer documentation of the Context:
https://developer.android.com/reference/android/content/Context.html
You might found helpful too look in this question, that explains what is Context for:
What is 'Context' on Android?
In your code you have used geContext() change it to getContext() or getApplicationContext() or if calling the object from inside an activity simply pass this
The View class does have a getContext method.
You either have a typo, or your code is not located in a non-static method of a sub-class of View.
Thats how I made it
MainActivity
FeedReaderContract contract = new FeedReaderContract(this);
I edited the constructor of the class FeedReaderContract
mDbHelper = new FeedReaderDbHelper(getContext());
The method getContext()
public Context getContext() {
return context;
}
I've got two activities, one of them is called MyActivity. I want both of them to be able to use a function located in a class othat we may call MyClass. In MyClass, I try to use an intent to launch the activity AnotherActivity. Since the constructor takes a context as parameter, I simply tried to store a context from the activity in the constructor, and then use it when I try to create my intent.
class MyClass {
private Context cxt;
MyClass(Context cxt) {
this.cxt = cxt;
}
startIntent() {
Intent intent = new Intent(cxt, AnotherActivity.class);
startActivity(intent); // this line throws a NullPointerException
}
}
The code in MyActivity to use the class is shown below:
myClassObject = new MyClass(MyActivity.this);
myClassObject.startIntent();
However, even thought none of the arguments are null (checked that with a simple if-statement), intent seems to be null and a NullPointerException is thrown. Why does it not work, and what can I do to solve the problem?
I'm quite new to Android and Java development, so please explain it as basic as you can.
cxt.startActivity(new Intent(cxt, AnotherActivity.class));
and to be sure that it's intent is NULL, and not something internal in startActivity method, you can add some checks, i.e.
Intent intent = new Intent(cxt, AnotherActivity.class);
Log.d(toString(), "intent = " + intent.toString());
cxt.startActivity(intent);
I've used almost identical code in my applications and it's worked fine.
I suspect there's something else going on that's in code you haven't shown us; I suspect there's some cut-and-paste issues --- e.g. what are you calling startActivity() on in MyClass?