I keep getting a NullPointerException on Android studio - Android Q&A, Help & Troubleshooting

Here is the main error from my logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.milespc.funfacts.ActivityFunFacts.onCreate(ActivityFunFacts.java:50)
and here is the line where it says it is getting the null:
showFactButton.setOnClickListener(listener);
Can someone please help me figure this out?

Related

[Q] My code throws exceptions

Hello, i am new to development for android and i am trying to make a simple app for me to learn how to code in java.
The idea was to make an app that you can use to send a text (yes, i could just use the messaging app, but that's no fun) and after fiddling with my code quite a bit i made it run on the emulator.
everything works other than when i send the message; an error is thrown (sending a message using the messaging app works though, and the message appears on the other emulator)
The emulators are both running 4.0.3, only one has the app installed
here is the error:
Thread [<1> main] (Suspended (exception IllegalArgumentException))
and for the full stack you can visit this link: http://pastebin.com/Sdc4RshX
and for the code please go here: http://pastebin.com/4xXDTbD1
what would have caused the issue, and what should i do to fix it?
Thanks
Have you added the user permission in the android manifest for sending SMS?
Code:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
learning Java under Android is a really bad idea, just learn how to use Java with the JDK under a desktop OS.
dillonr, check this:
cheznutts said:
Have you added the user permission in the android manifest for sending SMS?
Code:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
Click to expand...
Click to collapse
Also, you need to provide a PendingIntent as fourth argument to sendTextMessage method (as described here).
i did/do have the permission in the android manifest
should edit the first post, i am trying to make a simple app to learn how to code in java for android...i have a few ideas for better apps, gotta learn the basics first.
i understand how the views work, and how the files are placed...so i'm learning some things, and it is similar to when i used to code a few years ago in flash (like classes, variables, if...etc.)
EDIT: i un-commented the parts i commented out previously (including the PendingIntent)
http://pastebin.com/cH4NMTwM
still throwing the same error
Thread [<1> main] (Suspended (exception IllegalArgumentException))
I also noticed this
SmsManager.sendTextMessage(String, String, String, PendingIntent, PendingIntent) line: 77
but i do not have a line 77, and nothing seems to be wrong with my code (that i can see, anyway)
Could you post your AndroidManifest.xml and layout XML would like to see the declarations.
manifest
http://pastebin.com/L6VU7bJV
layout
http://pastebin.com/yAB6ia4Q
ho[e this helps you help me
I went ahead and made a project using your code. I commented out the pending intent, I opened up two emulators. One with port 5554 and another with 5556. Now I compiled your code on 5556 and then text to 5554 and it went through fine, no exceptions. Here is what Im looking at in the Activity:
Code:
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.telephony.SmsManager;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void csend(View view) {
String _number = this.getString(R.string.number);
String _message = this.getString(R.string.message);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(_number, null, _message, null, null);
}
}
Here is my Android Manifest:
Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I left the layout alone. I say try it again. Check SDK is up to date and try creating new emulator using a different API level.
i updated and made new emulators (google api level 16, and google api level 15)
then i made a new "android application project" with the name 'multisender'
i copied the code from the previous application and pasted it into the correct files in the new one
what eclipse should i be running for android development? (on a mac)
running the application causes errors on sending the text (to phone number 15555215556 or 5556; the number shown when a text is recieved, and the port number)
it says "unfortunately, multisender has stopped." on the screen and i get errors in the LogCat
here is a copy of the LogCat errors
Code:
07-11 12:23:57.177: E/Trace(919): error opening trace file: No such file or directory (2)
07-11 12:29:33.907: E/Trace(954): error opening trace file: No such file or directory (2)
07-11 12:32:11.908: E/AndroidRuntime(954): FATAL EXCEPTION: main
07-11 12:32:11.908: E/AndroidRuntime(954): java.lang.IllegalStateException: Could not execute method of the activity
07-11 12:32:11.908: E/AndroidRuntime(954): at android.view.View$1.onClick(View.java:3591)
07-11 12:32:11.908: E/AndroidRuntime(954): at android.view.View.performClick(View.java:4084)
07-11 12:32:11.908: E/AndroidRuntime(954): at android.view.View$PerformClick.run(View.java:16966)
07-11 12:32:11.908: E/AndroidRuntime(954): at android.os.Handler.handleCallback(Handler.java:615)
07-11 12:32:11.908: E/AndroidRuntime(954): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 12:32:11.908: E/AndroidRuntime(954): at android.os.Looper.loop(Looper.java:137)
07-11 12:32:11.908: E/AndroidRuntime(954): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-11 12:32:11.908: E/AndroidRuntime(954): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:32:11.908: E/AndroidRuntime(954): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 12:32:11.908: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-11 12:32:11.908: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-11 12:32:11.908: E/AndroidRuntime(954): at dalvik.system.NativeStart.main(Native Method)
07-11 12:32:11.908: E/AndroidRuntime(954): Caused by: java.lang.reflect.InvocationTargetException
07-11 12:32:11.908: E/AndroidRuntime(954): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 12:32:11.908: E/AndroidRuntime(954): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 12:32:11.908: E/AndroidRuntime(954): at android.view.View$1.onClick(View.java:3586)
07-11 12:32:11.908: E/AndroidRuntime(954): ... 11 more
07-11 12:32:11.908: E/AndroidRuntime(954): Caused by: java.lang.IllegalArgumentException: Invalid destinationAddress
07-11 12:32:11.908: E/AndroidRuntime(954): at android.telephony.SmsManager.sendTextMessage(SmsManager.java:77)
07-11 12:32:11.908: E/AndroidRuntime(954): at com.dillonregi.multisender.multisender.csend(multisender.java:71)
07-11 12:32:11.908: E/AndroidRuntime(954): ... 14 more
it seems like i have an "invalid destination address" but sending a text to those numbers works in the default sms app
and what would be the "method of the activity"?
sorry for all these questions, but maybe one day someone else will also have this problem
EDIT: so i found the problem...kind of...when i replaced the line
sms.sendTextMessage(_number, null, _message, sentPI, null);
with
sms.sendTextMessage("5556", null, "test", null, null);
and ran it on the emulator (on port 5554) it sent "test" to the one on port 5556 with no errors!
Still wondering why i was getting errors with the variables there though
second edit: if either of the _number or _message variables are on that line it breaks...sentPI does not break it
okay, so i found the problem with the _number and _message
they were not defined in my strings.xml
the only problem is i cannot change who i send it to, or what the message is, even when i change what the text boxes say in the application...so my new question is how can i change a variable using an editable text box? (or how can i check what is in the text box before sending the text?)
EDIT: okay, i think i fixed it....
"
EditText number = (EditText)findViewById(R.id.pnumber);
EditText message = (EditText)findViewById(R.id.pmessage);
String _number = number.getText().toString();
String _message = message.getText().toString();
"
then
" sms.sendTextMessage(_number, null, _message, sentPI, null);
"
hope this helps other people
thanks everyone

Force Close onCreate()

Please pardon me if this is the wrong section and feel free to report this thread with a request to move to the proper section.
I've been working on an app for XDA Developer TV. The source is located here: https://code.google.com/p/xda-adk/s...cts/xdaadk/app/src/com/google/android/DemoKit
I've been developing on a Nexus 7. I decided to switch to my Galaxy Nexus and the application Force Closes on This Line. Apparently my Bundle savedInstanceState == null.
This is the error:
Code:
09-09 21:59:58.959: D/AndroidRuntime(18735): Shutting down VM
09-09 21:59:58.959: W/dalvikvm(18735): threadid=1: thread exiting with uncaught exception (group=0x40a73300)
09-09 21:59:58.959: E/AndroidRuntime(18735): FATAL EXCEPTION: main
09-09 21:59:58.959: E/AndroidRuntime(18735): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.DemoKit/com.google.android.DemoKit.DemoKitPhone}: java.lang.NullPointerException
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.os.Looper.loop(Looper.java:137)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 21:59:58.959: E/AndroidRuntime(18735): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 21:59:58.959: E/AndroidRuntime(18735): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 21:59:58.959: E/AndroidRuntime(18735): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 21:59:58.959: E/AndroidRuntime(18735): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 21:59:58.959: E/AndroidRuntime(18735): at dalvik.system.NativeStart.main(Native Method)
09-09 21:59:58.959: E/AndroidRuntime(18735): Caused by: java.lang.NullPointerException
09-09 21:59:58.959: E/AndroidRuntime(18735): at com.google.android.DemoKit.DemoKitPhone.showTabContents(DemoKitPhone.java:47)
09-09 21:59:58.959: E/AndroidRuntime(18735): at com.google.android.DemoKit.DemoKitPhone.showControls(DemoKitPhone.java:42)
09-09 21:59:58.959: E/AndroidRuntime(18735): at com.google.android.DemoKit.BaseActivity.onCreate(BaseActivity.java:26)
09-09 21:59:58.959: E/AndroidRuntime(18735): at com.google.android.DemoKit.DemoKitPhone.onCreate(DemoKitPhone.java:32)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.Activity.performCreate(Activity.java:5008)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-09 21:59:58.959: E/AndroidRuntime(18735): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-09 21:59:58.959: E/AndroidRuntime(18735): ... 11 more
I don't really know how to troubleshoot it because it's a pretty low level problem. How can I fix this?
Disregard Everything i said before just had a look at the code. and reread your log, it is null pointing at like 47 of the DemoKitPhone
Code:
45 - void showTabContents(Boolean showInput) {
46 - if (showInput) {
47 - mInputContainer.setVisibility(View.VISIBLE);
48 - mOutputContainer.setVisibility(View.GONE);
49 - mOutputLabel.setBackgroundDrawable(mNormalTabImage);
i recon the mInputContainer, mOutputContainer are null.
Let me know how ya go.
Pvy
AdamOutler said:
Please pardon me if this is the wrong section and feel free to report this thread with a request to move to the proper section.
I've been working on an app for XDA Developer TV. The source is located here: https://code.google.com/p/xda-adk/s...cts/xdaadk/app/src/com/google/android/DemoKit
Click to expand...
Click to collapse
may be a little crazy but i remember reading that the
Code:
super.onCreate(savedInstanceState);
Must be first in your overridden onCreate()
i'll DL the code tonight and take another look later if no-one else comes back before hand
Pvy.
@pvyParts, It already has super.Oncreate and the log warns about a null de-reference on another line, i don't think that's the problem.
This might be helpful.
Super class method does not seem to initialize mInputContainer and it's done by view finder in their demokit2 package. Since your code does not explicitly set it to an object, it causes a null dereference when you call a method from a null pointing class. You can confirm this by trying:
Code:
 void showTabContents(Boolean showInput) {
  if (showInput) {
   mOutputContainer.setVisibility(View.GONE);
   mInputContainer.setVisibility(View.VISIBLE);
   mOutputLabel.setBackgroundDrawable(mNormalTabImage);
  } else {
   mInputContainer.setVisibility(View.GONE);
   mInputLabel.setBackgroundDrawable(mNormalTabImage);
   mOutputContainer.setVisibility(View.VISIBLE);
   mOutputLabel.setBackgroundDrawable(mFocusedTabImage);
  }
 }
If this causes null reference exception at mOutputContainer.setVisibility then you need to set mOutputContainer and mInputContainer to a valid LinearLayout object/resource before calling their class methods.
Rick_1995 said:
@pvyParts, It already has super.Oncreate and the log warns about a null de-reference on another line, i don't think that's the problem.
[/CODE]
If this causes null reference exception at mOutputContainer.setVisibility then you need to set mOutputContainer and mInputContainer to a valid LinearLayout object/resource before calling their class methods.
Click to expand...
Click to collapse
Yeah once i re-read the code at home i saw what was wrong made me feel rather noobish for getting it so dam wrong
Pvy

need some help for smali editing

Process: com.android.systemui
Flags: 0x40d83e0d
Package: com.android.systemui v21 (5.0.2-G850FXXU2COK1)
Build: samsung/sltexx/slte:5.0.2/LRX22G/G850FXXU2COK1:user/release-keys
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.android.systemui.qs.QSEditPanel.findViewById(int)' on a null object reference
at com.android.systemui.statusbar.phone.PhoneStatusBar$3.onOpenThemeChanged(PhoneStatusBar.java:663)
at com.android.keyguard.KeyguardUpdateMonitor.handleOpenThemeChangeUpdate(KeyguardUpdateMonitor.java:1521)
at com.android.keyguard.KeyguardUpdateMonitor.access$1600(KeyguardUpdateMonitor.java:102)
at com.android.keyguard.KeyguardUpdateMonitor$1.handleMessage(KeyguardUpdateMonitor.java:306)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
i edit qseditpanel, phonestatusbar etc. but same error.

NullPointerException

How do I make it not null? here is the error on my logcat:
02-05 13:59:14.764 26351-26351/com.example.milespc.funfacts2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.milespc.funfacts2, PID: 26351
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.milespc.funfacts2.MainActivity$1.onClick(MainActivity.java:34)
here is the string of code:
factLabel.setText(fact);

PdfRenderer Sample Crashes

Hey there,
I tried the PdfRenderer Sample Project on serveral AVDs (API lvl 23 and 24) and on my Galaxy A5 2015 with CM13 (API 23). the app starts and shows an empty activity - after about 10 seconds it crashes.
The Error:
Code:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.pdfrendererbasic, PID: 27604
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pdfrendererbasic/com.example.android.pdfrendererbasic.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
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:5461)
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 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference
at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.showPage(PdfRendererBasicFragment.java:163)
at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onViewCreated(PdfRendererBasicFragment.java:101)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
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:6267)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)*
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:5461)*
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)*
Application terminated.
What did I do wrong?
LG Christopher

Categories

Resources