I'm a new Android programmer and recently, a lot of my projects have been getting this error:
07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}: java.lang.NullPointerException
Can anyone help me solve this problem?
Here's the entire logcat stack trace:
07-31 23:45:18.512: INFO/ActivityManager(63): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.amrit.musifind/.Main } 07-31 23:45:18.682: INFO/ActivityManager(63): Start proc com.amrit.musifind for activity com.amrit.musifind/.Main: pid=716 uid=10025 gids={3003} 07-31 23:45:18.962: INFO/jdwp(716): received file descriptor 20 from ADB 07-31 23:45:19.102: DEBUG/ddm-heap(716): Got feature list request 07-31 23:45:19.572: DEBUG/AndroidRuntime(716): Shutting down VM 07-31 23:45:19.572: WARN/dalvikvm(716): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) 07-31 23:45:19.572: ERROR/AndroidRuntime(716): Uncaught handler: thread main exiting due to uncaught exception 07-31 23:45:19.592: ERROR/AndroidRuntime(716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amrit.musifind/com.amrit.musifind.Main}: java.lang.NullPointerException 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.app.ActivityThread.access$2100(ActivityThread.java:116) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.os.Handler.dispatchMessage(Handler.java:99) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.os.Looper.loop(Looper.java:123) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.app.ActivityThread.main(ActivityThread.java:4203) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at java.lang.reflect.Method.invoke(Method.java:521) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at dalvik.system.NativeStart.main(Native Method) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): Caused by: java.lang.NullPointerException 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at com.amrit.musifind.Main.onCreate(Main.java:44) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 07-31 23:45:19.592: ERROR/AndroidRuntime(716): ... 11 more 07-31 23:45:19.612: INFO/Process(63): Sending signal. PID: 716 SIG: 3 07-31 23:45:19.612: INFO/dalvikvm(716): threadid=7: reacting to signal 3 07-31 23:45:19.642: INFO/dalvikvm(716): Wrote stack trace to '/data/anr/traces.txt' 07-31 23:45:21.422: INFO/Process(716): Sending signal. PID: 716 SIG: 9 07-31 23:45:21.442: INFO/ActivityManager(63): Process com.amrit.musifind (pid 716) has died. 07-31 23:45:21.583: WARN/UsageStats(63): Unexpected resume of com.android.launcher while already resumed in com.amrit.musifind 07-31 23:45:21.712: WARN/InputManagerService(63): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@439559d0 07-31 23:45:23.482: DEBUG/AndroidRuntime(725): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 07-31 23:45:23.482: DEBUG/AndroidRuntime(725): CheckJNI is ON 07-31 23:45:23.812: DEBUG/AndroidRuntime(725): --- registering native functions --- 07-31 23:45:23.832: INFO/jdwp(725): received file descriptor 20 from ADB 07-31 23:45:24.193: DEBUG/ddm-heap(725): Got feature list request 07-31 23:45:25.052: DEBUG/AndroidRuntime(725): Shutting down VM 07-31 23:45:25.052: DEBUG/dalvikvm(725): DestroyJavaVM waiting for non-daemon threads to exit 07-31 23:45:25.062: DEBUG/dalvikvm(725): DestroyJavaVM shutting VM down 07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread shutting down 07-31 23:45:25.062: DEBUG/dalvikvm(725): HeapWorker thread has shut down 07-31 23:45:25.073: DEBUG/jdwp(725): JDWP shutting down net... 07-31 23:45:25.073: DEBUG/jdwp(725): +++ peer disconnected 07-31 23:45:25.073: INFO/dalvikvm(725): Debugger has detached; object registry had 1 entries 07-31 23:45:25.093: DEBUG/dalvikvm(725): VM cleaning up 07-31 23:45:25.113: DEBUG/dalvikvm(725): LinearAlloc 0x0 used 665652 of 4194304 (15%) 07-31 23:47:22.712: DEBUG/dalvikvm(104): GC freed 9173 objects / 524184 bytes in 149ms
Heres the 1st activity: start.java
package com.amrit.musifind; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import com.amrit.musifind.R; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public abstract class start extends Activity implements OnClickListener { Button buttonGo, buttonReset; EditText EditTextinput; String input = ""; String ur = "http://www.tastekid.com/ask/ws?q="; String l = "&f=musifin2125&k=mjjlnzkyzwuz&format=JSON"; String url = "" ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonGo = (Button) findViewById(R.id.buttonGo); buttonReset = (Button) findViewById(R.id.buttonReset); EditTextinput = (EditText) findViewById(R.id.EditTextinput); //Button listener buttonReset.setOnClickListener(this); buttonGo.setOnClickListener(this); } public void onClick (View src){ switch(src.getId()){ case R.id.buttonGo: input = EditTextinput.getText().toString(); url = ur + input + l ; Intent Main = new Intent(this, ToClass.class); Main.putExtra("userurl", url); startActivity(Main); break; case R.id.buttonReset: EditTextinput.setText(""); break; } } }
The second activity: Main.java
package com.amrit.musifind; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.amrit.musifind.JSONfunctions; import com.amrit.musifind.R; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Main extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listplaceholder); Intent intent = getIntent(); String url = intent.getExtras().getString("userurl"); ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>> (); JSONObject json = JSONfunctions.getJSONfromURL(url); try{ JSONObject earthquakes = json.getJSONObject("Similar"); JSONArray info = earthquakes.getJSONArray("Results"); for (int i = 0; i < info.length(); i++) { HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = info.getJSONObject(i); map.put("id", String.valueOf(i)); map.put("name", "Name:" + e.getString("Name")); map.put("type", "Type: " + e.getString("Type")); mylist.add(map); } JSONArray results = json.getJSONArray("Results"); for(int i=0;i<results.length();i++){ HashMap<String, String> map = new HashMap<String, String>(); JSONObject e = results.getJSONObject(i); map.put("id", String.valueOf(i)); map.put("name", "Name:" + e.getString("name")); map.put("type", "Type: " + e.getString("type")); mylist.add(map); } } catch(JSONException e) { Log.e("log_tag", "Error parsing data "+e.toString()); } ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "name", "type" }, new int[] { R.id.item_title, R.id.item_subtitle }); setListAdapter(adapter); final ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { @SuppressWarnings("unchecked") HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); } }); } }
Lastly, my manifest file: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amrit.musifind" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".start" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.start" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name= ".Main"> <intent-filter> <action android:name="android.intent.action.Main" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
Any help is much appreciated, without solving this problem I can't test my app out!
Your null pointer exception seems to be on this line:
String url = intent.getExtras().getString("userurl");
because intent.getExtras()
returns null when the intent doesn't have any extras.
You have to realize that this piece of code:
Intent Main = new Intent(this, ToClass.class); Main.putExtra("userurl", url); startActivity(Main);
doesn't start the activity you wrote in Main.java, it will attempt to start an activity called ToClass
and if that doesn't exist, your app crashes.
Also, there is no such thing as "android.intent.action.start"
so the manifest should look more like:
<activity android:name=".start" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name= ".Main"> </activity>
I hope this fixes some of the issues you are encountering but I strongly suggest you check out some "getting started" tutorials for android development and build up from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With