I have an Android Runtime Fatal Exception: main and I don't know what I have to do to resolve this problem. I have the following code, which causes this Exception:
... @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Cursor c = (Cursor) mAdapter.getItem(position);
// startActivity(new Intent(Intent.ACTION_VIEW,
// Uri.parse(c.getString(c.getColumnIndex("link")))));
// Cursor c = (Cursor) mAdapter.getItem(position);
// String urlWebView =
// Uri.parse(c.getString(c.getColumnIndex("link"))).toString();
String feedDescription = Uri.parse(
c.getString(c.getColumnIndex("description"))).toString();
String feedTitle = Uri.parse(c.getString(c.getColumnIndex("title")))
.toString();
String feedLink = Uri.parse(c.getString(c.getColumnIndex("link")))
.toString();
String imageLink = Uri.parse(c.getString(c.getColumnIndex("img_link"))).toString();
Intent in = new Intent(MainActivity.this, ShowNewsSummary.class);
in.putExtra("FeedDescription", feedDescription);
in.putExtra("FeedTitle", feedTitle);
in.putExtra("FeedLink", feedLink);
in.putExtra("ImageLink", imageLink);
startActivity(in);
}
In this code I get an img_link, which can be null when no image link is found in the database. When I click on an item I start ShowNewsSummaryActivity
and it throws Fatal Main Exception if no img_link is found in the database, because AsyncTask can't download an image when no image address is found:
... @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_show_news_summary);
String descriptionFeed = getIntent().getStringExtra("FeedDescription");
String titleFeed = getIntent().getStringExtra("FeedTitle");
imageLinkFeed = getIntent().getStringExtra("ImageLink");
title = (TextView) findViewById(R.id.title_feed);
description = (TextView) findViewById(R.id.description_feed);
imageDescription = (ImageView) findViewById(R.id.descriptionImage);
if (imageDescription != null) {
imageDescription.setVisibility(View.VISIBLE);
new DownloadImageAsyncTask(imageDescription).execute(imageLinkFeed);
}
title.setText(titleFeed);
description.setText(descriptionFeed);
title.setOnClickListener(this);
}
This is the LogCat of Exception:
09-23 22:54:42.210: E/AndroidRuntime(11885): FATAL EXCEPTION: main
09-23 22:54:42.210: E/AndroidRuntime(11885): java.lang.NullPointerException: uriString
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.net.Uri$StringUri.<init>(Uri.java:464)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.net.Uri$StringUri.<init>(Uri.java:454)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.net.Uri.parse(Uri.java:426)
09-23 22:54:42.210: E/AndroidRuntime(11885): at com.fire147.samplesmartrss.MainActivity.onItemClick(MainActivity.java:176)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3083)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.widget.AbsListView$1.run(AbsListView.java:3983)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.os.Handler.handleCallback(Handler.java:615)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.os.Looper.loop(Looper.java:137)
09-23 22:54:42.210: E/AndroidRuntime(11885): at android.app.ActivityThread.main(ActivityThread.java:4949)
09-23 22:54:42.210: E/AndroidRuntime(11885): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 22:54:42.210: E/AndroidRuntime(11885): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 22:54:42.210: E/AndroidRuntime(11885): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
09-23 22:54:42.210: E/AndroidRuntime(11885): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
09-23 22:54:42.210: E/AndroidRuntime(11885): at dalvik.system.NativeStart.main(Native Method)
Where is my mistake?
In the Android Developers API Uri.Parse state the following:
http://developer.android.com/reference/android/net/Uri.html#parse(java.lang.String)
Throws NullPointerException if uriString is null
It goes of in your MainActivity - onItemClick method. So the problem here is that one of the following strings you have entered in the Uri.parse() is null.
Debug the following code and check which one is null.
String feedDescription = Uri.parse(
c.getString(c.getColumnIndex("description"))).toString();
String feedTitle = Uri.parse(c.getString(c.getColumnIndex("title")))
.toString();
String feedLink = Uri.parse(c.getString(c.getColumnIndex("link")))
.toString();
String imageLink = Uri.parse(c.getString(c.getColumnIndex("img_link"))).toString();
to clarify, one of the following code returns a null value
c.getString(c.getColumnIndex("description"))).toString();
c.getString(c.getColumnIndex("title"))).toString();
c.getString(c.getColumnIndex("link"))).toString();
c.getString(c.getColumnIndex("img_link"))).toString();
The stackstrace is saying line 176 but i cant see which line that is here, so you have to check that out.
EDIT
You said this one is the string that returns null.
c.getString(c.getColumnIndex("img_link"))).toString();
To fix this just use the following code:
String img_link = c.getString(c.getColumnIndex("img_link"));
String imageLink = (img_link == null) ? "NULL" : Uri.parse(img_link).toString();
it may be not the cleanest null-check but you can use the following site to find your best null-check there
http://howtodoinjava.com/2013/04/05/how-to-effectively-handle-nullpointerexception-in-java/