Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBryan Wiedeman
10,203 Pointsthe log.e is causing a null pointer exception
I'm on a different project but am using this for it's login info
I'm trying to send a message to the log with the logged in username if I'm logged in, and send the user to the login screen if not. When I try to run my code I get a nullpointer exception that looks as such:
01-07 00:08:25.314 12048-12048/com.parse.starter E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.parse.starter, PID: 12048
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parse.starter/com.parse.starter.MainActivity}: java.lang.NullPointerException: println needs a message at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418) 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:5289) 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: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.i(Log.java:160) at com.parse.starter.MainActivity.onCreate(MainActivity.java:43) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418) 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:5289) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372)
I tried adding a '+ ""' to the end of getUserName, and that allowed the app to run, but I couldn't find the only username I've made anywhere in the log, so I can't tell if I've done this correctly and am logged in. I also tried adding .toString() after getUserName but the app still didn't run. Thank you in advace :-)
package com.parse.starter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import com.parse.ParseUser;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser == null) {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
Log.i(TAG, currentUser.getUsername());
}
}
public boolean onCreateOptionsMenu (Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Ben Deitch
Treehouse TeacherI'm not familiar enough with Parse to know why this isn't working, but I wouldn't rule out getUsername returning null.
1 Answer
Bryan Wiedeman
10,203 Pointsthe problem turned out to be that the StarterApplication activity created by parse contained a method called enableAutomaticUser() which needed to be deleted.
Bryan Wiedeman
10,203 PointsBryan Wiedeman
10,203 Pointsit seems like getCurrentUser is returning Null, but why doesn't it bring me to the login screen if that's the case?