realm null object reference - android

when i start my app with out network and call my data in my database app crash and this is my code
public void getAllMovies() {
findData = realm.where(MovieDb.class).findAll();
movieArrayList = new ArrayList(findData);
movieMethod();
}
and i make my conflagration in here
public class myApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
RealmConfiguration configuration = new RealmConfiguration
.Builder(this).name("Movie_database.realm").build();
Realm.setDefaultConfiguration(configuration);}}
and moviedb is my realm object
my app crash in line
findData = realm.where(MovieDb.class).findAll();
and this is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.massive.movieapp, PID: 4222
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.massive.movieapp/com.massive.movieapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
at com.massive.movieapp.FragmentForActivity.getAllMovies(FragmentForActivity.java:143)
at com.massive.movieapp.FragmentForActivity.CallNetwork(FragmentForActivity.java:62)
at com.massive.movieapp.FragmentForActivity.onCreate(FragmentForActivity.java:55)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method

The exception happens because your realm variable is null when you call the getAllMovies method.
Remember to call realm = Realm.getDefaultInstance(); previously.

Related

Pushwoosh.getInstance() error

I am getting an error when I try to update my pushwoosh applications in the Pushwoosh library.I've updated it in Gradle.
07-06 18:21:59.637 19950-19950/com.ahmet.sen E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ahmet.sen, PID: 19950
java.lang.ExceptionInInitializerError
at com.ahmet.sen.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.pushwoosh.notification.j com.pushwoosh.c.d()' on a null object reference
at com.pushwoosh.Pushwoosh.<init>(Unknown Source)
at com.pushwoosh.Pushwoosh.<clinit>(Unknown Source)
at com.ahmet.sen.MainActivity.onCreate(MainActivity.java:50) 
at android.app.Activity.performCreate(Activity.java:6237) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Where i get the error
Pushwoosh.getInstance().registerForPushNotifications();
Duplicating response from Pushwoosh Github here:
This is not a bug of Pushwoosh SDK. This is a peculiarity of multi-processor applications in Android apps environment.
When you integrate analytics tools such as LeakCanary, AppMetrica, and others, these libraries start a new process for itself. A new instance of the app is created In this process. But you do not need to listen for pushes in this separate process, so by default you should not call registerForPushNotifications() inside Application.onCreate().
Therefore there are 2 possible solutions:
Not to call registerForPushNotifications inside Application.onCreate()
If you want to call registerForPushNotifications inside Application.onCreate(), you should perform a check in application's main process:
List < ActivityManager.RunningAppProcessInfo > runningAppProcesses = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
if (runningAppProcesses != null && runningAppProcesses.size() != 0) {
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo: runningAppProcesses) {
boolean isCurrentProcess = runningAppProcessInfo.pid == android.os.Process.myPid();
boolean isMainProcessName = getPackageName().equals(runningAppProcessInfo.processName);
if (isCurrentProcess && isMainProcessName) {
Pushwoosh.getInstance().registerForPushNotifications(...);
break;
}
}
}

Android: Using 2 ArrayAdapters Error

I now get a new error.
This is my code:
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int i, long id) {
Toast.makeText(getApplicationContext(), beaconAdap.getItem(i).toString() + " Added to history", Toast.LENGTH_SHORT).show();
historyBeacons.add(beaconAdap.getItem(i).toString());
Intent intent = new Intent(getBaseContext(), HistoryActivity.class);
intent.putStringArrayListExtra("beacons", historyBeacons);
startActivity(intent);
return true;
}
});
}
So i add the int i to an arraylist, i have tested that it goes in and it does. Then a start a new intent for the history activity and send the arraylist over.
This is the code in history:
ListView historyLv = (ListView) findViewById(R.id.historyView);
ArrayAdapter historyAdap;
ArrayList<String> historyBeacons = getIntent().getExtras().getStringArrayList("beacons");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
historyAdap = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, historyBeacons);
historyLv.setAdapter(historyAdap);
}
But i get this error:
03-18 17:44:41.238 18641-18641/nsa.com.museum E/AndroidRuntime: FATAL EXCEPTION: main
Process: nsa.com.museum, PID: 18641
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nsa.com.museum/nsa.com.museum.HistoryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at nsa.com.museum.HistoryActivity.<init>(HistoryActivity.java:12)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
03-18 17:44:43.362 18846-18846/nsa.com.museum W/System: ClassLoader referenced unknown path: /data/app/nsa.com.museum-2/lib/x86_64
can You show the erro please
and I guess you forget to add
ListAdapter.notifyDataSetChanged();
after you add value to lisy
.........................................................
NullPointerException
this erro shows in Android cause you may not Initialized the Adapter
make sure you Initialized the 2nd adapter

NPE error in Android Studio

I am getting the following error in Android Studio and I am unable to resolve it:
07-06 19:44:41.798 4491-4491/com.example.first_app D/AndroidRuntime: Shutting down VM
07-06 19:44:41.799 4491-4491/com.example.first_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.first_app, PID: 4491
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first_app/com.example.first_app.welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' on a null object reference
at com.example.first_app.welcome.onCreate(welcome.java:22)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-06 19:44:44.696 4491-4497/com.example.first_app W/art: Suspending all threads took: 46.612ms
07-06 19:44:46.517 4491-4491/com.example.first_app I/Process: Sending signal. PID: 4491 SIG: 9
THIS IS THE ERROR I M INCURRING
CODE FOR welcome.java
public class welcome extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
TextView my_msg;
String name;
my_msg= (TextView)findViewById(R.id.textView3);
name= "Welcome " +findViewById(R.id.textView2).toString();
my_msg.setText(name);
}
}
XML CODE FOR welcome.xml ...Please refer it..it only shows the message on screen "welcome Name"..the name is entered by user through activity_form.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView3"
android:layout_gravity="center"
android:layout_weight="1" />
</LinearLayout>
First the way to solve your exception to type cast the View and getText from that View
(TextView) findViewById(R.id.textView2).getText().toString();
So the final String will look like
name= "Welcome " + (TextView) findViewById(R.id.textView2).getText().toString();
SUGGESTION
Use proper naming conventions for views, Rather than naming view1 view2 etc

Limit error in Firebase chatting in Android?

Some time ther is not error its run without error but most of time got error in this code, I try many thing for this. I try call this code on onStart() and I try this on onActivityCreted() too. but i get same error. This is my coding for show previous chat. I got error in limit().
final ListView listView = getListView();
mChatListAdapter = new FirebaseListAdapter<>(mFirebaseRef.limit(50), Chat.class, R.layout.chat_item, R.layout.chat_item_other,
R.layout.third_layout_chat, R.layout.fourth_layout_chat, getActivity(), chat_id);
listView.setAdapter(mChatListAdapter);
mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
listView.setSelection(mChatListAdapter.getCount() - 1);
}
});
This is error.
FATAL EXCEPTION: main Process: com.socialapp.android, PID: 8112 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{com.socialapp.android/com.socialapp.chat.ChatViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Query com.firebase.client.Firebase.limit(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Query com.firebase.client.Firebase.limit(int)' on a null object reference
at com.socialapp.chat.ChatViewFragment.onStart(ChatViewFragment.java:609)
at android.app.Fragment.performStart(Fragment.java:2244)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1958)
at android.app.FragmentController.dispatchStart(FragmentController.java:163)
at android.app.Activity.performStart(Activity.java:6274)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5466) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

GetData put string in android EditText

I want to put a URL received from Application B in to my application, in a EditText.
But im receiving null
Here is my code where im trying to get the intent:
Intent intent = getIntent();
mStreamUrl = intent.getDataString();
FinalurlPrimit.setText(mStreamUrl);
layout:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_torrent_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_below="#+id/include"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp">
<EditText
android:id="#+id/bTorrentUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:hint="#string/Text_magnet_sau_torrent_url"
android:text="#+id/FinalurlPrimit"/>
</android.support.design.widget.TextInputLayout>
How can achieve this?
Thank you
edit
Log:
02-28 16:54:09.900 5698-5698/ro.vrt.videoplayerstreaming E/AndroidRuntime: FATAL EXCEPTION: main
Process: ro.vrt.videoplayerstreaming, PID: 5698
java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.vrt.videoplayerstreaming/ro.vrt.videoplayerstreaming.TorrentPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at ro.vrt.videoplayerstreaming.TorrentPlayer.onCreate(TorrentPlayer.java:48)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
02-28 16:54:11.988 5698-5698/ro.vrt.videoplayerstreaming I/Process: Sending signal. PID: 5698 SIG: 9
If you delete android:text="#+id/FinalurlPrimit". everything work fine.
But I want the URL received, to appear in EditText
I do not have access to application B, and information from application B receive correct. Just can not display in EditText
FirstActivity
String yourURL ="http://stackoverflow.com/";
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("URL",yourURL);
startActivity(i);
SecondActivity
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("URL");
FinalurlPrimit.setText(value);
}
Please remove android:text="#+id/FinalurlPrimit". I don't think you need it. Between, android: text used to display the text you want it to show on EditText when there are no input on it. No id is required.
In OnCreate method, make sure you initialize the EditText as below
FinalurlPrimit= (EditText)findViewById(R.id.bTorrentUrl);

Categories

Resources