I\'m getting error when runtime my project.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.olympic/com.prima.olympic.ProductD
checkOrigin is null
String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin!=null && checkOrigin.equals("shoppinglist")){
btnAddtoShoppingList.setVisibility(View.GONE);
btnDeleteShoppingList.setVisibility(View.VISIBLE);
}
Your String checkOrigin
is null
thus giving you a NullPointerException
. This is what is causing it:
String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin.equals("shoppinglist")){
btnAddtoShoppingList.setVisibility(View.GONE);
btnDeleteShoppingList.setVisibility(View.VISIBLE);
}
The String checkOrigin
is null
because you are not receiving any value from your Intent
. This might be because you forgot to pass a value from your previous Activity
.
However, you can check to see if your String is null
and if it is, then those actions won't be performed. You would do this by the following:
String checkOrigin = i.getStringExtra("from_activity");
if(checkOrigin != null && checkOrigin.equals("shoppinglist")){
btnAddtoShoppingList.setVisibility(View.GONE);
btnDeleteShoppingList.setVisibility(View.VISIBLE);
}
Therefore, if checkOrigin
is null, then those actions won't be performed.
But I would recommend checking the Activity
that you are receiving the Intent
from to make sure that you are sending and receiving the Intent
correctly.
If you want to read more about NullPointerException
s, have a look at this question and its answers for more detail.
Change <view> to <View>
, because view is not about empty view. It's for custom view defined through class attr, like below:
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.your.package.YourCustomView" />
And you got error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference