Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I am new to android app development, and I am currently building an educational app launcher and on an activity the NullPointerException makes the app to stop while running and I don't know how to solve this.

This is my code:

Java
package com.example.homestudent;

import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseException;

import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;



public class UserLogin extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_login);

       // Use StrictMode to counter the actions of the NetworkOnMainThread Exception
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
       StrictMode.setThreadPolicy(policy);

       // Connect to Parse Database
       Parse.initialize(this, "fjXPa3CFuEjoLwVsNOqWI4GqbTPEnWPgyicL75Kk", "2sbmKPxjFT9OFWHhb7qQjDIqnjTEAhVgWRS27r4u");

       // Create Segoe Ui Light Typeface
       Typeface tfSegoeUILight = Typeface.createFromAsset(getAssets(), "fonts/segoeuil.ttf");

       // Create Big Noodle Titling Typeface
       Typeface tfBigNoodleTitling = Typeface.createFromAsset(getAssets(), "fonts/big_noodle_titling.ttf");

       // Create TextView class and connect to layout
       final TextView tvSchoolName = (TextView) findViewById(R.id.tvSchoolName);
       tvSchoolName.setTypeface(tfSegoeUILight);

       // Create EditTesk class and connect to layout
       final EditText etUserName = (EditText) findViewById(R.id.etUserName);
       etUserName.setTypeface(tfSegoeUILight);

       // Create Button class and connect to layout
       Button btnSubmit = (Button) findViewById(R.id.btnSubmit1);
       btnSubmit.setTypeface(tfBigNoodleTitling);

       // Put the value of "code" from the intent into String variable
       String SchoolCode1 = getIntent().getExtras().getString("code");

       // Convert the string value of SchoolCode1 and put it into an int variable
       int SchoolCode = Integer.parseInt(SchoolCode1);

       // ParseQuery to get the School Name from the database
       ParseQuery<ParseObject> query = ParseQuery.getQuery("SchoolData");
       query.whereEqualTo("Code", SchoolCode);
       query.getFirstInBackground(new GetCallback<ParseObject>() {
           public void done(ParseObject object, ParseException e) {
               if (object == null) {
                   tvSchoolName.setText("No School");
               } 
               else {
                   String SchoolName = object.getString("SchoolName");
                   tvSchoolName.setText(String.valueOf(SchoolName));
               }
            }
        }); 
    }


    @Override
    public void onBackPressed() {
        // do nothing.
    }


The logcat is


Java
04-16 12:52:03.731: D/dalvikvm(20239): GC_CONCURRENT freed 378K, 7% free 6948K/7431K, paused 2ms+3ms
04-16 12:52:04.544: D/OpenGLRenderer(20239): Flushing caches (mode 0)
04-16 12:52:05.676: D/dalvikvm(20239): GC_CONCURRENT freed 384K, 7% free 7014K/7495K, paused 2ms+4ms
04-16 12:52:09.731: D/OpenGLRenderer(20239): Flushing caches (mode 1)
04-16 12:52:09.895: D/AndroidRuntime(20239): Shutting down VM
04-16 12:52:09.895: W/dalvikvm(20239): threadid=1: thread exiting with uncaught exception (group=0x40bea1f8)
04-16 12:52:09.895: E/AndroidRuntime(20239): FATAL EXCEPTION: main
04-16 12:52:09.895: E/AndroidRuntime(20239): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homestudent/com.example.homestudent.UserLogin}: java.lang.NullPointerException
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.os.Looper.loop(Looper.java:137)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.ActivityThread.main(ActivityThread.java:4514)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at dalvik.system.NativeStart.main(Native Method)
04-16 12:52:09.895: E/AndroidRuntime(20239): Caused by: java.lang.NullPointerException
04-16 12:52:09.895: E/AndroidRuntime(20239):    at com.example.homestudent.UserLogin.onCreate(UserLogin.java:58)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.Activity.performCreate(Activity.java:4465)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
04-16 12:52:09.895: E/AndroidRuntime(20239):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
04-16 12:52:09.895: E/AndroidRuntime(20239):    ... 11 more
04-16 12:54:11.387: I/Process(20239): Sending signal. PID: 20239 SIG: 9
04-16 12:54:31.098: D/libEGL(21248): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-16 12:54:31.106: D/libEGL(21248): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-16 12:54:31.106: D/libEGL(21248): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-16 12:54:31.309: D/OpenGLRenderer(21248): Enabling debug mode 0
04-16 12:54:40.739: D/dalvikvm(21248): GC_CONCURRENT freed 285K, 6% free 6849K/7239K, paused 4ms+4ms
04-16 12:54:41.856: D/OpenGLRenderer(21248): Flushing caches (mode 0)
04-16 12:54:55.684: D/OpenGLRenderer(21248): Flushing caches (mode 0)
04-16 12:55:01.223: D/dalvikvm(21248): GC_CONCURRENT freed 364K, 7% free 6953K/7431K, paused 2ms+3ms
04-16 12:55:02.208: D/OpenGLRenderer(21248): Flushing caches (mode 0)
04-16 12:55:03.958: D/dalvikvm(21248): GC_CONCURRENT freed 380K, 7% free 7023K/7495K, paused 2ms+5ms
04-16 12:55:08.372: D/OpenGLRenderer(21248): Flushing caches (mode 1)
04-16 12:55:08.520: D/AndroidRuntime(21248): Shutting down VM
04-16 12:55:08.520: W/dalvikvm(21248): threadid=1: thread exiting with uncaught exception (group=0x40bea1f8)
04-16 12:55:08.520: E/AndroidRuntime(21248): FATAL EXCEPTION: main
04-16 12:55:08.520: E/AndroidRuntime(21248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homestudent/com.example.homestudent.UserLogin}: java.lang.NullPointerException
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.os.Looper.loop(Looper.java:137)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.ActivityThread.main(ActivityThread.java:4514)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at dalvik.system.NativeStart.main(Native Method)
04-16 12:55:08.520: E/AndroidRuntime(21248): Caused by: java.lang.NullPointerException
04-16 12:55:08.520: E/AndroidRuntime(21248):    at com.example.homestudent.UserLogin.onCreate(UserLogin.java:58)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.Activity.performCreate(Activity.java:4465)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
04-16 12:55:08.520: E/AndroidRuntime(21248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
04-16 12:55:08.520: E/AndroidRuntime(21248):    ... 11 more
Posted
Updated 17-Apr-14 0:03am
v2

1 solution

 Caused by: java.lang.NullPointerException
04-16 12:52:09.895: E/AndroidRuntime(20239):    at com.example.homestudent.UserLogin.onCreate(UserLogin.java:58)


Have you checked that line nr. 58?
Please turn on Line numbering in Eclipse preferences (Window -> Preferences -> General -> Editors -> Text Editors)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900