This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
My app crashes right away after signin and it gives a null object on
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class, R.layout.menu_item, MenuViewHolder.class, category)
Can someone help or have any better idea?
private void LoadMenu(){
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class, R.layout.menu_item, MenuViewHolder.class, category) {
#Override
protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position) {
viewHolder.txtMenuName.setText(model.getName());
Picasso.get().load(model.getImage()).into(viewHolder.imageView);
final Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View v, int position, boolean isLongClick) {
//Get CategoryId and send to new Activity
Intent foodList = new Intent(Home.this, FoodList.class);
//Because Category is key, so we just get the key of this item
foodList.putExtra("CategoryId", adapter.getRef(position).getKey());
startActivity(foodList);
}
});
}
};
recycler_menu.setAdapter(adapter);
}
this is the full stacktrace error that crashes my app, I dont know why, Iv'e initiated the FireDatabase and set the database reference to "Category" which is in my firedatabase
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fahad.foodie, PID: 4287
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.fahad.foodie.Home$2.populateViewHolder(Home.java:88)
at com.fahad.foodie.Home$2.populateViewHolder(Home.java:85)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:184)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:407)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:693)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Your textview is not properly defined.
viewHolder.txtMenuName is NULL as per the logcat messages.
Please define it properly at your MenuViewHolder like below,
public MenuViewHolder(View itemView) {
super(itemView);
txtMenuName = itemView.findViewById(R.id.menu_name);
imageMenu = itemView.findViewById(R.id.menu_image);
itemView.setOnClickListener(this);
}
You have to ensure the txtMenuName is properly mapped to the textview id(for example android:id="#+id/menu_name") in the menu_item.xml.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
When the register button is clicked the app should load another activity however it just crashes. Tried looking here and here for help but I still couldn't fix it. I also tried removing 'implements View.OnClickListner' and using it how its shown in the examples and it didn't work. Also tried changing the xml file to a LinearLayout and that didn't work either. Thanks in advance!
public class LogInActivity extends AppCompatActivity implements View.OnClickListener {
//user input variables
EditText emailAddressInput;
EditText passwordInput;
Button logInButton;
Button registerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the layout to activity_login.xml
setContentView(R.layout.activity_login);
//get user inputs and set to the variables
emailAddressInput = (EditText)findViewById(R.id.emailAddressInput);
passwordInput = (EditText)findViewById(R.id.passwordInput);
logInButton = (Button)findViewById(R.id.logInButton);
registerButton = (Button)findViewById(R.id.registerButton);
logInButton.setOnClickListener(this);
registerButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent registerIntent = new Intent (LogInActivity.this,RegisterActivity.class);
switch (view.getId())
{
case R.id.logInButton:
break;
case R.id.registerButton:
startActivity(registerIntent);
break;
}
}
}
EDIT:
the logcat:
2020-04-04 16:02:10.759 12718-12718/com.example.blooddonorsystem E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.blooddonorsystem, PID: 12718
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blooddonorsystem/com.example.blooddonorsystem.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at com.example.blooddonorsystem.RegisterActivity.onCreate(RegisterActivity.java:56)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
In your RegisterActivity, seems like the Spinner you are setting the adapter to is null. Make sure it has correct value, usually its an ArrayAdapter of String.
The error is not in LoginActivity but in RegisterActivity.There is a null pointer exception in RegisterActivity when you are setting adapter to a spinner.
This question already has answers here:
Can not find a View with findViewById()
(4 answers)
Closed 6 years ago.
When I start my SettingsActivity, I get the following Error, but I don't know why, because I have done everything as always.
ERROR:
FATAL EXCEPTION: main Process: com.android.niklasvlach.vertretung, PID: 9175
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.niklasvlach.vertretung/com.android.niklasvlach.vertretung.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5294) 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:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference at com.android.niklasvlach.vertretung.SettingsActivity.onCreate(SettingsActivity.java:28) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5294) 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:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
The Code:
public class SettingsActivity extends AppCompatActivity {
Spinner select;
EditText username;
EditText password;
#Override
protected void onCreate(Bundle savedInstanceState) {
select = (Spinner) findViewById(R.id.selectclass);
username = (EditText) findViewById(R.id.username_view);
password = (EditText) findViewById(R.id.password_view);
select.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long text) {
Toast.makeText(SettingsActivity.this,"" + text,Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
}
If anything else is needed, I will post it!
You need to do your UI work after setting the layout of the Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
select = (Spinner) findViewById(R.id.selectclass);
username = (EditText) findViewById(R.id.username_view);
password = (EditText) findViewById(R.id.password_view);
select.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long text) {
Toast.makeText(SettingsActivity.this,"" + text,Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am a beginner, and I am trying to use ArrayList from an Interface. I got the structure from an article here. The problem is it's giving me null when I try to use it in a Fragment.
The class is:
public class NewsFRagment extends Fragment implements BackgroundTask.CallBack {
private ArrayList<NewsPOJO> arrayList1;
public NewsFRagment() {
// Required empty public constructor
}
#Override
public void onSuccess(ArrayList<NewsPOJO> arrayList) {
this.arrayList1 = arrayList;
Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.news_fragment, container, false);
BackgroundTask backgroundTask = new BackgroundTask(this.getActivity(), this);
backgroundTask.execute();
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));
return v;
}
#Override
public String toString() {
return "News Fragment";
}
}
I need to use arraylist from the onSuccess and pass it in my setAdapter method, Problem is its null when I pass it in setAdaper, but it's not null in the onSuccess, How can I solve it?
This is logcat:
06-03 04:52:50.367 21851-21851/hilz.reycfrag W/art: Failed to find OatDexFile for DexFile /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex ( canonical path /data/data/hilz.reycfrag/files/instant-run/dex/slice-slice_8-classes.dex) with checksum 0xd8bc1e5a in OatFile /data/data/hilz.reycfrag/cache/slice-slice_8-classes.dex
06-03 04:52:51.150 21851-21851/hilz.reycfrag D/AndroidRuntime: Shutting down VM
06-03 04:52:51.150 21851-21851/hilz.reycfrag E/AndroidRuntime: FATAL EXCEPTION: main
Process: hilz.reycfrag, PID: 21851
java.lang.RuntimeException: Unable to start activity ComponentInfo{hilz.reycfrag/hilz.reycfrag.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at hilz.reycfrag.NewsFRagment.onCreateView(NewsFRagment.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-03 04:52:51.178 21851-21897/hilz.reycfrag D/arraylist in onSuccess: 4
#Override
public void onSuccess(ArrayList<NewsPOJO> arrayList) {
this.arrayList1 = arrayList;
Log.d("arraylist in onSuccess", String.valueOf(arrayList.size()));
// Make sure your fragment is done view loading before on success.
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.rv_news);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
Log.d("Arraylist1 size", String.valueOf(+arrayList1.size()));
recyclerView.setAdapter(new MyRecyclerAdapter(this.getActivity(), arrayList1));
}
Call the below code on start, show a progress dialog, and cancel on success:
BackgroundTask backgroundTask = new BackgroundTask(this.getActivity(), this);
backgroundTask.execute();
Check in what order the callbacks happen...
Your arraylist may not be initialized, when the callback happens at setAdapter as you only declare it. You never initialize it in constructor. As opposed to in the OnSuccess call back, where you receive an arraylist reference as input and set your local instance variable to it.
The creation of the view would be the initial callback run, so if you need to pass the list you need to initialise it first, you could for instance add an initialization to the constructor:
public NewsFRagment() {
// Required empty public constructor
arrayList1 = new ArrayList<NewsPOJO>();
}
Or you could have a specific initialise method, that you call after doing needed stuff in the view creation event first (if you need to pre-set the list).
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
My code is below, I'm thinking it has something to do with my if / else statement. Essentially I want to make sure there is at least one radio button in a group checked before I open the next page. Once this works, I'll set it up so that if one of the radiobuttons in the group isn't checked then It will display an error message.
public void onClickListenerButton() {
radioGroup_gender = (RadioGroup) findViewById(R.id.radioGroup_gender);
radioGroup_course = (RadioGroup) findViewById(R.id.radioGroup_course);
button_splits = (Button)findViewById(R.id.button_splits);
button_splits.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.idealsplit.admin.idealsplit.ResultsActivity");
int selectedID_gender = radioGroup_gender.getCheckedRadioButtonId();
int selectedID_course = radioGroup_course.getCheckedRadioButtonId();
radioButton_gender = (RadioButton) findViewById(selectedID_gender);
radioButton_course = (RadioButton) findViewById(selectedID_course);
CharSequence gender, courseType, age, event, time;
if (selectedID_gender == -1) {
//Throw Error
isNull = true;
} else if (selectedID_course == -1) {
//Throw Error
isNull = true;
} else {
courseType = radioButton_course.getText();
gender = radioButton_gender.getText();
startActivity(intent);
}
}
}
);
}
And here is the logcat:
03-01 08:52:22.060 19234-19234/com.idealsplit.admin.idealsplit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.idealsplit.admin.idealsplit, PID: 19234
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.idealsplit.admin.idealsplit/com.idealsplit.admin.idealsplit.ResultsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.idealsplit.admin.idealsplit.ResultsActivity.onCreate(ResultsActivity.java:96)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Thanks for any and all help with this, if more information was needed let me know!
**If it helps I was following this youtube tutorial: https://www.youtube.com/watch?v=3f0NAn5xFy4
It looks like button_splits is null so (Button)findViewById(R.id.button_splits); is returning null.
Ensure that view R.id.button_splits exists.
Your view that is attached to this activity must have a button on it with the id of 'button_splits'.
What is a Null Pointer Exception
also you can stop this NPE happening by ensuring the button is not null before adding the listener, but if the button is null then this will not add the listener.
if (button_splits != null) {
button_splits.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.idealsplit.admin.idealsplit.ResultsActivity");
int selectedID_gender = radioGroup_gender.getCheckedRadioButtonId();
int selectedID_course = radioGroup_course.getCheckedRadioButtonId();
radioButton_gender = (RadioButton) findViewById(selectedID_gender);
radioButton_course = (RadioButton) findViewById(selectedID_course);
CharSequence gender, courseType, age, event, time;
if (selectedID_gender == -1) {
//Throw Error
isNull = true;
} else if (selectedID_course == -1) {
//Throw Error
isNull = true;
} else {
courseType = radioButton_course.getText();
gender = radioButton_gender.getText();
startActivity(new Intent(getApplicationContext(), ResultsActivity.class));
startActivity(intent);
}
}
}
);
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm currently working on an android app to fetch data from the server. It crashes and I get a NullPointerException. Can anyone help me edit my code/ list out my error?
Here is my code (I have read a through few of the forums but did not really understand how to solve my problem)
public class MainActivity extends AppCompatActivity {
ListView lvPost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvPost = (ListView)findViewById(R.id.lvPost);
String url2 = "http://192.168.0.146/client/login.php?format=json";
PostResponseAsyncTask task2 = new PostResponseAsyncTask(MainActivity.this, new AsyncResponse() {
#Override
public void processFinish(String s) {
ArrayList<Post> postList =
new JsonConverter<Post>().toArrayList(s, Post.class);
ArrayList<String> titles = new ArrayList<String>();
for (Post value:postList) {
titles.add(value.post_title);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, titles);
lvPost.setAdapter(adapter);
}
});
task2.execute(url2);
}
}
Here is my logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
at com.example.1231.t12.MainActivity$1.processFinish(MainActivity.java:40)
at com.kosalgeek.genasync12.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:188)
at com.kosalgeek.genasync12.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:27)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6075)
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)
It looks like String s parameter is null, use logging to confirm. In such case Gson.fromJson() returns null as a result. Then you're trying to iterate through the list postList which is null and this results to NullPointerException.